天天看點

Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

轉載位址https://www.cnblogs.com/gczr/p/6767175.html

Python資料可視化-seaborn

Seaborn其實是在matplotlib的基礎上進行了更進階的API封裝,進而使得作圖更加容易,在大多數情況下使用seaborn就能做出很具有吸引力的圖。這裡執行個體采用的資料集都是seaborn提供的幾個經典資料集,dataset檔案可見于Github。本部落格隻總結了一些,友善部落客自己查詢,詳細介紹可以看seaborn官方API和example gallery,官方文檔還是寫的很好的。

1  set_style( )  set( )

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

  1. import matplotlib.pyplot as plt  
  2. import seaborn as sns  
  3. sns.set_style("whitegrid")  
  4. plt.plot(np.arange(10))  
  5. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

    set( )通過設定參數可以用來設定背景,調色闆等,更加常用。

  1. import seaborn as sns  
  2. import matplotlib.pyplot as plt  
  3. sns.set(style="white", palette="muted", color_codes=True)     #set( )設定主題,調色闆更常用  
  4. plt.plot(np.arange(10))  
  5. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

2  distplot( )  kdeplot( )

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

  1. import matplotlib.pyplot as plt  
  2. import seaborn as sns  
  3. df_iris = pd.read_csv('../input/iris.csv')  
  4. fig, axes = plt.subplots(1,2)  
  5. sns.distplot(df_iris['petal length'], ax = axes[0], kde = True, rug = True)        # kde 密度曲線  rug 邊際毛毯  
  6. sns.kdeplot(df_iris['petal length'], ax = axes[1], shade=True)                     # shade  陰影                         
  7. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn
  1. import numpy as np  
  2. import seaborn as sns  
  3. import matplotlib.pyplot as plt  
  4. sns.set( palette="muted", color_codes=True)  
  5. rs = np.random.RandomState(10)  
  6. d = rs.normal(size=100)  
  7. f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)  
  8. sns.distplot(d, kde=False, color="b", ax=axes[0, 0])  
  9. sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])  
  10. sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])  
  11. sns.distplot(d, color="m", ax=axes[1, 1])  
  12. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

3  箱型圖 boxplot( )

  1. import matplotlib.pyplot as plt  
  2. import seaborn as sns  
  3. df_iris = pd.read_csv('../input/iris.csv')  
  4. sns.boxplot(x = df_iris['class'],y = df_iris['sepal width'])  
  5. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn
  1. import matplotlib.pyplot as plt  
  2. import seaborn as sns  
  3. tips = pd.read_csv('../input/tips.csv')  
  4. sns.set(style="ticks")                                     #設定主題  
  5. sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")   #palette 調色闆  
  6. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

4  聯合分布jointplot( )

  1. tips = pd.read_csv('../input/tips.csv')   #右上角顯示相關系數  
  2. sns.jointplot("total_bill", "tip", tips)  
  3. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn
  1. tips = pd.read_csv('../input/tips.csv')  
  2. sns.jointplot("total_bill", "tip", tips, kind='reg')       
  3. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

5  熱點圖heatmap( )

 internal_chars = ['full_sq', 'life_sq', 'floor', 'max_floor', 'build_year', 'num_room', 'kitch_sq', 'state', 'price_doc']

corrmat = train[internal_chars].corr()

f, ax = plt.subplots(figsize=(10, 7))

plt.xticks(rotation='90')

sns.heatmap(corrmat, square=True, linewidths=.5, annot=True)

plt.show()

Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

  6  散點圖scatter( ) f, ax = plt.subplots(figsize=(10, 7))

plt.scatter(x=train['full_sq'], y=train['price_doc'], c='r')

plt.xlim(0,500)

plt.show()

Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

7.pointplot畫出變量間的關系

grouped_df = train_df.groupby('floor')['price_doc'].aggregate(np.median).reset_index()

plt.figure(figsize=(12,8))

sns.pointplot(grouped_df.floor.values, grouped_df.price_doc.values, alpha=0.8, color=color[2])

plt.ylabel('Median Price', fontsize=12)

plt.xlabel('Floor number', fontsize=12)

plt.xticks(rotation='vertical') plt.show()

Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

8 pairplot( )

  1. import matplotlib.pyplot as plt  
  2. import seaborn as sns  
  3. data = pd.read_csv("../input/iris.csv")  
  4. sns.set()                        #使用預設配色  
  5. sns.pairplot(data,hue="class")   #hue 選擇分類列  
  6. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn
  1. import seaborn as sns  
  2. import matplotlib.pyplot as plt  
  3. iris = pd.read_csv('../input/iris.csv')  
  4. sns.pairplot(iris, vars=["sepal width", "sepal length"],hue='class',palette="husl")    
  5. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

9  FacetGrid( )

  1. import seaborn as sns  
  2. import matplotlib.pyplot as plt  
  3. tips = pd.read_csv('../input/tips.csv')  
  4. g = sns.FacetGrid(tips, col="time",  row="smoker")  
  5. g = g.map(plt.hist, "total_bill",  color="r")  
  6. plt.show()  
Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

10  barplot( )

f, ax=plt.subplots(figsize=(12,20))

#orient='h'表示是水準展示的,alpha表示顔色的深淺程度

sns.barplot(y=group_df.sub_area.values, x=group_df.price_doc.values,orient='h', alpha=0.8, color='red')

#設定y軸、X軸的坐标名字與字型大小

plt.ylabel('price_doc', fontsize=16)

plt.xlabel('sub_area', fontsize=16)

#設定X軸的各列下标字型是水準的

plt.xticks(rotation='horizontal')

#設定Y軸下标的字型大小

plt.yticks(fontsize=15)

plt.show()

注:如果orient='v'表示成豎直顯示的話,一定要記得y=group_df.sub_area.values, x=group_df.price_doc.values調換一下坐标軸,否則報錯

Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

11.bar圖

import matplotlib.pyplot as plt

import numpy as np

plt.rc('font', family='SimHei', size=13)

num = np.array([13325, 9403, 9227, 8651])

ratio = np.array([0.75, 0.76, 0.72, 0.75])

men = num * ratio

women = num * (1-ratio)

x = ['聊天','支付','團購\n優惠券','線上視訊']

width = 0.5

idx = np.arange(len(x))

plt.bar(idx, men, width, color='red', label='男性使用者')

plt.bar(idx, women, width, bottom=men, color='yellow', label='女性使用者')  #這一塊可是設定bottom,top,如果是水準放置的,可以設定right或者left。

plt.xlabel('應用類别')

plt.ylabel('男女分布')

plt.xticks(idx+width/2, x, rotation=40)

plt.legend()

plt.show()

Python資料可視化-seaborn Seaborn 轉載位址https://www.cnblogs.com/gczr/p/6767175.html Python資料可視化-seaborn

來源:http://blog.csdn.net/qq_34264472/article/details/53814653

 也可以參考:http://seaborn.pydata.org/tutorial/distributions.html

知乎專欄關于seaborn的:https://zhuanlan.zhihu.com/p/27570774