matplotlib.pyplot.plot教程

本文翻译官方文档 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可以是任何索引对象。例如:dictpandas.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属性

PropertyDescription
agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alphascalar or None
animatedbool
antialiased or aabool
clip_boxBbox
clip_onbool
clip_pathPatch or (Path, Transform) or None
color or ccolor
containsunknown
dash_capstyleCapStyle or {‘butt’, ‘projecting’, ‘round’}
dash_joinstyleJoinStyle or {‘miter’, ‘round’, ‘bevel’}
dashessequence 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’
figureFigure
fillstyle{‘full’, ‘left’, ‘right’, ‘bottom’, ‘top’, ‘none’}
gidstr
in_layoutbool
labelobject
linestyle or ls{’-’, ‘–’, ‘-.’, ‘:’, ‘’, (offset, on-off-seq), …}
linewidth or lwfloat
markermarker style string, Path or MarkerStyle
markeredgecolor or meccolor
markeredgewidth or mewfloat
markerfacecolor or mfccolor
markerfacecoloralt or mfcaltcolor
markersize or msfloat
markeveryNone or int or (int, int) or slice or list[int] or float or (float, float) or list[bool]
path_effectsAbstractPathEffect
pickerfloat or callable[[Artist, Event], tuple[bool, dict]]
pickradiusfloat
rasterizedbool
sketch_params(scale: float, length: float, randomness: float)
snapbool or None
solid_capstyleCapStyle or {‘butt’, ‘projecting’, ‘round’}
solid_joinstyleJoinStyle or {‘miter’, ‘round’, ‘bevel’}
transformmatplotlib.transforms.Transform
urlstr
visiblebool
xdata1D array
ydata1D array
zorderfloat

Markers

characterdescription
'.'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

characterdescription
'-'solid line style
'--'dashed line style
'-.'dash-dot line style
':'dotted line style

Colors

支持一个字母代表颜色,其他颜色可以通过mfc=#fff000格式设置

charactercolor
'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
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eva_5433

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值