Matplotlib:IndexError:数组索引太多:数组是一维的,但有 2 个被索引

Zex*_*xxx 0 matplotlib seaborn

我试图将绘图彼此靠近,但这会导致错误:

sns.set_style('darkgrid')
f , axes =plt.subplots(1, 2, figsize=(15,15))

sns.boxplot(data=df, x='Month', y='Price', ax=axes[0,0])
sns.boxplot(data=df, x='Year', y='Price', ax=axes[0,1])
Run Code Online (Sandbox Code Playgroud)

这是错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/tmp/ipykernel_33/3036328835.py in <module>
      2 f , axes =plt.subplots(1, 2, figsize=(15,15))
      3 
----> 4 sns.boxplot(data=df, x='Month', y='Price', ax=axes[0,0])
      5 sns.boxplot(data=df, x='Year', y='Price', ax=axes[0,1])

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
Run Code Online (Sandbox Code Playgroud)

Jod*_*mak 5

axes是一维的,但你试图以二维的方式访问。 axes[0]axes[1]您想要的,或者致电subplots(1, 2, squeeze=False)

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html