一个箱线图的x轴标签被移动了。

3
我使用以下代码在dataframe中绘制多个列的箱线图。
df = df[["Col1", "Col2", "Col3", "Col4", "Col5", "Col6", "Col7"]]

df.plot.box()
plt.xticks(list(range(len(df.columns))), df.columns, rotation='vertical')
plt.tight_layout()
plt.show()

图表如下所示。

enter image description here

但是,正如您所看到的,x标签向左偏移了一步,右侧的标签没有任何标签。
我想知道解决这个问题的方法。谢谢。

2
range(1, len(df.columns)+1) - ImportanceOfBeingErnest
@ImportanceOfBeingErnest。谢谢,它运行正常。 - k.ko3n
1个回答

3
原因是ticks不正确,因为range(len(df.columns))从零开始,而盒形图文档默认使用range(1,N+1)。如IOBE所指出的那样,您可以在range的开头加1并增加上限以保留相同数量的列以显示。您还可以通过使用positions kwarg在绘图时直接指定ticks。
df = df[["Col1", "Col2", "Col3", "Col4", "Col5", "Col6", "Col7"]]

df.plot.box()
plt.xticks(list(range(1,len(df.columns)+1)), df.columns, rotation='vertical')
plt.tight_layout()
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接