# 加载数据 import seaborn as sns data = sns.load_dataset('iris') # 绘制小提琴图 sns.violinplot(x=data["species"], y=data["sepal_length"])
OUTPUT:
import seaborn as sns import matplotlib.pyplot as plt data = sns.load_dataset('iris') sns.violinplot(y=data["sepal_length"]) plt.show()
OUTPUT:
import seaborn as sns import matplotlib.pyplot as plt da = sns.load_dataset('iris') sns.violinplot(data=da.iloc[:,0:2]) plt.show()
OUTPUT:
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:
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()
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()
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:
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:
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: