1. 程式人生 > >Python資料視覺化—seaborn

Python資料視覺化—seaborn

Seaborn其實是在matplotlib的基礎上進行了更高階的API封裝,從而使得作圖更加容易,在大多數情況下使用seaborn就能做出很具有吸引力的圖。這裡例項採用的資料集都是seaborn提供的幾個經典資料集,dataset檔案可見於Github。

1  set_style( )  set( )

set_style( )是用來設定主題的,Seaborn有五個預設好的主題: darkgrid , whitegrid , dark , white ,和 ticks  預設: darkgrid

import matplotlib.pyplot as plt import seaborn as snssns.set_style("whitegrid")

plt.plot(np.arange(10)) plt.show()

set( )通過設定引數可以用來設定背景,調色盤等,更加常用。 import seaborn as sns import matplotlib.pyplot as pltsns.set(style="white", palette="muted", color_codes=True)     #set( )設定主題,調色盤更常用 plt.plot(np.arange(10)) plt.show()

2  distplot( )  kdeplot( )

distplot( )為hist加強版,kdeplot( )為密度曲線圖 

import matplotlib.pyplot as plt import seaborn as sns df_iris = pd.read_csv('../input/iris.csv') fig, axes = plt.subplots(1,2)sns.distplot(df_iris['petal length'], ax = axes[0], kde = True, rug = True)        # kde 密度曲線  rug 邊際毛毯sns.kdeplot(df_iris['petal length'], ax = axes[1], shade=True)                     # shade  陰影  
                     plt.show()

import numpy as np import seaborn as sns import matplotlib.pyplot as plt sns.set( palette="muted", color_codes=True) rs = np.random.RandomState(10) d = rs.normal(size=100) f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)sns.distplot(d, kde=False, color="b", ax=axes[0, 0]) sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1]) sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0]) sns.distplot(d, color="m", ax=axes[1, 1]) plt.show()

3  箱型圖 boxplot( )

import matplotlib.pyplot as plt import seaborn as sns df_iris = pd.read_csv('../input/iris.csv')sns.boxplot(x = df_iris['class'],y = df_iris['sepal width']) plt.show()

import matplotlib.pyplot as plt import seaborn as sns tips = pd.read_csv('../input/tips.csv') sns.set(style="ticks")                                     #設定主題sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")   #palette 調色盤 plt.show()

4  聯合分佈jointplot( )

tips = pd.read_csv('../input/tips.csv')   #右上角顯示相關係數sns.jointplot("total_bill", "tip", tips) plt.show()

tips = pd.read_csv('../input/tips.csv') sns.jointplot("total_bill", "tip", tips, kind='reg')      plt.show()

5  熱點圖heatmap( )

import matplotlib.pyplot as plt import seaborn as sns data = pd.read_csv("../input/car_crashes.csv") data = data.corr()sns.heatmap(data) plt.show()

6  配對圖pairplot( )

import matplotlib.pyplot as plt import seaborn as sns data = pd.read_csv("../input/iris.csv") sns.set()                        #使用預設配色sns.pairplot(data,hue="class")   #hue 選擇分類列 plt.show()

import seaborn as sns import matplotlib.pyplot as plt iris = pd.read_csv('../input/iris.csv') sns.pairplot(iris, vars=["sepal width", "sepal length"],hue='class',palette="husl")   plt.show()

7  FacetGrid( )

import seaborn as sns import matplotlib.pyplot as plt tips = pd.read_csv('../input/tips.csv')g = sns.FacetGrid(tips, col="time",  row="smoker") g = g.map(plt.hist, "total_bill",  color="r") plt.show()

轉自:https://blog.csdn.net/qq_34264472/article/details/53814653?utm_source=copy