Matplotlib: TypeError: 'AxesSubplot' 对象不可下标 [英] Matplotlib: TypeError: 'AxesSubplot' object is not subscriptable
本文介绍了Matplotlib: TypeError: 'AxesSubplot' 对象不可下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对包含在两个数据帧df1和df2中的变量'x'进行简单的箱形图绘制.为此,我使用以下代码:
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:
fig, axs = plt.subplots()
axs[0, 0].boxplot([df1['x'], df2['x']])
plt.show();
但是,我明白了:
---------------------------------------------------------------------------
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
有什么想法吗?
推荐答案
fig, axs = plt.subplots()
返回仅包含一个子图的图形,因此斧头已经拥有该图形而无需编制索引.
returns a figure with only one single subplot, so axs already holds it without indexing.
fig, axs = plt.subplots(3)
返回子图的一维数组.
fig, axs = plt.subplots(3, 2)
返回子图的二维数组.
请注意,这只是由于 kwarg 的默认设置 squeeze=True
.
通过将其设置为 False
,您可以强制结果为2D数组,而与子图的数量或排列方式无关.
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.
这篇关于Matplotlib: TypeError: 'AxesSubplot' 对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文