Python-seaborn library data visualization {histogram, scatter plot, box plot, variable relationship diagram, heat map, bar graph}

Seaborn is a more advanced package based on matplotlib, which can make it easier to draw various exquisite charts that are convenient for analyzing data. This article mainly summarizes the six common types {histogram, scatterplot, boxplot, variable relationship diagram, heat map, bar graph}, and attaches code and picture display.

Table of contents

1. Histogram

Two, scatter plot

3. Boxplot

4. Variable relationship diagram

Five, heat map

6. Bar graph


The English letters corresponding to the painting colors are attached here to facilitate subsequent modification of the color configuration.

 

1. Histogram

First obtain the data, clean the data, and then visualize the data

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

#读入数据
df=pd.read_csv('C:/Users/27812/Desktop/1-Advertising.csv')
#数据清洗
#1.清除重复值
df.drop_duplicates(keep='first',inplace=True)
#2.清除缺失值
df.dropna(how='any',inplace=True)
all_cloumns=list(df)#获取所有列名
print(all_cloumns)
#['TV', 'radio', 'newspaper', 'sales']
#直方图可视化
fig1,axes=plt.subplots(2,2)
sns.distplot(df['TV'],ax=axes[0,0])#kde和hist默认保留
sns.kdeplot(df['radio'],ax=axes[0,1],color='darkviolet',shade=True)#shade代表是否填充
sns.distplot(df['newspaper'],ax=axes[1,0],color='g',kde=False)
sns.distplot(df['sales'],ax=axes[1,1],hist=False,color='r')
#保存图片
fig1.savefig('A.png',dpi=500)

 Two, scatter plot

#绘制散点图
fig2,axes=plt.subplots(2,2)#创建2^2的子图
sns.scatterplot(x=df['TV'],y=df['sales'],data=df,ax=axes[0,0])
sns.scatterplot(x=df['radio'],y=df['sales'],data=df,ax=axes[0,1],color='y')
sns.scatterplot(x=df['newspaper'],y=df['sales'],data=df,ax=axes[1,0],color='orangered')
sns.scatterplot(x=df['TV'],y=df['newspaper'],data=df,ax=axes[1,1],color='crimson')
#保存图片
fig2.savefig('B.png',dpi=500)

 3. Boxplot

#绘制箱线图
fig3,axes=plt.subplots(2,2)
sns.set_style('whitegrid')#设置白色网格背景
sns.boxplot(y=df['sales'],ax=axes[0,0])
sns.boxplot(data=df,ax=axes[0,1])
sns.set_style('darkgrid')#设置灰色网格背景
sns.boxplot(data=df,ax=axes[1,0],color='cyan')
sns.swarmplot(data=df,ax=axes[1,0],color='plum',linewidth=0.1)#swarmplot可以和boxplot画在一起
sns.boxplot(data=df,ax=axes[1,1],color='cyan')
#保存图片
fig3.savefig('C.png',dpi=500)

 4. Variable relationship diagram

#绘制变量关系图
#1.两两变量
fig_1=sns.jointplot(x=df['TV'],y=df['sales'],data=df,kind='reg')
fig_2=sns.jointplot(x=df['newspaper'],y=df['sales'],data=df,color='r',kind='kde',space=0)
fig_3=sns.jointplot(x=df['radio'],y=df['sales'],data=df,kind='hex',color='violet')
fig_1.savefig('D1.png',dpi=500)
fig_2.savefig('D2.png',dpi=500)
fig_3.savefig('D3.png',dpi=500)

#2.多变量
p1=sns.pairplot(df)#所有变量两两画图
p2=sns.pairplot(df,kind='kde')
p3=sns.pairplot(df,vars=['TV','sales'],kind='kde')#vars选择变量
p1.savefig('E1.png',dpi=500)
p2.savefig('E2.png',dpi=500)
p3.savefig('E3.png',dpi=500)

 

  

 Five, heat map

#绘制热力图
data_corr=df.corr().abs()
p4=sns.heatmap(data_corr,annot=True)
p4.savefig('F.png',dpi=500) 

 6. Bar graph

#绘制条形图
fig5,axes=plt.subplots(2,2)
sns.barplot(data=df,ax=axes[0,0])
sns.barplot(data=df,ax=axes[0,1],ci=0)#ci取消置信线
sns.countplot(data=df,ax=axes[1,0])
sns.barplot(data=df,ax=axes[1,1])
fig5.savefig('G.png',dpi=500) 

 

Guess you like

Origin blog.csdn.net/weixin_57501965/article/details/126625267