编程语言
首页 > 编程语言> > Python | Seaborn可视化合集 | 分布图(Distribution)之小提琴图

Python | Seaborn可视化合集 | 分布图(Distribution)之小提琴图

作者:互联网

分布(Distribution)

1 小提琴图(Violin Plot)

# 加载数据
import seaborn as sns
data = sns.load_dataset('iris')

# 绘制小提琴图
sns.violinplot(x=data["species"], y=data["sepal_length"])

OUTPUT:

1.1 一维变量

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
sns.violinplot(y=data["sepal_length"])
plt.show()

OUTPUT:

​​​​​​​

 1.2 多组数据

import seaborn as sns
import matplotlib.pyplot as plt

da = sns.load_dataset('iris')
 
sns.violinplot(data=da.iloc[:,0:2])
plt.show()

OUTPUT:

1.3 横向提琴图

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# 转换x与y轴
sns.violinplot(y=data["species"], x=data["sepal_length"])
plt.show()

OUTPUT:

1.4 设置背景和改变图形尺寸

import seaborn as sns
import matplotlib.pyplot as plt

# 设置背景
sns.set(style="darkgrid")
data = sns.load_dataset('iris')
 
# 改变线条宽度
sns.violinplot(x=data["species"], y=data["sepal_length"], linewidth=4)
plt.show()

OUTPUT:

import seaborn as sns
import matplotlib.pyplot as plt

# 设置背景
sns.set(style="darkgrid")
data = sns.load_dataset('iris')
 
# 改变图形宽度
sns.violinplot(x=data["species"], y=data["sepal_length"], width=0.5)
plt.show()

1.5 改变颜色

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# 调色盘设置颜色
sns.violinplot(x=data["species"], y=data["sepal_length"], palette="Blues")
plt.show()

OUTPUT:

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# 统一颜色
sns.violinplot(x=data["species"], y=data["sepal_length"], color="teal")
plt.show()

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')

# 个性化设置调色盘
my_palette = {"versicolor": "teal", "setosa": "skyblue", "virginica": "purple"}
 
# 个性化设置颜色
sns.violinplot(x=data["species"], y=data["sepal_length"], palette=my_palette)
plt.show()

1.6 命名

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('iris')

# 命名
sns.violinplot(x='species', y='sepal_length', data=df, order=[ "versicolor", "virginica", "setosa"])
plt.show()

OUTPUT:

 1.7 添加数据标签

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = sns.load_dataset('iris')
 
# 绘制小提琴图
vp = sns.violinplot(x="species", y="sepal_length", data=df)
 
# 计算数值
medians = df.groupby(['species'])['sepal_length'].median().values
num = df['species'].value_counts().values
num = [str(x) for x in num.tolist()]
num = ["n: " + i for i in num]
 
# 添加数据标签
txt = range(len(num))
for tick, label in zip(txt, vp.get_xticklabels()):
   vp.text(txt[tick], medians[tick] + 0.03, num[tick],
            horizontalalignment='center',
            size='large',
            color='white',
            weight='semibold')
plt.show()

OUTPUT:

 1.8 分组小提琴图

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')
 
# 分组
sns.violinplot(x="day", y="total_bill", hue="smoker", data=df, palette="Blues")
plt.show()

OUTPUT:

标签:plt,Seaborn,Python,seaborn,violinplot,sns,import,Distribution,data
来源: https://blog.csdn.net/Luminoll/article/details/122505057