Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在同一图形中使用seaborn pointplot和violinplot?(更改点图的xticks和标记)_Python_Matplotlib_Seaborn - Fatal编程技术网

Python 如何在同一图形中使用seaborn pointplot和violinplot?(更改点图的xticks和标记)

Python 如何在同一图形中使用seaborn pointplot和violinplot?(更改点图的xticks和标记),python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我试图创建ViolinPlot,显示平均值的置信区间。我认为一个简单的方法是在violinplot上绘制一个点图,但这不起作用,因为它们似乎对xaxis使用了不同的索引,如本例所示: import matplotlib.pyplot as plt import seaborn as sns titanic = sns.load_dataset("titanic") titanic.dropna(inplace=True) fig, (ax1,ax2,ax3) = plt.subplots

我试图创建ViolinPlot,显示平均值的置信区间。我认为一个简单的方法是在violinplot上绘制一个点图,但这不起作用,因为它们似乎对xaxis使用了不同的索引,如本例所示:

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])

给出:[0 1 2][1 2 3]

为什么这些图没有将相同的xtick编号分配给“who”-变量?我有什么方法可以改变这一点吗


我还想知道是否有任何方法可以更改pointplot的标记,因为正如您在图中所看到的,该点非常大,以至于它覆盖了整个置信区间。如果可能的话,我只想要一条水平线。

violinplot
接受一个
positions
参数,您可以使用该参数将小提琴放在其他地方(它们当前只继承默认的matplotlib箱线图位置)


pointplot
采用一个
markers
参数,您可以使用该参数更改点估计值的呈现方式。

我在这里发布我的最终解决方案。我之所以要开始绘制这种图,是为了在同一个图中显示有关分布形状、均值偏移和异常值的信息。通过mwaskom的指针和其他一些调整,我终于得到了我想要的。 左手图是与所有数据点比较的线,右边的是我的最后一个数字。小提琴中间的粗灰色线是平均点的自举99%置信区间,这是点线图中的白色水平线。三条虚线是标准的第25、第50和第75百分位,外部的线是在小提琴图顶部绘制的箱线图I的胡须帽。由于我的数据通常有一些极端的点,我需要手动删除这些点,如下图中的两个点,因此将单个数据点绘制为超出这些点的线

现在,除了这些增强型小提琴,我还将继续制作柱状图和箱线图,但我希望发现所有信息都准确地捕获在小提琴图中,我可以开始并依赖它作为我的主要初始数据探索图。下面是生成绘图的最终代码,以防其他人发现它们有用(或发现可以改进的内容)。对箱线图进行了大量调整

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns  

#change the linewidth which to get a thicker confidence interval line
mpl.rc("lines", linewidth=3)
df = sns.load_dataset("titanic")
df.dropna(inplace=True)
x = 'who'
y = 'age'
fig, (ax1,ax2) = plt.subplots(1,2, sharey=True, figsize=(12,6))
#Left hand plot
sns.violinplot(df[y], groupby=df[x], ax=ax1, inner='stick')
#Right hand plot
sns.violinplot(df[y], groupby=df[x], ax=ax2, positions=0)
sns.pointplot(df[x],df[y], join=False, ci=99, n_boot=1000, ax=ax2, color=[0.3,0.3,0.3], markers=' ')
df.boxplot(y, by=x, sym='_', ax=ax2, showbox=False, showmeans=True, whiskerprops={'linewidth':0},
    medianprops={'linewidth':0}, flierprops={'markeredgecolor':'k', 'markeredgewidth':1},
    meanprops={'marker':'_', 'color':'w', 'markersize':6, 'markeredgewidth':1.5},
    capprops={'linewidth':1, 'color':[0.3,0.3,0.3]}, positions=[0,1,2])
#One could argue that this is not beautiful
labels = [item.get_text() + '\nn=' + str(df.groupby(x).size().loc[item.get_text()]) for item in ax2.get_xticklabels()]
ax2.set_xticklabels(labels)
#Clean up
fig.suptitle('')
ax2.set_title('')
fig.set_facecolor('w')

编辑:添加了“n=”

非常感谢。我发誓我试过markers的论点,但没能让它起作用,尽管我用了和现在一样的方法。。。哦,好吧。起初我不想同时移动小提琴和箱线图,但因为我认为它会更复杂,但毕竟很漂亮。我也公布了我的最终数据。如果seaborn中的默认小提琴图可以显示异常值,可能还有胡须帽(可能带有“外部”参数),那就太酷了,但我知道你说过代码需要重构,所以我会坚持我的“逐图”解决方案一段时间。我喜欢你的问题——小提琴图很有用,但可以通过置信度和显示异常值的修改方法来改进。自从这篇文章发表以来,你改进了你的代码吗?如果你有,我很有兴趣看一看。谢谢,很抱歉反应太晚了!除了添加两条不同的离群值线之外,我并没有做更多的分析,除了我在上面的图中使用的四分位数范围之外,还有一条用于分位数截止。我也开始倾向于直接绘制数据点而不是小提琴,至少在我的样本量不大的情况下是这样。这是个好主意,只有在所有样本集中的数据都很稀疏的情况下才选择小提琴。祝你好运
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns  

#change the linewidth which to get a thicker confidence interval line
mpl.rc("lines", linewidth=3)
df = sns.load_dataset("titanic")
df.dropna(inplace=True)
x = 'who'
y = 'age'
fig, (ax1,ax2) = plt.subplots(1,2, sharey=True, figsize=(12,6))
#Left hand plot
sns.violinplot(df[y], groupby=df[x], ax=ax1, inner='stick')
#Right hand plot
sns.violinplot(df[y], groupby=df[x], ax=ax2, positions=0)
sns.pointplot(df[x],df[y], join=False, ci=99, n_boot=1000, ax=ax2, color=[0.3,0.3,0.3], markers=' ')
df.boxplot(y, by=x, sym='_', ax=ax2, showbox=False, showmeans=True, whiskerprops={'linewidth':0},
    medianprops={'linewidth':0}, flierprops={'markeredgecolor':'k', 'markeredgewidth':1},
    meanprops={'marker':'_', 'color':'w', 'markersize':6, 'markeredgewidth':1.5},
    capprops={'linewidth':1, 'color':[0.3,0.3,0.3]}, positions=[0,1,2])
#One could argue that this is not beautiful
labels = [item.get_text() + '\nn=' + str(df.groupby(x).size().loc[item.get_text()]) for item in ax2.get_xticklabels()]
ax2.set_xticklabels(labels)
#Clean up
fig.suptitle('')
ax2.set_title('')
fig.set_facecolor('w')