Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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 violinplot?_Python_Seaborn_Mean_Mode_Violin Plot - Fatal编程技术网

Python 如何创建显示平均值、中值和模式的seaborn violinplot?

Python 如何创建显示平均值、中值和模式的seaborn violinplot?,python,seaborn,mean,mode,violin-plot,Python,Seaborn,Mean,Mode,Violin Plot,有没有一种方法可以将平均值和模式添加到小提琴图中?我的一列中有分类数据,下一列中有相应的值。我尝试查看matplotlib Visor plot,因为它在技术上提供了我正在寻找的功能,但它不允许我在x轴上指定类别变量,这在我查看每个类别的数据分布时是至关重要的。我添加了一个小表格来说明数据的形状 plt.figure(figsize=10,15) ax=sns.violinplot(x='category',y='value',data=df) 首先,我们计算模式和方法: import se

有没有一种方法可以将平均值和模式添加到小提琴图中?我的一列中有分类数据,下一列中有相应的值。我尝试查看matplotlib Visor plot,因为它在技术上提供了我正在寻找的功能,但它不允许我在x轴上指定类别变量,这在我查看每个类别的数据分布时是至关重要的。我添加了一个小表格来说明数据的形状

plt.figure(figsize=10,15)
ax=sns.violinplot(x='category',y='value',data=df) 

首先,我们计算模式和方法:

import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame({'Category':[1,2,5,1,2,4,3,4,2],
                   'Value':[1.5,1.2,2.2,2.6,2.3,2.7,5,3,0]})

Means = df.groupby('Category')['Value'].mean()
Modes = df.groupby('Category')['Value'].agg(lambda x: pd.Series.mode(x)[0])
fig, ax = plt.subplots()
sns.violinplot(x='Category',y='Value',data=df,inner=None)
plt.setp(ax.collections, alpha=.3)
plt.scatter(x=range(len(Means)),y=Means,c="k")
plt.scatter(x=range(len(Modes)),y=Modes)
您可以使用seaborn制作基本图,下面我使用
internal=
参数删除内部箱线图,以便我们可以看到模式和方法:

import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame({'Category':[1,2,5,1,2,4,3,4,2],
                   'Value':[1.5,1.2,2.2,2.6,2.3,2.7,5,3,0]})

Means = df.groupby('Category')['Value'].mean()
Modes = df.groupby('Category')['Value'].agg(lambda x: pd.Series.mode(x)[0])
fig, ax = plt.subplots()
sns.violinplot(x='Category',y='Value',data=df,inner=None)
plt.setp(ax.collections, alpha=.3)
plt.scatter(x=range(len(Means)),y=Means,c="k")
plt.scatter(x=range(len(Modes)),y=Modes)