Python数据可视化—seaborn简介和实例

转载:https://blog.csdn.net/qq_34264472/article/details/53814653

Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图。这里实例采用的数据集都是seaborn提供的几个经典数据集,dataset文件可见于Github。本博客只总结了一些,方便博主自己查询,详细介绍可以看seaborn官方APIexample gallery,官方文档还是写的很好的。

1  set_style( )  set( )

set_style( )是用来设置主题的,Seaborn有五个预设好的主题: darkgrid , whitegrid , dark , white ,和 ticks  默认: darkgrid

[python]  view plain  copy
  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()  



set( )通过设置参数可以用来设置背景,调色板等,更加常用。

[python]  view plain  copy
  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()  

2  distplot( )  kdeplot( )

distplot( )为hist加强版,kdeplot( )为密度曲线图 
[python]  view plain  copy
  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]  view plain  copy
  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(22, figsize=(77), sharex=True)  
  8. sns.distplot(d, kde=False, color="b", ax=axes[00])  
  9. sns.distplot(d, hist=False, rug=True, color="r", ax=axes[01])  
  10. sns.distplot(d, hist=False, color="g", kde_kws={"shade"True}, ax=axes[10])  
  11. sns.distplot(d, color="m", ax=axes[11])  
  12. plt.show()  

3  箱型图 boxplot( )

[python]  view plain  copy
  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]  view plain  copy
  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()  

4  联合分布jointplot( )

[python]  view plain  copy
  1. tips = pd.read_csv('../input/tips.csv')   #右上角显示相关系数  
  2. sns.jointplot("total_bill""tip", tips)  
  3. plt.show()  

[python]  view plain  copy
  1. tips = pd.read_csv('../input/tips.csv')  
  2. sns.jointplot("total_bill""tip", tips, kind='reg')       
  3. plt.show()  


5  热点图heatmap( )

[python]  view plain  copy
  1. import matplotlib.pyplot as plt  
  2. import seaborn as sns  
  3. data = pd.read_csv("../input/car_crashes.csv")  
  4. data = data.corr()  
  5. sns.heatmap(data)  
  6. plt.show()  


6  pairplot( )

[python]  view plain  copy
  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]  view plain  copy
  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()  

7  FacetGrid( )

[python]  view plain  copy
  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()  

参考链接:

猜你喜欢

转载自blog.csdn.net/m0_37870649/article/details/80555782