gpt4 book ai didi

python - Seaborn 箱线图单个箱间距

转载 作者:行者123 更新时间:2023-12-01 09:17:35 24 4
gpt4 key购买 nike

如何增加seaborn箱线图中两个特定框之间的空间?在提示数据集中,如何修改周六和周日之间的间距而不影响其他框。我已经在数据框中包含了空列,但使用此解决方法无法控制间距。

%matplotlib inline
import seaborn as sns
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", data=tips)

Tips Boxplot Example

最佳答案

据我所知,这对于seaborn来说是不可能的,因为遗憾的是它没有提供任何修改positions关键字的方法。另请参阅this similar question

最简单的解决方法是使用不同的箱线图函数,例如 pandas 数据帧附带的函数:

bplot = tips.boxplot(by="day", column="total_bill", positions=[1,2,3,4.5])

当然,这远没有 Seaborn 版本那么漂亮。

幸运的是,matplotlib 为那些愿意探索它们的人提供了无限的选择,因此人们可以通过相应地修改绘图的不同部分来创建类似于 seaborn 绘图的东西。

这很接近:

# Prep
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")

# Create boxplot
bplot = tips.boxplot(by="day", column="total_bill", positions=[1,2,3,4.5],
return_type='dict', figsize=(8,6), grid=False, patch_artist=True,
sym='d', fontsize=16)

# Style boxplot
colors = ['blue', 'green', 'red', 'cyan']
for patch, color in zip(bplot['total_bill']['boxes'], colors):
patch.set_facecolor(color)
patch.set_edgecolor('0.2')
patch.set_linewidth(1.5)
for whisker in bplot['total_bill']['whiskers']:
whisker.set_color('0.2')
whisker.set_linewidth(1.5)
for fliers in bplot['total_bill']['fliers']:
fliers.set_markerfacecolor('0.2')
for median in bplot['total_bill']['medians']:
median.set_color('0.2')
median.set_linewidth(1.5)
for caps in bplot['total_bill']['caps']:
caps.set_color('0.2')
caps.set_linewidth(1.5)

# Other adjustments
plt.title("")
plt.suptitle("")
plt.xlabel("day", fontsize=18)
plt.ylabel("total_bill", fontsize=18)

enter image description here

关于python - Seaborn 箱线图单个箱间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51105226/

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