1.箱型图。
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p1=sns.boxplot( y=df["sepal_length"] ) plt.show() #保存图片 fig = p1.get_figure() fig.set_size_inches(4.8, 4.8) fig.savefig('PNG/#30_Basic_Box_seaborn1.png')import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p2=sns.boxplot( x=df["species"], y=df["sepal_length"] ) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p1=sns.boxplot( y=df["species"], x=df["sepal_length"] ) plt.show() import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p1=sns.boxplot( x=df["species"], y=df["sepal_length"], linewidth=5) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p2=sns.boxplot( x=df["species"], y=df["sepal_length"], notch=True) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p3=sns.boxplot( x=df["species"], y=df["sepal_length"], width=0.3) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p1=sns.boxplot( x=df["species"], y=df["sepal_length"], palette="Blues") plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p2=sns.boxplot( x=df["species"], y=df["sepal_length"], color="skyblue") plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') my_pal = {"versicolor": "g", "setosa": "b", "virginica":"m"} p3=sns.boxplot( x=df["species"], y=df["sepal_length"], palette=my_pal) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') my_pal = {species: "r" if species == "versicolor" else "b" for species in df.species.unique()} p4=sns.boxplot( x=df["species"], y=df["sepal_length"], palette=my_pal) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') ax = sns.boxplot(x='species', y='sepal_length', data=df) for patch in ax.artists: r, g, b, a = patch.get_facecolor() patch.set_facecolor((r, g, b, .3)) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('tips') p1=sns.boxplot(x="day", y="total_bill", hue="smoker", data=df, palette="Set1") plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') p1=sns.boxplot(x='species', y='sepal_length', data=df, order=["virginica", "versicolor", "setosa"]) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') my_order = df.groupby(by=["species"])["sepal_length"].median().iloc[::-1].index p2=sns.boxplot(x='species', y='sepal_length', data=df, order=my_order) plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') ax = sns.boxplot(x='species', y='sepal_length', data=df) ax = sns.swarmplot(x='species', y='sepal_length', data=df, color="grey") plt.show()
import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset('iris') ax = sns.boxplot(x="species", y="sepal_length", data=df) # 计算obs和中位数并设置标签 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()
import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd my_dpi=96 plt.figure(figsize=(480/my_dpi, 480/my_dpi)) # 数据: a = pd.DataFrame({ 'group' : np.repeat('A',500), 'value': np.random.normal(10, 5, 500) }) b = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(13, 1.2, 500) }) c = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(18, 1.2, 500) }) d = pd.DataFrame({ 'group' : np.repeat('C',20), 'value': np.random.normal(25, 4, 20) }) e = pd.DataFrame({ 'group' : np.repeat('D',100), 'value': np.random.uniform(12, size=100) }) df=a.append(b).append(c).append(d).append(e) # 普通的箱型图 sns.boxplot(x='group', y='value', data=df) plt.show()
import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd my_dpi=96 plt.figure(figsize=(480/my_dpi, 480/my_dpi)) # 数据: a = pd.DataFrame({ 'group' : np.repeat('A',500), 'value': np.random.normal(10, 5, 500) }) b = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(13, 1.2, 500) }) c = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(18, 1.2, 500) }) d = pd.DataFrame({ 'group' : np.repeat('C',20), 'value': np.random.normal(25, 4, 20) }) e = pd.DataFrame({ 'group' : np.repeat('D',100), 'value': np.random.uniform(12, size=100) }) df=a.append(b).append(c).append(d).append(e) ax = sns.boxplot(x='group', y='value', data=df) ax = sns.stripplot(x='group', y='value', data=df, color="orange", jitter=0.2, size=2.5) plt.title("Boxplot with jitter", loc="left") plt.show()
import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd my_dpi=96 plt.figure(figsize=(480/my_dpi, 480/my_dpi)) # 数据: a = pd.DataFrame({ 'group' : np.repeat('A',500), 'value': np.random.normal(10, 5, 500) }) b = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(13, 1.2, 500) }) c = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(18, 1.2, 500) }) d = pd.DataFrame({ 'group' : np.repeat('C',20), 'value': np.random.normal(25, 4, 20) }) e = pd.DataFrame({ 'group' : np.repeat('D',100), 'value': np.random.uniform(12, size=100) }) df=a.append(b).append(c).append(d).append(e) sns.violinplot( x='group', y='value', data=df) plt.title("Violin plot", loc="left") plt.show() import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd my_dpi=96 plt.figure(figsize=(480/my_dpi, 480/my_dpi)) # 数据: a = pd.DataFrame({ 'group' : np.repeat('A',500), 'value': np.random.normal(10, 5, 500) }) b = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(13, 1.2, 500) }) c = pd.DataFrame({ 'group' : np.repeat('B',500), 'value': np.random.normal(18, 1.2, 500) }) d = pd.DataFrame({ 'group' : np.repeat('C',20), 'value': np.random.normal(25, 4, 20) }) e = pd.DataFrame({ 'group' : np.repeat('D',100), 'value': np.random.uniform(12, size=100) }) df=a.append(b).append(c).append(d).append(e) ax = sns.boxplot(x="group", y="value", data=df) medians = df.groupby(['group'])['value'].median().values nobs = df.groupby("group").size().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()): plt.text(pos[tick], medians[tick] + 0.4, nobs[tick], horizontalalignment='center', size='medium', color='w', weight='semibold') plt.title("Boxplot with number of observation", loc="left") plt.show()
本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。
本文来自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py