pyplot 子图因 seaborn 而失败
pyplot subplots fails with seaborn
在尝试并排绘制两个带有 seaborn 的直方图时,出现以下错误:
我的代码:
fig, axs = plt.subplots(1, 2)
sns.distplot(MAPE_per_stock , ax = axs[0,0])
sns.distplot(MAPE_per_stock[start_test:], ax = axs[0, 1])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-212-9c4fad36d97d> in <module>
1 fig, axs = plt.subplots(1, 2)
----> 2 sns.distplot(MAPE_per_stock , ax = axs[0,0])
3 sns.distplot(MAPE_per_stock[start_test:], ax = axs[0, 1])
IndexError: too many indices for array
是什么引起了异常,我应该如何更正代码?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
查看您创建的 axs
axs.shape
out: (2,)
因此,一个沿一个轴具有两个元素的数组。可以使用 axs[0]
、axs[1]
:
访问各个元素
axs[0]
<matplotlib.axes._subplots.AxesSubplot at 0x1965b6bc5f8>
由于数据的排序方向只有一个,指定两个位置会导致您遇到的错误:
axs[0,1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-aaadfc217757> in <module>
----> 1 axs[0,1]
IndexError: too many indices for array
请注意,如果您创建 2 x 2 网格:
fig, axs = plt.subplots(2, 2)
您实际上创建了一个 2 by2 数组,您可以使用问题中的方案对其进行索引。
axs.shape
out: (2,2)
axs[1,1]
out: <matplotlib.axes._subplots.AxesSubplot at 0x1965bcd1860>
在尝试并排绘制两个带有 seaborn 的直方图时,出现以下错误:
我的代码:
fig, axs = plt.subplots(1, 2)
sns.distplot(MAPE_per_stock , ax = axs[0,0])
sns.distplot(MAPE_per_stock[start_test:], ax = axs[0, 1])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-212-9c4fad36d97d> in <module>
1 fig, axs = plt.subplots(1, 2)
----> 2 sns.distplot(MAPE_per_stock , ax = axs[0,0])
3 sns.distplot(MAPE_per_stock[start_test:], ax = axs[0, 1])
IndexError: too many indices for array
是什么引起了异常,我应该如何更正代码?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
查看您创建的 axs
axs.shape
out: (2,)
因此,一个沿一个轴具有两个元素的数组。可以使用 axs[0]
、axs[1]
:
axs[0]
<matplotlib.axes._subplots.AxesSubplot at 0x1965b6bc5f8>
由于数据的排序方向只有一个,指定两个位置会导致您遇到的错误:
axs[0,1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-aaadfc217757> in <module>
----> 1 axs[0,1]
IndexError: too many indices for array
请注意,如果您创建 2 x 2 网格:
fig, axs = plt.subplots(2, 2)
您实际上创建了一个 2 by2 数组,您可以使用问题中的方案对其进行索引。
axs.shape
out: (2,2)
axs[1,1]
out: <matplotlib.axes._subplots.AxesSubplot at 0x1965bcd1860>