当我将 pandas 中的箱线图与子图一起使用时出现错误

I get an error when I use boxplot from pandas with subplots

例如,如果我有这个数据框并绘制密度图:

df = pd.DataFrame(np.random.randn(6,6))
df.plot(kind='density', subplots=True, layout=(3,2), sharex=False, sharey=False,
fontsize=1)
pyplot.show()

它产生

问题是,如果我尝试对箱线图做同样的事情,我会得到错误:

df.plot(kind='box', subplots=True, layout=(3,2), sharex=False, sharey=False,
fontsize=1)
pyplot.show()

我收到以下错误:

IndexError: index 0 is out of bounds for axis 0 with size 0

谢谢

你确定这就是使用方法吗boxplot

this

我想应该是这样吧

df.boxplot(by=0,layout=(3,2))

整数列似乎存在错误。这有效:

df = pd.DataFrame(np.random.randn(6,6))
df.columns=list('abcdef')

df.plot.box(subplots=True, layout=(3,2));

输出:

或将列类型更改为 str:

(df.rename(columns=lambda x: str(x))
   .plot.box(subplots=True, layout=(3,2))
)

输出: