使用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)更多推荐
发布评论