使用Seaborn,我如何从点图上的所有元素出现在violoinplot的元素之上?(Using Seaborn, how do I get all the elements from a point

编程入门 行业动态 更新时间:2024-10-26 02:24:26
使用Seaborn,我如何从点图上的所有元素出现在violoinplot的元素之上?(Using Seaborn, how do I get all the elements from a pointplot to appear above the elements of a violoinplot?)

使用Seaborn 0.6.0,我试图在pointplot上叠加一个pointplot violinplot 。 我的问题是,如下图所示,来自pointplot图的个别观察的'棒'被绘制在来自pointplot图的标记pointplot 。

import seaborn as sns import matplotlib.pyplot as plt fig, ax = plt.subplots(1, figsize=[12,8]) sns.violinplot(x="day", y="total_bill", hue="smoker", data=tips, split=True, inner='stick', ax=ax, palette=['white']*2) sns.pointplot(x="day", y='total_bill', hue="smoker", data=tips, dodge=0.3, ax=ax, join=False)

仔细看这个图,看起来绿色的错误条被绘制在高音棍之上(看星期六),但是蓝色的错误条,蓝色和绿色的点被绘制在小提琴条的下面。

我曾尝试将zorder不同组合zorder给两个函数,但这并没有改善情节外观。 我能做些什么来让点图中的所有元素出现在violoinplot的所有元素之上?

Using Seaborn 0.6.0, I am trying to overlay a pointplot on a violinplot. My problem is that the 'sticks' from the individual observations of the violinplot are plotted on top of the markers from pointplot as you can see below.

import seaborn as sns import matplotlib.pyplot as plt fig, ax = plt.subplots(1, figsize=[12,8]) sns.violinplot(x="day", y="total_bill", hue="smoker", data=tips, split=True, inner='stick', ax=ax, palette=['white']*2) sns.pointplot(x="day", y='total_bill', hue="smoker", data=tips, dodge=0.3, ax=ax, join=False)

Looking closely at this figure, it appears as the green errorbar is plotted above the violoin sticks (look at Saturday), but the blue error bars, and the blue and green dots are plotted underneath the violin sticks.

I have tried passing different combinations of zorder to both functions, but that did not ameliorate the plot appearance. Is there anything I can do to get all the elements from the pointplot to appear above all the elements of the violoinplot?

最满意答案

与Diziet Asahi的答案类似,但更直接一点。 由于我们正在设置zorder,所以我们不需要按照我们希望它们出现的顺序绘制这些图,这可以节省排序艺术家的麻烦。 我也是这么做的,这样点图就不会出现在图例中,没有用处。

import seaborn as sns import matploltlib.pyplot as plt tips = sns.load_dataset("tips") ax = sns.pointplot(x="day", y='total_bill', hue="smoker", data=tips, dodge=0.3, join=False, palette=['white']) plt.setp(ax.lines, zorder=100) plt.setp(ax.collections, zorder=100, label="") sns.violinplot(x="day", y="total_bill", hue="smoker", data=tips, split=True, inner='stick', ax=ax)

在这里输入图像描述

Similar to Diziet Asahi's answer, but a little bit more straightforward. Since we're setting the zorder, we don't need to draw the plots in the order we want them to appear, which saves the trouble of sorting the artists. I'm also making it so that the pointplot doesn't appear in the legend, where it is not useful.

import seaborn as sns import matploltlib.pyplot as plt tips = sns.load_dataset("tips") ax = sns.pointplot(x="day", y='total_bill', hue="smoker", data=tips, dodge=0.3, join=False, palette=['white']) plt.setp(ax.lines, zorder=100) plt.setp(ax.collections, zorder=100, label="") sns.violinplot(x="day", y="total_bill", hue="smoker", data=tips, split=True, inner='stick', ax=ax)

enter image description here

更多推荐

本文发布于:2023-07-04 17:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   出现在   图上   Seaborn   elements

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!