当我使用带有子图的 pandas 箱线图时出现错误

问题描述 投票:0回答:2

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

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()

它的产量

Show image

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

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

我收到以下错误:

IndexError:索引 0 超出尺寸为 0 的轴 0 的范围

谢谢

python pandas plot boxplot
2个回答
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))
)

输出:


0
投票

你确定这就是使用方法吗

boxplot

参见这个

我想事情会是这样的

df.boxplot(by=0,layout=(3,2))
© www.soinside.com 2019 - 2024. All rights reserved.