问题描述
我正在尝试创建显示均值置信区间的小提琴图.我认为一种简单的方法是在小提琴图的顶部绘制一个点图,但这是行不通的,因为在本例中,他们似乎对x轴使用了不同的索引:
I am trying to create violinplots that shows confidence intervals for the mean. I thought an easy way to do this would be to plot a pointplot on top of the violinplot, but this is not working since they seem to be using different indices for the xaxis as in this example:
import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset("titanic")
titanic.dropna(inplace=True)
fig, (ax1,ax2,ax3) = plt.subplots(1,3, sharey=True, figsize=(12,4))
#ax1
sns.pointplot("who", "age", data=titanic, join=False,n_boot=10, ax=ax1)
#ax2
sns.violinplot(titanic.age, groupby=titanic.who, ax=ax2)
#ax3
sns.pointplot("who", "age", data=titanic, join=False, n_boot=10, ax=ax3)
sns.violinplot(titanic.age, groupby=titanic.who, ax=ax3)
ax3.set_xlim([-0.5,4])
print(ax1.get_xticks(), ax2.get_xticks())
给出:[0 1 2] [1 2 3]
gives: [0 1 2] [1 2 3]
为什么这些图没有为who"变量分配相同的 xtick 数字,我有什么办法可以改变它?
Why are these plots not assigning the same xtick numbers to the 'who'-variable and is there any way I can change this?
我也想知道是否有任何方法可以更改点图的标记,因为正如您在图中看到的那样,该点非常大,以至于它涵盖了整个置信区间.如果可能的话,我想要一条水平线.
I also wonder if there is anyway I can change the marker for pointplot, because as you can see in the figure, the point is so big so that it covers the entire confidence interval. I would like just a horizontal line if possible.
推荐答案
violinplot
接受一个 positions
参数,你可以用它把小提琴放在其他地方(他们目前只是继承默认的 matplotlib boxplot 位置).
violinplot
takes a positions
argument that you can use to put the violins somewhere else (they currently just inherit the default matplotlib boxplot positions).
pointplot
接受一个 markers
参数,您可以使用它来更改点估计的呈现方式.
pointplot
takes a markers
argument that you can use to change how the point estimate is rendered.
这篇关于如何在同一图中使用seaborn点图和小提琴图?(更改点号和点图标记)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!