Task02:艺术画笔见乾坤

Matplotlib的三层api(应用程序编程接口)

  • matplotlib.backend_bases.FigureCanvas:绘图区
  • matplotlib.backend_bases.Renderer:渲染器
  • matplotlib.artist.Artist:绘图组件

基本要素-primitives

1、Line2D

  • 常见参数:
    • xdata:x轴上的取值
    • ydata:y轴上的取值
    • linewidth:线条的宽度
    • linestyle:'-'、'–'、'-.'、':'、'.'。
    • color:线条的颜色
    • marker:标记点
    • markersize:标记的size
  • 参数的设置:
    • 直接在plot()函数中设置
      plt.plot(x, y, linewidth=10)
      
    • 通过获得线对象,对线对象进行设置
      line, = plt.plot(x, y, '-')`# 这里等号坐标的line,是一个列表解包的操作,目的是获取plt.plot返回列表中的Line2D对象
      
    • 获得线属性,使用setp()函数设置
      lines = plt.plot(x, y)
      plt.setp(lines, color='r', linewidth=0)
      
  • 绘制曲线
    • plot方法绘制
      x = range(0,5)
      y1 = [2,5,7,8,10]
      y2= [3,6,8,9,11]
      
      fig,ax= plt.subplots()
      ax.plot(x,y1)
      ax.plot(x,y2)
      
    • Line2D方法
      x = range(0,5)
      y1 = [2,5,7,8,10]
      y2= [3,6,8,9,11]
      fig,ax= plt.subplots()
      lines = [Line2D(x, y1), Line2D(x, y2,color='orange')]  # 显式创建Line2D对象
      for line in lines:
          ax.add_line(line) # 使用add_line方法将创建的Line2D添加到子图中
      
  • 绘制误差折线图
    • 主要参数:
      • yerr: 指定y轴水平的误差
      • xerr: 指定x轴水平的误差
      • fmt: 指定折线图中某个点的颜色,形状,线条风格
      • ecolor: 指定error bar的颜色
      • elinewidth: 指定error bar的线条宽度
    • 代码实现:
      fig = plt.figure()
      x = np.arange(10)
      y = 2.5 * np.sin(x / 20 * np.pi)
      yerr = np.linspace(0.05, 0.2, 10)
      plt.errorbar(x, y + 3, yerr=yerr, label='both limits (default)');
      
posted @ 2022-05-18 00:54  Mundane-_-  阅读(44)  评论(0编辑  收藏  举报