英文:
How can I add space between bars in a dodged (hue) group?
问题
你可以在seaborn histplot中的"shrink"参数上增加一些数值来增加男性和女性柱状图之间的间距。例如:
sns.histplot(data=tips, x="day", hue="sex", multiple="dodge", shrink=0.6)
这将增加男性和女性柱状图之间的间距。
英文:
How can I add space between Male and Female bars in seaborn histplot ?
import seaborn as sns
tips = sns.load_dataset("tips")
sns.histplot(data=tips, x="day", hue="sex", multiple="dodge", shrink=.9)
plt.show()
答案1
得分: 4
这部分的内容翻译如下:
虽然我认为不能直接通过sns.histplot来实现这一目标,但你可以使用更灵活(尽管更复杂)的seaborn.objects接口来达到相同的效果。
对于这个具体的示例,你可以通过在条形之间留有间隔(参见seaborn.objects.Dodge)来实现相同的图形效果:
import seaborn as sns
import seaborn.objects as so
tips = sns.load_dataset("tips")
so.Plot(tips, x="day", color="sex").add(so.Bar(), so.Count(), so.Dodge(gap=0.2))
英文:
While I don't think this can be achieved directly with sns.histplot, you could use the seaborn.objects interface, which is more flexible (at the expense of complexity).
For this specific example, here's how you could achieve the same plot, but with a gap between the bars (see seaborn.objects.Dodge):
import seaborn as sns
import seaborn.objects as so
tips = sns.load_dataset("tips")
so.Plot(tips, x="day", color="sex").add(so.Bar(), so.Count(), so.Dodge(gap=0.2))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论