Python画小提琴图之seaborn

1.小提琴图。

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

p1=sns.violinplot( y=df["sepal_length"] )
plt.show()

Python画小提琴图之seaborn_第1张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

p2=sns.violinplot( x=df["species"], y=df["sepal_length"] )
plt.show()

 Python画小提琴图之seaborn_第2张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

p1=sns.violinplot( y=df["species"], x=df["sepal_length"] )
plt.show()

 Python画小提琴图之seaborn_第3张图片

 

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

p1=sns.violinplot( x=df["species"], y=df["sepal_length"], linewidth=5)
plt.show()

Python画小提琴图之seaborn_第4张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

p3=sns.violinplot( x=df["species"], y=df["sepal_length"], width=0.3)
plt.show()

 Python画小提琴图之seaborn_第5张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

p1=sns.violinplot( x=df["species"], y=df["sepal_length"], palette="Blues")
plt.show()

 Python画小提琴图之seaborn_第6张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

my_pal = {"versicolor": "g", "setosa": "b", "virginica":"m"}
p3=sns.violinplot( x=df["species"], y=df["sepal_length"], palette=my_pal)
plt.show()

 Python画小提琴图之seaborn_第7张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

my_pal = {species: "r" if species == "versicolor" else "b" for species in df.species.unique()}
p4=sns.violinplot( x=df["species"], y=df["sepal_length"], palette=my_pal)
plt.show()

 Python画小提琴图之seaborn_第8张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('tips')

p1=sns.violinplot(x="day", y="total_bill", hue="smoker", data=df, palette="Pastel1")
plt.show()

 Python画小提琴图之seaborn_第9张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

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

 Python画小提琴图之seaborn_第10张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

my_order = df.groupby(by=["species"])["sepal_length"].median().iloc[::-1].index

p2=sns.violinplot(x='species', y='sepal_length', data=df, order=my_order)
plt.show()

 Python画小提琴图之seaborn_第11张图片

import matplotlib.pylab as plt
import seaborn as sns
df = sns.load_dataset('iris')

ax = sns.violinplot(x="species", y="sepal_length", data=df)

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]

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')
plt.show()

 Python画小提琴图之seaborn_第12张图片

本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。

Python画小提琴图之seaborn_第13张图片 

本文来自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py 

你可能感兴趣的:(python画图)