我的箱线图似乎与绘图的 x 刻度线不一致。如何使箱线图与 x 刻度对齐?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame([['0', 0.3],['1', 0.5],['2', 0.9],
['0', 0.8],['1', 0.3],['2', 0.4],
['0', 0.4],['1', 0.0],['2', 0.7]])
df.columns = ['label', 'score']
label_list = ['0', '1', '2']
fig = plt.figure(figsize=(8, 5))
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list)
g.legend_.remove()
plt.show()
最佳答案
您可以将 dodge=False
添加到您的 boxplot
行中,这应该可以解决此问题。
更新后的代码如下
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False)
然后您可以使用 width
来控制箱线图的宽度(默认宽度为 0.8),如下所示
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False, width=.2)
关于python - 如何将绘图在 xticks 上居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080376/