matplotlib子图的动态编号 [英] matplotlib dynamic number of subplot
问题描述
我正在尝试使用 matplotlib
获取一个子图,并在运行时计算出 subplots
的数量(因为 pnum
在下面的示例中有所不同)
I am trying to get a subplot using matplotlib
, with number of subplots
calculated in runtime (as pnum
varies in the example below)
pnum = len(args.m)
f, (ax1, ax2) = plt.subplots(pnum, sharex=True, sharey=True)
ax1.plot(x,ptp, "#757578",label="Total")
ax2.fill_between(x,dxyp,facecolor="C0", label="$d_{xy}$")
很明显,此示例仅在pnum = 2时有效.所以,我需要做其他事情.
This example, obviously, only work, when pnum=2. So, I need to do some thing else.
我已经检查了这个问题的可接受答案,但这是在所有情节中绘制相同的内容.
I have checked the accepted answer of this question, but this is plotting same thing in all the plots.
推荐答案
要创建动态数量的子图,您可以决定不单独指定坐标轴,而是指定坐标轴数组
To create a dynamic number of subplots you may decide not to specify the axes individually, but as an axes array
pnum = len(args.m)
fig, ax_arr = plt.subplots(pnum, sharex=True, sharey=True)
ax_arr
然后是一个 numpy 数组.然后,您可以为循环中的每个轴做一些事情.
ax_arr
is then a numpy array.
You can then do something for each axes in a loop.
for ax in ax_arr.flatten():
ax.plot(...)
这篇关于matplotlib子图的动态编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!