gpt4 book ai didi

python - 如何将绘图在 xticks 上居中对齐

转载 作者:行者123 更新时间:2023-11-30 21:54:26 32 4
gpt4 key购买 nike

我的箱线图似乎与绘图的 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()

enter image description here

最佳答案

您可以将 dodge=False 添加到您的 boxplot 行中,这应该可以解决此问题。更新后的代码如下

g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False)

enter image description here

然后您可以使用 width 来控制箱线图的宽度(默认宽度为 0.8),如下所示

g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False, width=.2)

enter image description here

关于python - 如何将绘图在 xticks 上居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080376/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com