Python教程

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:

这篇关于Python | Seaborn可视化合集 | 分布图(Distribution)之小提琴图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!