matplotlib-- Add a legend and notes

Coding #:. 8 UTF- 
Import matplotlib.pyplot AS PLT
Import numpy AS NP
X1 = np.random.normal ( 30, . 3, 100)
X2 = np.random.normal ( 20 is, 2, 100)
X3 = np.random. Normal ( 10, . 3, 100)

# normal randomly generated x1, x2, x3, and assign a label for each string Plot
plt.plot (X1, label = "Plot")
plt.plot (X2, label = "Plot2")
plt.plot (X3, label = "plot3")

# loc position setting parameter determining the legend box
provided for the number of columns # nloc = 3, the specified border (bbox_to_anchor) the starting position (0.0,1.02) setting a width and a height of 0.102, that these values are based on the normalized coordinate system. None parameter or mode can be set to expand, when to expand, the legend box will expand to the entire horizontal axis region. Borderaxespad parameter specifies the spacing between axes and legends boundary.

plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=3, mode="expand", borderaxespad=0.)

# 进行备注
plt.annotate("Important value", (55,20), xycoords='data',
xytext=(5, 38),
arrowprops=dict(arrowstyle='->'))
plt.show()

 

plt.annotate () function parses:

# Plt.annotate () function is used to dimension text
plt.annotate(s='str', xy=(x,y) , xytext=(l1,l2) , ... )
 

Parameters explanation:

s is a text annotation

xy coordinate point as being annotated

xytext coordinate position of the note text

xycoords parameters are as follows:

    Figure Points: bottom left point of
    figure pixels: the pixel lower left corner of FIG
    figure fraction: the lower left portion of the graph
    axes points: coordinates the lower left corner point of the shaft
    axes pixels: the pixel coordinates of the lower left corner shaft
    axes fraction: left lower score shaft
    data: annotation objects using the coordinate system (the default)
    Polar (Theta, R & lt): Not Native IF 'Data' T coordinates

weight set Font linear

    { 'ultralight', 'light' , 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy ',' Extra Bold ',' Black '}

color set Font color

    {' B ',' G ',' R & lt ',' C ',' m ',' Y ',' K ',' W '}
    ' Black ',' red 'and other

    floating-point data between [0,1]
    the RGB or the RGBA, such as: (0.1, 0.2, 0.5), (0.1, 0.2, 0.5, 0.3), etc.

arrowprops # arrow parameter of type a dictionary dict

    width: thickness of the arrows (in points)
    headwidth: bottom arrow points width units
    headlength: length of the arrow (in points)
    Shrink: part of the total length, from ends "shrink"
    facecolor: arrow color

bbox to increase the frame header, the following common parameters:

    boxstyle: block shape
    facecolor :( abbreviated fc) background color
    edgecolor :( abbreviated EC) border line color
    edgewidth: border line size

# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
def test1():
x = np.arange(0, 50, 5)
print(x)
y = x * x
plt.plot(x, y, marker="o")
for xy in zip(x,y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(2, 4), textcoords='offset points')
plt.show()

# test1()

def test2():
x = np.arange(0,50, 5)
y = x * x

plt.plot(x, y, marker='o')
# weight:增加字体线型
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', weight='heavy')
plt.show()
# test2()

def test3():
x = np.arange(0,50,5)
y = x * x
plt.plot(x, y, marker='o')
# 增加字体的颜色
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', color='y', weight='heavy')
plt.show()

# test3()

def test4():
x = np.arange(0, 50, 10)
y = x * x
plt.plot(x, y, marker='o')
# 增加箭头
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', color='y', weight='heavy',
arrowprops=dict(facecolor="y", headlength=10, headwidth=10, width=20))
plt.show()

# test4()

def test5():
x = np.arange(0, 50, 10)
y = x * x
plt.plot(x, y, marker='o')
# 增加标题外框
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points',color='y', weight='heavy',
arrowprops=dict(facecolor="y", headlength=10, headwidth=10, width=20),
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k', lw=1, alpha=0.5))
plt.show()

# test5()

def test6():
x = np.arange(0, 50, 10)
y = x * x
plt.plot(x, y, marker='o')
# 增加箭头指示
for xy in zip(x, y):
# plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', color='y', weight='heavy',
# arrowprops=dict(facecolor="y", headlength=10, headwidth=10, width=20),
# bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k', lw=1, alpha=0.5))
plt.annotate('local max', xy=(5, 25), xytext=(20, 25), arrowprops=dict(facecolor='red', shrink=0.05))
plt.show()

# test6()

Guess you like

Origin www.cnblogs.com/zhouzetian/p/11611858.html