[seaborn] seaborn学习笔记5-小提琴图VIOLINPLOT

5 小提琴图Violinplot

(代码下载)
小提琴图允许可视化一个或多个组的数字变量的分布。它与箱形图非常接近,但可以更深入地了解密度。小提琴图特别适用于数据量巨大且无法显示个别观察结果的情况。在seaborn中使用violinplot函数绘制小提琴图,该章节主要内容有:

  1. 基础小提琴图绘制 Basic violinplot
  2. 小提琴图样式自定义 Custom seaborn violinplot
  3. 小提琴图颜色自定义 Control color of seaborn violinplot
  4. 分组小提琴图 Grouped violinplot
  5. 小提琴图组的顺序设置 Control order of groups in violinplot
  6. 显示小提琴图上的观察次数 Show number of observation on violinplot
#调用seaborn
import seaborn as sns
#调用seaborn自带数据集
df = sns.load_dataset('iris')
#显示数据集
df.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

1. 基础小提琴图绘制 Basic violinplot

  • 单个变量 One numerical variable only
  • 包含多个分组的单个变量 One variable and several groups
  • 多个变量 Several variables
  • 水平小提琴图 Horizontal violinplot
# 单个变量 One numerical variable only
# 如果只有一个数值变量,则最好制作直方图或密度图,但是仍然可以用小提琴图来表示
# Make boxplot for one group only
sns.violinplot( y=df["sepal_length"] );
C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

png

# 包含多个分组的单个变量 One variable and several groups
# x为种类名,y为花萼长度
sns.violinplot( x=df["species"], y=df["sepal_length"] );

png

# 多个变量 Several variables
# 单独拿出sepal_length和sepal_width绘制
sns.violinplot(data=df.iloc[:,0:2]);

png

# 水平小提琴图 Horizontal violinplot
# 可以通过orient设定方向,但是交换x,y画水平小提琴图更好
# Just switch x and y
sns.violinplot( y=df["species"], x=df["sepal_length"] );

png

2. 小提琴图样式自定义 Custom seaborn violinplot

  • 线宽自定义 Change line width
  • 图像一般宽度自定义 Change width
# 线宽自定义 Change line width
sns.violinplot( x=df["species"], y=df["sepal_length"], linewidth=5);

png

# 图像一般宽度自定义 Change width
sns.violinplot( x=df["species"], y=df["sepal_length"], width=0.3);

png

3. 小提琴图颜色自定义 Control color of seaborn violinplot

  • 使用调色板 Use a color palette
  • 单种颜色 Uniform color
  • 指定每个组的颜色 Specify color of each group
  • 突出显示一个组 Highlight a group
# 使用调色板 Use a color palette
sns.violinplot( x=df["species"], y=df["sepal_length"], palette="Blues");

png

# 单种颜色 Uniform color
sns.violinplot( x=df["species"], y=df["sepal_length"], color="skyblue");

png

# 指定每个组的颜色 Specify color of each group
# Make a dictionary with one specific color per group:
my_pal = {"versicolor": "g", "setosa": "b", "virginica":"m"}
#plot it
sns.violinplot( x=df["species"], y=df["sepal_length"], palette=my_pal);

png

# 突出显示一个组 Highlight a group
# make a vector of color: red for the interesting group, blue for others:
my_pal = {species: "r" if species == "versicolor" else "b" for species in df.species.unique()} 
# make the plot
sns.violinplot( x=df["species"], y=df["sepal_length"], palette=my_pal);

png

4. 分组小提琴图 Grouped violinplot

# 如果您有一个变量,变量有几个组和子组,您可能需要制作一个分组的小提琴图。
df_test = sns.load_dataset('tips')
# Grouped violinplot 分组
sns.violinplot(x="day", y="total_bill", hue="smoker", data=df_test, palette="Pastel1");

png

5. 小提琴图组的顺序设置 Control order of groups in violinplot

# plot order设置顺序就行
sns.violinplot(x='species', y='sepal_length', data=df, order=[ "versicolor", "virginica", "setosa"]);

png

# Find the order 或者通过设置一定的规则排序
my_order = df.groupby(by=["species"])["sepal_length"].median().iloc[::-1].index
# Give it to the violinplot
sns.violinplot(x='species', y='sepal_length', data=df, order=my_order);

png

6. 显示小提琴图上的观察次数 Show number of observation on violinplot

# Basic violinplot 基础小提琴图像绘制
ax = sns.violinplot(x="species", y="sepal_length", data=df)
 
# Calculate number of obs per group & median to position labels 计算各个样本数量
medians = df.groupby(['species'])['sepal_length'].median().values
nobs = df['species'].value_counts().values
nobs = [str(x) for x in nobs.tolist()]
nobs = ["n: " + i for i in nobs]
 
# Add it to the plot 加入图像
pos = range(len(nobs))
for tick,label in zip(pos,ax.get_xticklabels()):
   ax.text(pos[tick], medians[tick] + 0.03, nobs[tick], horizontalalignment='center', size='x-small', color='w', weight='semibold');

png

  • 15
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Matlab是一种非常强大的科研工具,可以用于绘制各种类型的表,包括小提琴小提琴Violin Plot)是一种常用的数据可视化工具,通过在一个或多个变量上绘制核密度估计、箱线和散点,可以更好地展示数据的分布情况。 在Matlab中,我们可以使用violinplot函数来绘制小提琴。该函数需要提供一个包含数据的矩阵作为输入,其中每一列代表一个变量。通过调整函数的参数,我们可以根据需求进行定制,例如设置小提琴的颜色、边界线样式和宽度等。 绘制小提琴的过程包括以下几个步骤: 1. 创建一个包含数据的矩阵,每一列代表一个变量。 2. 调用violinplot函数,传入数据矩阵作为参数,绘制小提琴。 3. 根据需要,调整小提琴的颜色、边界线样式和宽度等参数。 4. 添加例和标签,使形更加清晰易懂。 5. 根据需要,对绘制的小提琴进行美化和调整。 绘制小提琴可以帮助我们更好地理解数据的分布情况,例如不同变量之间的差异和异常值的存在。通过调整小提琴的参数和美化绘效果,我们可以使像更具吸引力,并达到更好的数据可视化效果。 总而言之,Matlab是一个非常强大的科研工具,可以用于绘制各种类型的科研表,包括小提琴。通过合理选择函数和调整参数,我们可以根据需求绘制出清晰、美观的小提琴,以帮助更好地理解数据的分布情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值