【python】seaborn可视化

https://blog.csdn.net/cymy001/article/details/78418432
有待补充
Python 中,数据可视化一般是通过较底层的 Matplotlib 库和较高层的 Seaborn库实现的,本文主要介绍一些常用的图的绘制方法。

9是多分类面板,能够绘制多个图,并且可通过kind参数绘制出更多的图。

1、一维数据

import seaborn as sns
import pandas as pd
import numpy as np
from scipy import stats,integrate
import matplotlib.pyplot as plt
sns.set(color_codes = True)
np.random.seed(sum(map(ord,"distributions")))
x = np.random.normal(size=100)
sns.distplot(x,kde=False)

在这里插入图片描述

sns.distplot(x,bins=20,kde=False) # 切分为20小块

在这里插入图片描述

x = np.random.gamma(6,size=200)
sns.distplot(x,kde=False,fit=stats.gamma) 

在这里插入图片描述

2、二维数据的分布

mean,cov = [0,1],[(1,0.5),(0.5,1)]
data = np.random.multivariate_normal(mean,cov,200) # 生成指定的均值和协方差
df = pd.DataFrame(data,columns=["x","y"])
sns.jointplot(x="x",y = 'y',data = df)
观测两个变量的分布关系,而且还输出皮尔森指数

在这里插入图片描述

如果数据很多的话,用hex图能更好的显示出分布
x,y = np.random.multivariate_normal(mean,cov,1000).T
with sns.axes_style('white'):
    sns.jointplot(x=x,y = y,kind='hex',color='k')
可以看出结果分布较多是在中间位置,通过颜色深浅看出分布

在这里插入图片描述

3、回归

np.random.seed(sum(map(ord,"regression")))
tips = sns.load_dataset("tips")
tips.head()

在这里插入图片描述

regplot() 和 lmplot()都可以绘制回归关系
sns.regplot(x="total_bill",y="tip",data=tips)
sns.lmplot(x="total_bill",y="tip",data=tips)
目前看区别不大,但是后者功能更多,规范也多。

在这里插入图片描述

用离散数据作回归
sns.regplot(x="size",y="tip",data=tips)
看出显示并不好

在这里插入图片描述

原始数据集是类别值,都是整数,现在加了点浮动(参数x_jitter)
使得离散值,变得像连续值
sns.regplot(x="size",y="tip",data=tips,x_jitter=0.05)

在这里插入图片描述

sns.regplot(x="size",y="tip",data=tips,x_jitter=0.5)

在这里插入图片描述

4、对类别值的可视化展示

titanic = sns.load_dataset("titanic")
iris = sns.load_dataset("iris")
sns.stripplot(x='day',y="total_bill",data=tips)
像散点图,但是不怎么看得出来,因为都重叠了,影响观察数据量

在这里插入图片描述

参数 jitter=True相当于往左右偏移,使得不重叠
sns.stripplot(x='day',y="total_bill",data=tips,jitter=True)

在这里插入图片描述

sns.swarmplot(x='day',y="total_bill",data=tips)
画出来像一棵树

在这里插入图片描述

三个参数,加入更多的条件hue
sns.swarmplot(x='day',y="total_bill",hue='sex',data=tips)

在这里插入图片描述

sns.swarmplot(y='day',x="total_bill",hue='time',data=tips)

在这里插入图片描述

5、盒图

可观察离群点,竖着画对x,y类型没要求
sns.boxplot(x='day',y='total_bill',hue="time",data=tips)

在这里插入图片描述

可以横着画,但是x,y必须是数值型
sns.boxplot(y='size',x='total_bill',data=tips,orient="h") 

在这里插入图片描述

6、小提琴图

sns.violinplot(x='day',y='total_bill',hue="time",data=tips)

在这里插入图片描述

sns.violinplot(x='day',y='total_bill',hue="sex",data=tips,split=True)

在这里插入图片描述

还可以合体
sns.violinplot(x='day',y='total_bill',data=tips,inner=None)
sns.swarmplot(x='day',y="total_bill",data=tips,color='w',alpha=0.5)

在这里插入图片描述

7、分类数据

通过柱状图观察一等、二等、三等舱的分布

sns.barplot(x='sex',y='survived',hue='class',data=titanic)

在这里插入图片描述

8、折线图

