My brain hurts
我的大脑伤害
I have some code that produces 33 graphics in one long column
我有一些代码可以在一个长列中生成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')
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.
我想把这些图中的每一个都写在上面,但我所有的尝试都失败了。我试着把ax= I放到plot命令中,我得到了“numpy”。ndarray'对象没有属性'get_figure'。同样,当我缩小,用一个图一个一个图做这个时,我的x和y的尺度都变大了。我觉得我已经接近答案了,但我需要一点推动力。谢谢。
2 个解决方案
#1
13
The axes handles that subplots
returns vary according to the number of subplots requested:
子图返回的轴句柄根据所请求的子图的数量而变化:
- for (1x1) you get a single handle,
- 对于(1x1)你得到一个单独的句柄,
- for (n x 1 or 1 x n) you get a 1d array of handles,
- 对于(n x 1或1 x n)你会得到一个1d的句柄数组,
- for (m x n) you get a 2d array of handles.
- 对于(mxn)你会得到一个二维的句柄数组。
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.
您的问题似乎来自于接口从第2到第3种情况(即1d到2d轴阵列)的改变。如果您事先不知道数组的形状,下面的代码片段将会有所帮助。
I have found numpy's unravel_index
useful for iterating over the axes, e.g.:
我已经发现numpy的分解索引对于迭代坐标轴是有用的,例如:
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).
正如在链接的解决方案中所指出的,如果您可能有1x1的子图(然后接收单个轴句柄;axs = np.array(axs)就足够了。
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).
在仔细阅读了文档之后(糟糕),设置压缩=False force子块,以返回一个2d矩阵,而不考虑ncols/nrows的选择。(挤压默认值为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
.
如果这样做,您可以在两个维度上进行迭代(如果数据是自然的),也可以使用上述任何一种方法对数据进行线性迭代,并将2d索引计算为ax。
#2
0
Expanding on Bonlenfum's answer, here's a way to do it with a groupby clause:
在Bonlenfum的回答中,有一种方法可以用groupby子句来做:
accountList = training.account.unique()
accountList.sort()
for i, group in training.groupby('account'):
ix = np.where(accountList==i)[0][0]
ix = np.unravel_index(ix, ax.shape)
group.plot(ax=ax[ix],title = i)
This way we can use the title in our graphs, and also accommodates groups with values that are missing (i.e. 1, 3, 8)
这样我们就可以在图中使用标题,也可以容纳缺失值的组(即1,3,8)
#1
13
The axes handles that subplots
returns vary according to the number of subplots requested:
子图返回的轴句柄根据所请求的子图的数量而变化:
- for (1x1) you get a single handle,
- 对于(1x1)你得到一个单独的句柄,
- for (n x 1 or 1 x n) you get a 1d array of handles,
- 对于(n x 1或1 x n)你会得到一个1d的句柄数组,
- for (m x n) you get a 2d array of handles.
- 对于(mxn)你会得到一个二维的句柄数组。
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.
您的问题似乎来自于接口从第2到第3种情况(即1d到2d轴阵列)的改变。如果您事先不知道数组的形状,下面的代码片段将会有所帮助。
I have found numpy's unravel_index
useful for iterating over the axes, e.g.:
我已经发现numpy的分解索引对于迭代坐标轴是有用的,例如:
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).
正如在链接的解决方案中所指出的,如果您可能有1x1的子图(然后接收单个轴句柄;axs = np.array(axs)就足够了。
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).
在仔细阅读了文档之后(糟糕),设置压缩=False force子块,以返回一个2d矩阵,而不考虑ncols/nrows的选择。(挤压默认值为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
.
如果这样做,您可以在两个维度上进行迭代(如果数据是自然的),也可以使用上述任何一种方法对数据进行线性迭代,并将2d索引计算为ax。
#2
0
Expanding on Bonlenfum's answer, here's a way to do it with a groupby clause:
在Bonlenfum的回答中,有一种方法可以用groupby子句来做:
accountList = training.account.unique()
accountList.sort()
for i, group in training.groupby('account'):
ix = np.where(accountList==i)[0][0]
ix = np.unravel_index(ix, ax.shape)
group.plot(ax=ax[ix],title = i)
This way we can use the title in our graphs, and also accommodates groups with values that are missing (i.e. 1, 3, 8)
这样我们就可以在图中使用标题,也可以容纳缺失值的组(即1,3,8)