写在前面的准备工作
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
sns.set_style('darkgrid',{'font.sans-serif':['SimHei','Arial']})
import warnings #去除部分警告信息
warnings.filterwarnings('ignore')
sns.set() #返回seaborn的默认设置
柱状图
x = ['a','b','c','d']
y = [164, 56, 126, 56]
sns.barplot(x,y)
#条形图 orient表示倒置, saturation是颜色深度
sns.barplot(y,x,order = ['b','d','c','a'],orient='h',saturation=1)
tips = sns.load_dataset("tips")
sns.barplot(x = 'day', y = 'tip', data = tips)
#柱子的高是平均值,黑线是置信区间
sns.barplot(x = 'day', y = 'tip', data = tips, hue = 'sex', palette = 'Greens')
条形图
#条形图
sns.barplot(y = 'day',x = 'total_bill',data = tips)
箱线图
L = [3,5,1,4,8]
sns.boxplot( y = L)
sns.boxplot(x ='day',y = 'tip',data = tips)
sns.boxplot(x ='day',y = 'tip',data = tips,hue = 'sex')
小提琴图 是箱线图和密度图的结合
L = [3,2,0,1,1]
sns.violinplot(L)
L = [3,2,0,1,1]
sns.violinplot( y = L)
sns.violinplot(x = 'day',y = 'tip',hue = 'sex',data = tips)
sns.violinplot(x = 'day',y = 'tip',hue = 'sex',data = tips,split = True)
分类散点图----Strip(带状)和Swarm(蜂群)
sns.stripplot(x = 'day', y ='tip', data= tips)
sns.stripplot(x = 'day', y ='tip',hue = 'sex', data= tips)
sns.swarmplot(x = 'day', y ='tip', data= tips)
sns.swarmplot(x = 'day', y ='tip',hue = 'sex' ,data= tips)
分面网格分类图 (FacetGrid)
sns.catplot(x = 'day', y ='tip',hue = 'sex' ,col = 'time',kind = "violin",row = 'smoker',data= tips)
sns.catplot(x = 'day', y ='tip',hue = 'sex' ,col = 'time',kind = "violin",data= tips, split = True) #图形各一半的合并
sns.catplot()
sns.catplot(x = 'day', y ='tip',hue = 'sex' ,col = 'size',kind = "bar",data= tips)
sns.catplot(x = 'day', y ='tip',hue = 'sex' ,col = 'size',col_wrap = 3,kind = "bar",data= tips)
散点图
n = 1024
x = np.random.normal(0,1,n)
y = np.random.normal(0,1,n)
sns.scatterplot(x = x, y = y)
plt.title('绘制散点图',fontproperties = 'SimHei')
sns.scatterplot(x = 'total_bill', y = 'tip', hue = 'sex',data = tips)
plt.figure(dpi =150)
sns.scatterplot(x = 'total_bill', y = 'tip', hue = 'sex',style = 'time',size = 'size',data = tips)