sns.pointplot(x='sex',y='survived',hue='class',data=titanic)

在这里插入图片描述

sns.pointplot(x='class',y='survived',hue='sex',data=titanic,
              palette={"male":"g","female":"m"},
              maker=["s","o"],linestyles=["-","--"])

在这里插入图片描述

factorplot()

sns.factorplot(x='day',y='total_bill',hue='smoker',data=tips)

在这里插入图片描述

sns.factorplot(x='day',y='total_bill',hue='smoker',data=tips,kind='bar')

在这里插入图片描述

可绘制多个图

sns.factorplot(x='day',y='total_bill',hue='smoker',
               col='time',data=tips,kind='swarm')

在这里插入图片描述

sns.factorplot(x='time',y='total_bill',hue='smoker',
               col='day',data=tips,kind='box',size=4,aspect=0.5)
参数kind:point默认,bar柱形图,count频次,box箱体,violin提琴,strip散点,swarm分散点

在这里插入图片描述

FacetGrid()

准备:
import seaborn as sns
import pandas as pd
import numpy as np
from scipy import stats,integrate
import matplotlib.pyplot as plt
sns.set(color_codes = True)
np.random.seed(sum(map(ord,"distributions")))
x = np.random.normal(size=100)
sns.distplot(x,kde=False)
tips = sns.load_dataset("tips")
titanic = sns.load_dataset("titanic")
iris = sns.load_dataset("iris")

1、操作流程

  • 先sns.FacetGrid画出轮廓
  • 然后用map填充内容
g = sns.FacetGrid(tips,col='time')
g.map(plt.hist,"tip")

在这里插入图片描述

g = sns.FacetGrid(tips,col='sex',hue='smoker')
g.map(plt.scatter,"total_bill","tip",alpha=0.7)
g.add_legend()  # 加注释

在这里插入图片描述

g = sns.FacetGrid(tips,col='time',row='smoker',margin_titles=True)
g.map(sns.regplot,"size","total_bill",color="0.5",fit_reg=True,x_jitter=0.1)

在这里插入图片描述

g = sns.FacetGrid(tips,col='day',size=4,aspect=0.5)
g.map(sns.barplot,"sex","total_bill")

在这里插入图片描述

from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
ordered_days = Categorical(['Thur',"Fri","Sat","Sun"])
# FacetGrid传数据需要是pandas格式
g = sns.FacetGrid(tips,row='day',row_order=ordered_days,size=1.7,aspect=4)
g.map(sns.boxplot,"total_bill")

在这里插入图片描述

pal = dict(Lunch="seagreen",Dinner="red")# 两个变量自己指定颜色
g=sns.FacetGrid(tips,hue="time",palette=pal,size=5)
g.map(plt.scatter,"total_bill","tip",s=50, alpha=0.7, linewidth=0.5,edgecolor="white")
g.add_legend()

在这里插入图片描述

g=sns.FacetGrid(tips,hue="time",palette=pal,size=5,hue_kws={"marker":["^","v"]})
g.map(plt.scatter,"total_bill","tip",s=50, alpha=0.7, linewidth=0.5,edgecolor="white")
g.add_legend()

在这里插入图片描述

with sns.axes_style("white"):
    g = sns.FacetGrid(tips,row="sex",col="smoker",margin_titles=True,size=3)
g.map(plt.scatter,"total_bill","tip",color="#334488",edgecolor="white",lw=5)
g.set_axis_labels("Total bill (US Dollars)","Tip")
g.set(xticks=[10,30,50],yticks=[2,6,10])# 轴的取值范围
g.fig.subplots_adjust(wspace=0.02,hspace=0.02)# 子图和子图之间的间隔

在这里插入图片描述

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map(plt.scatter)

在这里插入图片描述

g = sns.PairGrid(iris)
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)

在这里插入图片描述

g = sns.PairGrid(iris,hue="species")
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()

在这里插入图片描述

# 指定想画出的特征
feature_=["sepal_length","sepal_width"]
g = sns.PairGrid(iris,vars=feature_, hue="species")
g.map(plt.scatter)

在这里插入图片描述

g = sns.PairGrid(tips, hue='size',palette="GnBu_d")#渐变色
g.map(plt.scatter,s=50,edgecolor="white")
g.add_legend()

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值