本文翻译官方文档 matplotlib.pyplot.plot
matplotlib.pyplot.plot
matploblib.pylob.plot(*arg, scalex=True, scaley=True, data=None, **kwargs )
绘制直线和标记点
可调用的方法:
plot([x], y, [fmt], *, data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
点和线的坐标是基于给定的 x , y x,y x,y( x x x是横坐标的数据集合, y y y是纵坐标的数据集合)
可选参数 f m t fmt fmt可方便定义图形基础样式,例如:颜色(color)、标记点形状(marker)和线的风格(linestyle)。该参数传递一个短字符串,例子如下:
plot(x, y) # 用默认的线和标记点绘制
plot(x, y, 'bo') # 用蓝圆标记点绘制
plot(y) # 绘制y,而x是数轴的正半轴
plot(y, 'r+') # 同上,但是线的风格是红色+
你可以用Line2D
属性(即下例的第二种)作为键来控制图形样式。Line
属性和
f
m
t
fmt
fmt可以混用,此时,如果二者产生矛盾,以键属性为准。后面有一个Line2D
属性的表格,例子如下:
plot(x, y, 'go--', linewidth=2, markersize=12)
plot(x, y, color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
绘制带标记的数据
有一种简便的方式代替坐标轴的数据集合绘制带标记的数据,即通过标记来绘制。此时的参数 x , y x,y x,y是数据 d a t a data data的列标记。
plot('xlabel', 'ylabel', data=obj)
obj
可以是任何索引对象。例如:dict
、pandas.DataFrame
或者结构化的numpy
数组。(其实就是任何通过键值对表示的数据)
绘制多个数据集
绘制多个数据集的方法有很多
-
最直接的方法是多次调用
plot
方法。例如:plot(x1, y1, 'bo') plot(x2, y2, 'go')
-
如果x或者y是二维数组,会为其对应的列绘制图像;如果x和y都是二维的,他们形状必须是一样的。如果有一个是 R ( n , m ) R(n,m) R(n,m),另一个长度必须为n。例如:
x = [1, 2, 3] y = np.array([ [1, 2, 5], [3, 4, 3], [5, 6, 8] ]) plot(x, y)
-
第三种方法是描述 [ x ] , y , [ f m t ] [x], y, [fmt] [x],y,[fmt]的组合:
plot(x1, y1, 'g^', x2, y2, 'g-')
在这种方法中,所有的关键字参数对任何一个数据集图像生效。并且,这种方法不能设置
data
参数。例如:x1 = [1, 2, 3] y1 = [2, 4, 6] x2 = [1, 3, 5] y2 = [3, 4, 5] plt.plot(x1, y1, 'g^-', x2, y2, 'r-o')
点的默认样式是不同颜色的圆,如果想不同于默认样式,fmt
参数就很有必要。你也可以用rcParams["axes.prop_cycle"]
改变图像样式。
Line2D属性
Property | Description |
---|---|
agg_filter | a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array |
alpha | scalar or None |
animated | bool |
antialiased or aa | bool |
clip_box | Bbox |
clip_on | bool |
clip_path | Patch or (Path, Transform) or None |
color or c | color |
contains | unknown |
dash_capstyle | CapStyle or {‘butt’, ‘projecting’, ‘round’} |
dash_joinstyle | JoinStyle or {‘miter’, ‘round’, ‘bevel’} |
dashes | sequence of floats (on/off ink in points) or (None, None) |
data | (2, N) array or two 1D arrays |
drawstyle or ds | {‘default’, ‘steps’, ‘steps-pre’, ‘steps-mid’, ‘steps-post’}, default: ‘default’ |
figure | Figure |
fillstyle | {‘full’, ‘left’, ‘right’, ‘bottom’, ‘top’, ‘none’} |
gid | str |
in_layout | bool |
label | object |
linestyle or ls | {’-’, ‘–’, ‘-.’, ‘:’, ‘’, (offset, on-off-seq), …} |
linewidth or lw | float |
marker | marker style string, Path or MarkerStyle |
markeredgecolor or mec | color |
markeredgewidth or mew | float |
markerfacecolor or mfc | color |
markerfacecoloralt or mfcalt | color |
markersize or ms | float |
markevery | None or int or (int, int) or slice or list[int] or float or (float, float) or list[bool] |
path_effects | AbstractPathEffect |
picker | float or callable[[Artist, Event], tuple[bool, dict]] |
pickradius | float |
rasterized | bool |
sketch_params | (scale: float, length: float, randomness: float) |
snap | bool or None |
solid_capstyle | CapStyle or {‘butt’, ‘projecting’, ‘round’} |
solid_joinstyle | JoinStyle or {‘miter’, ‘round’, ‘bevel’} |
transform | matplotlib.transforms.Transform |
url | str |
visible | bool |
xdata | 1D array |
ydata | 1D array |
zorder | float |
Markers
character | description |
---|---|
'.' | point marker |
',' | pixel marker |
'o' | circle marker |
'v' | triangle_down marker |
'^' | triangle_up marker |
'<' | triangle_left marker |
'>' | triangle_right marker |
'1' | tri_down marker |
'2' | tri_up marker |
'3' | tri_left marker |
'4' | tri_right marker |
'8' | octagon marker |
's' | square marker |
'p' | pentagon marker |
'P' | plus (filled) marker |
'*' | star marker |
'h' | hexagon1 marker |
'H' | hexagon2 marker |
'+' | plus marker |
'x' | x marker |
'X' | x (filled) marker |
'D' | diamond marker |
'd' | thin_diamond marker |
'|' | vline marker |
'_' | hline marker |
Line Styles
character | description |
---|---|
'-' | solid line style |
'--' | dashed line style |
'-.' | dash-dot line style |
':' | dotted line style |
Colors
支持一个字母代表颜色,其他颜色可以通过mfc=#fff000
格式设置
character | color |
---|---|
'b' | blue |
'g' | green |
'r' | red |
'c' | cyan |
'm' | magenta |
'y' | yellow |
'k' | black |
- | ------- |
'b' | blue |
'g' | green |
'r' | red |
'c' | cyan |
'm' | magenta |
'y' | yellow |
'k' | black |
'w' | white |