经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
Python?Matplotlib通过plt.subplots创建子绘图
来源:jb51  时间:2022/8/1 9:57:31  对本文有异议

前言

plt.subplots调用后将会产生一个图表(Figure)和默认网格(Grid),与此同时提供一个合理的控制策略布局子绘图。

一、只有子图的绘制

如果没有提供参数给subplots将会返回:

Figure一个Axes对象

例子:

  1. fig, ax = plt.subplots()
  2. ax.plot(x, y)
  3. ax.set_title('A single plot')

1

二、单个方向堆叠子图

堆叠子图就需要用到额外的可选参数,分别是子图的行和列数,如果你只传递一个数字,默认列数为1,行堆叠。

比如:

  1. fig, axs = plt.subplots(2)
  2. fig.suptitle('Vertically stacked subplots')
  3. axs[0].plot(x, y)
  4. axs[1].plot(x, -y)

当然如果你的子图比较少,可以考虑用元组接收axes对象:

  1. fig, (ax1, ax2) = plt.subplots(2)
  2. fig.suptitle('Vertically stacked subplots')
  3. ax1.plot(x, y)
  4. ax2.plot(x, -y)

如果想要按照行排列,将参数改成(1,2)即可。

三、行列方向扩展子图

如果行列扩展子图,那么axes返回的则是一个二维Numpy数组。利用axe的flat属性,可以批量对轴进行赋值。

  1. fig, axs = plt.subplots(2, 2)
  2. axs[0, 0].plot(x, y)
  3. axs[0, 0].set_title('Axis [0, 0]')# 等价于axes[0][0]
  4. axs[0, 1].plot(x, y, 'tab:orange')
  5. axs[0, 1].set_title('Axis [0, 1]')
  6. axs[1, 0].plot(x, -y, 'tab:green')
  7. axs[1, 0].set_title('Axis [1, 0]')
  8. axs[1, 1].plot(x, -y, 'tab:red')
  9. axs[1, 1].set_title('Axis [1, 1]')
  10.  
  11. for ax in axs.flat:
  12. ax.set(xlabel='x-label', ylabel='y-label')
  13.  
  14. # Hide x labels and tick labels for top plots and y ticks for right plots.
  15. for ax in axs.flat:
  16. ax.label_outer()

当然你可以用单个轴对象接收:

  1. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
  2. fig.suptitle('Sharing x per column, y per row')
  3. ax1.plot(x, y)
  4. ax2.plot(x, y**2, 'tab:orange')
  5. ax3.plot(x, -y, 'tab:green')
  6. ax4.plot(x, -y**2, 'tab:red')
  7.  
  8. for ax in fig.get_axes():
  9. ax.label_outer()

四、共享轴

默认情况下,每个子图都是独立创建的。

看下面这个例子:

  1. fig, (ax1, ax2) = plt.subplots(2)
  2. fig.suptitle('Axes values are scaled individually by default')
  3. ax1.plot(x, y)
  4. ax2.plot(x + 1, -y)

可以看出两者的横坐标刻度并不对齐,那么应该如何设置共享?答:在subplot创建之时使用sharex=Truesharedy=True分别创建X轴共享或者Y轴共享。

将上边的例子修改为以下:

  1. fig, (ax1, ax2) = plt.subplots(2, sharex=True)
  2. fig.suptitle('Aligning x-axis using sharex')
  3. ax1.plot(x, y)
  4. ax2.plot(x + 1, -y)

结果如下:

OK,看上去确实统一了坐标轴,除此,python帮你移除了多余的坐标刻度,上面中间的刻度被删除了。

如果你觉得中间的留白不太舒服的话,也有办法去除。方法是通过GridSpec对象,但是使用上就比较麻烦了,因为你需要自己创建一个figure并使用add_gridspec返回这个对象,然后再通过subplot进行接下来的操作。

直接看例子吧:

  1. fig = plt.figure()
  2. gs = fig.add_gridspec(3, hspace=0)
  3. axs = gs.subplots(sharex=True, sharey=True)
  4. fig.suptitle('Sharing both axes')
  5. axs[0].plot(x, y ** 2)
  6. axs[1].plot(x, 0.3 * y, 'o')
  7. axs[2].plot(x, y, '+')
  8.  
  9. # Hide x labels and tick labels for all but bottom plot.
  10. for ax in axs:
  11. ax.label_outer()

这里还用到了轴的label_outer方法,这是用来隐藏非边界的坐标轴的。“share”在这里的意思是:共享一个坐标轴,也就意味着刻度的位置是对齐的。

请注意,修改sharex和sharey是全局修改的,所以你如果想让每一行和每一列共享一个坐标轴,可以考虑用sharex='col', sharey='row'

  1. fig = plt.figure()
  2. gs = fig.add_gridspec(2, 2, hspace=0, wspace=0)
  3. (ax1, ax2), (ax3, ax4) = gs.subplots(sharex='col', sharey='row')
  4. fig.suptitle('Sharing x per column, y per row')
  5. ax1.plot(x, y)
  6. ax2.plot(x, y**2, 'tab:orange')
  7. ax3.plot(x + 1, -y, 'tab:green')
  8. ax4.plot(x + 2, -y**2, 'tab:red')
  9.  
  10. for ax in axs.flat:
  11. ax.label_outer()

如果你需要关联更加复杂的共享轴关系,可以创建出来使用axe的成员sharex、sharey进行设置:

  1. fig, axs = plt.subplots(2, 2)
  2. axs[0, 0].plot(x, y)
  3. axs[0, 0].set_title("main")
  4. axs[1, 0].plot(x, y**2)
  5. axs[1, 0].set_title("shares x with main")
  6. axs[1, 0].sharex(axs[0, 0])
  7. axs[0, 1].plot(x + 1, y + 1)
  8. axs[0, 1].set_title("unrelated")
  9. axs[1, 1].plot(x + 2, y + 2)
  10. axs[1, 1].set_title("also unrelated")
  11. fig.tight_layout()# 让绘图更加紧凑

五、极坐标子图

  1. fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar'))
  2. ax1.plot(x, y)
  3. ax2.plot(x, y ** 2)
  4. plt.show()

到此这篇关于Python Matplotlib通过plt.subplots创建子绘图的文章就介绍到这了,更多相关Python创建子绘图内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号