Python Matplotlib:TypeError:'AxesSubplot' 对象不可下标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52273546/
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
Matplotlib: TypeError: 'AxesSubplot' object is not subscriptable
提问by SHV_la
I am trying to make a simple box plot of a variable 'x' contained in two dataframes, df1 and df2. To do this I am using the following code:
我正在尝试制作包含在两个数据帧 df1 和 df2 中的变量“x”的简单箱线图。为此,我使用以下代码:
fig, axs = plt.subplots()
axs[0, 0].boxplot([df1['x'], df2['x']])
plt.show();
However, I get this:
但是,我明白了:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-108-ce962754d553> in <module>()
----> 2 axs[0, 0].boxplot([df1['x'], df2['x']])
3 plt.show();
4
TypeError: 'AxesSubplot' object is not subscriptable
Any ideas?
有任何想法吗?
回答by SpghttCd
fig, axs = plt.subplots()
returns a figure with only one single subplot, so axs already holds it without indexing.
返回一个只有一个子图的图形,所以 axs 已经保存了它而没有索引。
fig, axs = plt.subplots(3)
returns a 1D array of subplots.
返回子图的一维数组。
fig, axs = plt.subplots(3, 2)
returns a 2D array of subplots.
返回子图的二维数组。
Note that this is only due to the default setting of the kwarg squeeze=True
.
By setting it to False
you can force the result to be a 2D-array, independant of the number or arrangement of the subplots.
请注意,这仅仅是由于 kwarg 的默认设置squeeze=True
。
通过将其设置为,False
您可以强制结果为 2D 数组,与子图的数量或排列无关。