matplotlib dynamic number of subplot

人走茶凉 提交于 2019-12-11 06:06:56

问题


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}$")

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.


回答1:


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 is then a numpy array. You can then do something for each axes in a loop.

for ax in ax_arr.flatten():
    ax.plot(...)


来源:https://stackoverflow.com/questions/44159397/matplotlib-dynamic-number-of-subplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!