gpt4 book ai didi

python - Seaborn x 轴未涵盖所有数据

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:06 26 4
gpt4 key购买 nike

当我根据 Seaborn website 上的示例使用提示数据集制作 Seaborn 箱线图时x 轴覆盖箱线图的长度。

import seaborn as sns
sns.set_style("ticks")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=tips["total_bill"])

Seaborn boxplot

但是如果我更改数据集并执行相同的操作,x 轴甚至不会覆盖上四分位数。为什么默认值不扩展到覆盖所有数据,我该如何解决这个问题?我希望它从最低数据点以下到最高数据点以上。

my_df = pd.DataFrame({'total_bill' : [3082, 2024, 3333, 1544, 1861]})
sns.set_style("ticks")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=my_df["total_bill"])
sns.despine(trim=True)

boxplot two

最佳答案

您通过在代码中放置 sns.despine(trim=True) 明确要求 seaborn 切割 x 轴。您可以忽略它以获得完整的轴

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

my_df = pd.DataFrame({'total_bill' : [3082, 2024, 3333, 1544, 1861]})
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=my_df["total_bill"])

plt.show()

enter image description here

如果你想去掉刺,你仍然可以添加

sns.despine(left=True)

enter image description here

您可以根据自己的喜好手动设置刻度位置。在最简单的情况下,您使用已经存在的刻度位置,并将它们固定到轴上

ax.set_xticks(ax.get_xticks())

enter image description here

关于python - Seaborn x 轴未涵盖所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848579/

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