pandas 在 Matplotlib 中循环创建子图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/18901571/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create subplots in Matplotlib in a loop?
提问by dartdog
I am using this code which provides nice plots one after the next (using IPython-notebook & Pandas)
我正在使用这段代码,它提供了一个接一个的漂亮图(使用 IPython-notebook 和 Pandas)
for subsm in subsl:
    H7, subsm = sumsubdesc2(table, subsm)   
    ax1=H7.plot()
    plt.title('Rolling 4q mean %s'%(subsm))
    ax1.set_title('Rolling 4q mean %s'%(subsm))
    ax1.set_ylim(100000,600000)
I'd like to get the plots "2up" one next to the next for 3 rows total (5 subplots) can't figure out how to handle that since all the subplot examples seem to be for subplotting ether the data or specific plots and specific grid placement.
我想将图“2up”一个挨着下一个,总共 3 行(5 个子图)无法弄清楚如何处理它,因为所有子图示例似乎都是用于对数据或特定图进行子图和特定的网格放置。
So I don't know how to create the main plot and then subplot a number of graphs (in this case 5) with titles as two-up?
所以我不知道如何创建主图,然后将一些图(在本例中为 5)与标题为两个图进行子图?
Edit line two of code since I left out the function call ;-(
编辑第二行代码,因为我省略了函数调用;-(
回答by Phillip Cloud
Here's what you need to do:
您需要执行以下操作:
import math
import matplotlib.pylab as plt
nrows = int(math.ceil(len(subsl) / 2.))
fig, axs = plt.subplots(nrows, 2)
ylim = 100000, 600000
for ax, subsm in zip(axs.flat, subsl):
    H7, subsm = sumsubdesc2(table, subsm)
    H7.plot(ax=ax, title='Rolling 4q mean %s' % subsm)
    ax.set_ylim(ylim)
This will work even if axs.size > len(subsl)since StopIterationis raised when the shortest iterable runs out. Note that axs.flatis an iterator over the row-orderflattened axsarray.
这将工作,即使axs.size > len(subsl)因为StopIteration升高时最短可迭代用完。请注意,这axs.flat是行序扁平化axs数组上的迭代器。
To hide the last plot that isn't showing, do this:
要隐藏未显示的最后一个图,请执行以下操作:
axs.flat[-1].set_visible(False)
More generally, for axs.size - len(subsl)extra plots at the end of the grid do:
更一般地,对于axs.size - len(subsl)网格末尾的额外图,请执行以下操作:
for ax in axs.flat[axs.size - 1:len(subsl) - 1:-1]:
    ax.set_visible(False)
That slice looks a little gnarly, so I'll explain:
那个切片看起来有点粗糙,所以我来解释一下:
The array axshas axs.sizeelements. The index of the last element of the flattened version of axsis axs.size - 1. subslhas len(subsl)elements and the same reasoning applies about the index of the last element. But, we need to move back from the last element of axsto the last plottedelement so we need to step by -1.
数组axs有axs.size元素。的展平版本的最后一个元素的索引axs是axs.size - 1。subsl有len(subsl)元素,同样的推理适用于最后一个元素的索引。但是,我们需要从 的最后一个元素移回到最后axs一个绘制的元素,因此我们需要逐步 -1。
回答by Greg Whittier
I'm not sure, but I think what you're asking is
我不确定,但我想你要问的是
# not tested
import math
import matplotlib.pylab as plt
Nrows = math.ceil(len(subsl) / 2.)
for i in range(len(subsl)):
    subsm = subsl[i]
    H7, subsm = sumsubdesc2(table, subsm) 
    plt.subplot(Nrows, 2, i+1)
    # do some plotting
    plt.title('Rolling 4q mean %s'%(subsm))
I'm not sure what you mean by "titles as two-up."
我不确定你所说的“标题为二连胜”是什么意思。

