Seaborn x轴未覆盖所有数据

时间:2018-06-14 01:57:17

标签: python python-3.x seaborn

当我根据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

1 个答案:

答案 0 :(得分:1)

您明确要求seaborn通过在代码中放置sns.despine(trim=True)来切割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