是否可以在 matplotlib 中自动生成多个子图? [英] Is it possible to automatically generate multiple subplots in matplotlib?
本文介绍了是否可以在 matplotlib 中自动生成多个子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在matplotlib中自动生成多个子图?我要自动化的过程的一个示例是:
Is it possible to automatically generate multiple subplots in matplotlib? An example of the process I want to automate is:
import matplotlib.pyplot as plt
figure = plt.figure()
ax1 = figure.add_subplot(2, 3, 1)
ax2 = figure.add_subplot(2, 3, 2)
ax3 = figure.add_subplot(2, 3, 3)
ax4 = figure.add_subplot(2, 3, 4)
ax5 = figure.add_subplot(2, 3, 5)
ax6 = figure.add_subplot(2, 3, 6)
子图需要唯一的名称,因为这将允许我执行以下操作:
The subplots need unique names, as this will allow me to do stuff like:
for ax in [ax1, ax2, ax3, ax4, ax5, ax6]:
ax.set_title("example")
非常感谢.
补充:是否有任何函数可以自动生成多个子图?如果我需要重复上述过程 100 次怎么办?我是否必须键入每个ax1到ax100?
Addition: Are there any functions that automate the generation of multiple subplots? What if I needed to repeat the above process 100 times? Would I have to type out every ax1 to ax100?
推荐答案
您可以使用:
fig, axs = plt.subplots(2,3)
axs 将是一个包含子图的数组.
axs will be an array containing the subplots.
或立即解压缩阵列:
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2,3)
这篇关于是否可以在 matplotlib 中自动生成多个子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文