将pandas DataFrame.plot填充到matplotlib子图中(Stuffing a pandas DataFrame.plot into a matplotlib subplot)

编程入门 行业动态 更新时间:2024-10-23 23:31:10
将pandas DataFrame.plot填充到matplotlib子图中(Stuffing a pandas DataFrame.plot into a matplotlib subplot)

我的脑袋疼

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

#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尺度都会发生变化。 我觉得我接近答案,但我需要一点点推动。 谢谢。

My brain hurts

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')

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返回值的变化:

对于(1x1)你得到一个单一的句柄, 对于(nx 1或1 xn)你得到一个1d数组的句柄, 对于(mxn),你会得到一个2d的句柄数组。

看起来你的问题是由第二到第三种情况(即1d到2d轴阵列)的界面变化引起的。 如果您事先不知道阵列形状将如何,以下片段可以提供帮助。

我发现numpy的unravel_index对遍历坐标轴很有用,例如:

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/...)

你也可以重新设计返回的数组,使它是线性的(就像我在这个答案中所用的那样):

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

正如在链接解决方案中指出的,如果您可能有1x1个子图(然后接收单个轴手柄; axs = np.array(axs)就足够了), axs = np.array(axs)需要一些按摩。


在仔细阅读文档 (oops)后,设置squeeze=False强制subplots返回一个二维矩阵,而不管ncols / nrows的选择。 ( squeeze默认为True)。

如果你这样做,你可以迭代两个维度(如果它对你的数据来说很自然),或者使用上述任何一种方法来线性迭代你的数据并计算一个2d索引到ax 。

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

for (1x1) you get a single handle, for (n x 1 or 1 x n) you get a 1d array of handles, for (m x n) you get a 2d array of handles.

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.

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(...)

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).


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).

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.

更多推荐

本文发布于:2023-07-29 20:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   plot   pandas   DataFrame   subplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!