将pandas DataFrame.plot填充到matplotlib子图中 [英] Stuffing a pandas DataFrame.plot into a matplotlib subplot

查看:325
本文介绍了将pandas DataFrame.plot填充到matplotlib子图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脑痛

我有一些代码可以在一长列中生成33个图形

I have some code that produces 33 graphics in one long column

#fig,axes = plt.subplots(nrows=11,ncols=3,figsize=(18,50))
accountList =  list(set(training.account))
for i in range(1,len(accountList)):
    training[training.account == accountList[i]].plot(kind='scatter',x='date_int',y='rate',title=accountList[i])
#axes[0].set_ylabel('Success Rate')

我想将以上每个图都添加到上面已注释掉的图中,但是我的所有尝试都失败了.我尝试将ax=i放入plot命令中,然后得到'numpy.ndarray' object has no attribute 'get_figure'.同样,当我缩小比例并在一张一张一张地画一个图形的情况下执行此操作时,我的x和y比例都会变大.我觉得我已经接近答案了,但是我需要一点推动力.谢谢.

I'd like to get each of those plots into the figure that I have commented out above, but all my attempts are failing. I tried putting ax=i into the plot command and I get 'numpy.ndarray' object has no attribute 'get_figure'. Also, when I scale back and do this with one single plot in a one by one figure, my x and y scales both go to heck. I feel like I'm close to the answer, but I need a little push. Thanks.

推荐答案

subplots返回的轴句柄根据请求的子图的数量而有所不同:

The axes handles that subplots returns vary according to the number of subplots requested:

  • 对于(1x1),您只有一个手柄,
  • 对于(n x 1或1 x n),您将获得一维手柄数组,
  • 对于(m x n),您将获得一个二维手柄数组.

您的问题似乎是由于界面从第2种情况变化到第3种情况(即1d到2d轴阵列)引起的.如果您不提前知道数组的形状,以下代码片段将对您有所帮助.

It appears that your problem arises from the change in interface from the 2nd to 3rd cases (i.e. 1d to 2d axis array). The following snippets can help if you don't know ahead of time what the array shape will be.

我发现numpy的unravel_index对于在轴上进行迭代非常有用,例如:

I have found numpy's unravel_index useful for iterating over the axes, e.g.:

ncol = 3 # pick one dimension
nrow = (len(accountList)+ ncol-1) / ncol # make sure enough subplots
fig, ax = plt.subplots(nrows=nrow, ncols=ncol) # create the axes

for i in xrange(len(accountList)):   # go over a linear list of data
  ix = np.unravel_index(i, ax.shape) # compute an appropriate index (1d or 2d)

  accountList[i].plot( ..., ax=ax[ix])   # pandas method plot
  ax[ix].plot(...)   # or direct axis object method plot (/scatter/bar/...)


您还可以调整返回数组的形状,使其呈线性(如我在此答案中使用的那样):


You can also reshape the returned array so that it is linear (as I used in this answer):

for a in ax.reshape(-1):
    a.plot(...)

如链接的解决方案中所述,如果您可能具有1x1子图(然后接收单轴手柄; axs = np.array(axs)就足够了),则斧头需要一点按摩.

As noted in the linked solution, axs needs a bit of massaging if you might have 1x1 subplots (and then receive a single axes handle; axs = np.array(axs) is enough).

然后仔细阅读(哎呀)设置后,请仔细阅读文档 squeeze=False强制subplots返回2d矩阵,而与ncols/nrows的选择无关. (squeeze默认为True).

And after reading the docs more carefully (oops), setting squeeze=False forces subplots to return a 2d matrix regardless of the choices of ncols/nrows. (squeeze defaults to True).

如果执行此操作,则可以在两个维度上进行迭代(如果对数据而言是自然的话),或者使用上述方法之一对数据进行线性迭代并计算2d索引到ax中.

If you do this, you can either iterate over two dimensions (if it is natural for your data), or use either of the above approaches to iterate over your data linearly and computing a 2d index into ax.

这篇关于将pandas DataFrame.plot填充到matplotlib子图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