Python数据可视化-matplotlib and seaborn

作者:vinyyu
声明:版权所有,转载请注明出处,谢谢。

鸢尾花iris.csv文件

Python数据可视化-matplotlib and seaborn_第1张图片

numpy, matplotlib, seaborn, pandas

#准备好需要的库
import numpy as np
import matplotlib.pyplot as plt  
import seaborn as sns 
import pandas as pd

plt.rcParams['font.sans-serif'] = ['SimHei']  # 解决中文显示问题-设置字体为黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

1 画线,set_style( ) set( )

sns.set_style("whitegrid")  
plt.plot(np.arange(10))  
plt.show() 

Python数据可视化-matplotlib and seaborn_第2张图片

#set( )通过设置参数可以用来设置背景,调色板等,更加常用。
sns.set(style="white", palette="muted", color_codes=True)     #set( )设置主题,调色板更常用  
plt.plot(np.arange(10))  
plt.show() 

Python数据可视化-matplotlib and seaborn_第3张图片

2 distplot( )直方图加强版,kdeplot( )密度曲线图

plt.rcParams['font.sans-serif'] = ['SimHei']  # 解决中文显示问题-设置字体为黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
df_iris = pd.read_csv('d:/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  阴影   
axes[0].set_title("加强直方图")
axes[1].set_title("密度曲线图")
plt.show()

Python数据可视化-matplotlib and seaborn_第4张图片

sns.set( palette="muted", color_codes=True)  
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)  
sns.distplot(df_iris['petal length'], kde=False, color="b", ax=axes[0, 0])  
sns.distplot(df_iris['petal length'], hist=False, rug=True, color="r", ax=axes[0, 1])  
sns.distplot(df_iris['petal length'], hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])  
sns.distplot(df_iris['petal length'], color="m", ax=axes[1, 1])  
plt.show()  

Python数据可视化-matplotlib and seaborn_第5张图片

3 箱线图 boxplot( )

df_iris = pd.read_csv('d:\iris.csv')  
sns.boxplot(x = df_iris['class label'],y = df_iris['sepal width'])  
plt.title("箱线图")
plt.show()  

Python数据可视化-matplotlib and seaborn_第6张图片

df_iris = pd.read_csv('d:\iris.csv')  
#将iris数据根据sepal width分成1和2两类
df_iris['sepal class']=round(df_iris['sepal width']/2,0)
#fliersize=1将异常点虚化,showmeans=True显示平均值,order表示按x轴显示进行排序
sns.boxplot(data=df_iris,x='sepal class',y='sepal width',\
            hue='class label',hue_order=['Setosa','Versicolor','Virginica'],
            showmeans=True,fliersize=1)
plt.legend(loc='upper right')#图示靠右显示
plt.rc('font',family='SimHei',size=13)
plt.title("箱线图")
plt.show

Python数据可视化-matplotlib and seaborn_第7张图片

4 热图 heatmap( )

df_iris = pd.read_csv('d:\iris.csv') 
#求iris4个参数的相关性,越接近1表示正相关,越接近-1表示负相关,越接近0表示不相关
df_cor=df_iris.corr()
f, ax = plt.subplots(figsize=(10, 7))
#plt.xticks(rotation='90')
sns.heatmap(df_cor, square=True, linewidths=.5, annot=True)
plt.title("热图")
plt.show()

Python数据可视化-matplotlib and seaborn_第8张图片

5 散点图 scatter( )

df_iris = pd.read_csv('d:\iris.csv') 
plt.subplots(figsize=(10, 7))
plt.scatter(data=df_iris, x='sepal length', y='sepal width', c='r')
plt.xlim(2,10)
plt.ylim(1,5)
plt.title("散点图")
plt.show()

Python数据可视化-matplotlib and seaborn_第9张图片

6 矩阵散点图 pairplot( )

df_iris = pd.read_csv('d:\iris.csv') 
sns.pairplot(df_iris, vars=["sepal width", "sepal length"],hue='class label',palette="husl")    
plt.title("pairplot")
plt.show() 

Python数据可视化-matplotlib and seaborn_第10张图片

7 柱状图 bar()

df_iris = pd.read_csv('d:\iris.csv') 
#将iris数据根据sepal length分成4,5,6,7共4类
df_iris['sepal class']=round(df_iris['sepal length'])
#对分类数据统计个数
df_bar=pd.DataFrame({'x':pd.Series([4,5,6,7]),'y':pd.Series([0,0,0,0])})
for  i in df_bar['x']:
     df_bar.y[df_bar.x==i]=len(df_iris[df_iris['sepal class']==i])
plt.bar(x='x', height='y', data=df_bar)
plt.title("柱状图")
plt.show() 
#这里用统计个数和柱状图做了类似直方图的功能图像

Python数据可视化-matplotlib and seaborn_第11张图片

8 饼图 pie()

df_iris = pd.read_csv('d:\iris.csv') 
#将iris数据根据sepal length分成4,5,6,7共4类
df_iris['sepal class']=round(df_iris['sepal length'])
#对分类数据统计个数
df_bar=pd.DataFrame({'x':pd.Series([4,5,6,7]),'y':pd.Series([0,0,0,0])})
for  i in df_bar['x']:
     df_bar.y[df_bar.x==i]=len(df_iris[df_iris['sepal class']==i])
plt.pie(data=df_bar,x='y',labels='x',autopct='%1.2f%%') #画饼图(数据,数据对应的标签,百分数保留两位小数点) 
plt.title('饼图')
plt.show()

Python数据可视化-matplotlib and seaborn_第12张图片

9 联合分布图 jointplot( )

df_iris = pd.read_csv('d:\iris.csv')  
sns.jointplot("sepal length", "sepal width", df_iris, kind='reg')  
plt.title("联合分布图")
plt.show() 

Python数据可视化-matplotlib and seaborn_第13张图片

10 联合密度图

g = sns.JointGrid("sepal length", "sepal width", df_iris)
g = g.plot_joint(sns.kdeplot, cmap = 'Reds_r')     
#绘制密度图
plt.grid(linestyle = '--')
g.plot_marginals(sns.kdeplot, shade = True, color = 'r')

Python数据可视化-matplotlib and seaborn_第14张图片

11 多种联合图

g = sns.PairGrid(df_iris)
g.map_diag(sns.kdeplot, lw=3) #设置对角线图表
g.map_upper(plt.scatter, color = 'r')  #设置对角线上端图表
g.map_lower(sns.kdeplot, cmap='Blues_d') #设置对角线下端图表

Python数据可视化-matplotlib and seaborn_第15张图片

你可能感兴趣的:(深度学习)