day11-seaborn

代码,笔记,数据集文件放在gitee
点这里进入gitee
https://gitee.com/jiangxun07/python-data-analysis.git

0 Seaborn简介

Seaborn 是一个基于 matplotlib 且数据结构与 pandas 统一的统计图制作库。

这里列出了一些 seaborn 的功能:

Seaborn 框架旨在以数据可视化为中心来挖掘与理解数据。它提供的面向数据集制图函数主要是对行列索引和数组的操作,包含对整个数据集进行内部的语义映射与统计整合,以此生成富于信息的图表。

在终端安装: pip install seaborn

官方文档:http://seaborn.pydata.org/index.html
seaborn官方文档
中文文档: https://seaborn.apachecn.org/#/README
seaborn官方中文文档(需要加速)
博主可以无偿私发
官方数据集: https://github.com/mwaskom/seaborn-data

seaborn官方数据集

1 Season入门

# 先看一下matplotlab的绘图
import matplotlib.pyplot as plt
import numpy as np

# 创建一些数据
# np.random.RandomState(0)  是一个随机数种子
# 通过该随机数种子生成的随机序列<正态分布>, 可以保证数据相同
rng = np.random.RandomState(0)
x = np.linspace(0, 10, num=500)
y = np.cumsum(rng.randn(500, 6), 0)  # 500行数据, 6列,  按照行计算
# 用Matplotlib默认样式画图
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')

在这里插入图片描述

import seaborn as sns  # 导入
import matplotlib.pyplot as plt
import numpy as np

sns.set()  # 使用seaborn的默认配置, 可以直接对接matplotlib绘图

plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')

在这里插入图片描述

2 seaborn绘图

2.1 频次直方图

sns.set() 可以设置以下 Seaborn 参数:

  • context:设定绘图上下文,可以是 “notebook” (默认值)或 “paper”。
  • style:设定绘图风格,可以是 “white”(默认值)、“darkgrid”、“whitegrid”、“dark”、"white"等。
  • palette:设定调色板,可以是 “default”(默认值)、“cubehelix”、"Set1"等。
  • font:设定字体,可以是 “serif”(默认值)、“sans-serif”、“monospace”。
  • axlinestyle:设定轴线样式,可以是 “–”、"steps-mid"等。
  • savefig.dpi:保存图像的分辨率。
  • figure.figsize:设定图像大小。
  • print_grid:设定是否打印网格。
  • verbose:设定是否显示信息提示。
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd

# 取的是正态分布的值
data=np.random.multivariate_normal([0,0],[[5,2],[2,2]],size=2000)

data=pd.DataFrame(data,columns=['x','y'])
sns.set()

# 绘制平滑估计图<直方图的拟合线>
sns.kdeplot(data['x'])
sns.kdeplot(data['y'])

sns.jointplot(data=data,x='x',y='y',kind='kde')
#data 是你的数据集。
                        #x 和 y 是在数据集中你想要在联合图上表示的列的名字。
#kind 参数指定你想创建的联合图的类型。在这种情况下,你选择了 'kde',也就是核密度估计图。
# kind='hex' 六边形核密度图样式  kind='kde' 线条样式

在这里插入图片描述
在这里插入图片描述

# kind='hex' 六边形核密度图样式
sns.jointplot(data=data,x='x',y='y',kind='hex')

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# 取的是正太分布的值
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])

plt.hist(data['x'], density=True, alpha=0.5)
plt.hist(data['y'], density=True, alpha=0.1)
plt.legend('XY', ncol=2, loc='upper left')

plt.show()

在这里插入图片描述

2.2 矩阵图

sns.set()

iris=pd.read_csv('./data/iris.csv')
#sepal_length萼片长度
#sepal_width萼片宽度
#petal_length花瓣长度 
#petal_width花瓣宽度  
#species 种类
iris

在这里插入图片描述

# pairplot  pair 成双成对, 描述数据两联高枝剪的关系, 线性, 非线性关系, 相关关系等等
# hue='species' 指定分类的字段, 将每个数据划分不同颜色
sns.pairplot(iris, hue='species')

在这里插入图片描述

2.3 分面频次图

tips=pd.read_csv('./data/tips.csv')
tips

在这里插入图片描述

# 计算消费占总餐费的比重
tips['tip_proportion']=tips['tip']/tips['total_bill']*100

# margin_titles  显示字段标题
grid=sns.FacetGrid(data=tips,row='sex',col='time',margin_titles=True)

grid.map(plt.hist,'tip_proportion',bins=np.linspace(0,40,15))

在这里插入图片描述
为什么男人在晚上给的小费比中午多很多呢?

2.4 箱线图

tips

with sns.axes_style(style='ticks'):
    sns.catplot(data=tips,x='day',y='total_bill',hue='sex',kind='box')
    #kind: 绘图类型:violin 小提琴图   swarm 散点图 box 箱线图  

在这里插入图片描述

2.5 分类图

with sns.axes_style(style='ticks'):
    sns.catplot(data=tips,x='day',y='total_bill',hue='sex',kind='violin')

在这里插入图片描述

with sns.axes_style(style='ticks'):
    sns.catplot(data=tips,x='day',y='total_bill',hue='sex',kind='swarm')

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

2.6 联合分布图

依旧使用tips数据

sns.set()
sns.jointplot(data=tips,x='total_bill',y='tip',kind='hex')

在这里插入图片描述

sns.jointplot(data=tips, x='total_bill', y='tip', kind='reg')
# reg= 构建线性回归的拟合线条

在这里插入图片描述

  • pyecharts 侧重于显示数据, 有动态数据交互效果
  • matplotlib 绘制平面图
  • seaborn 画图研究数据

2.7 调色板

sns.color_palette()  # 默认的调色板
sns.palplot(sns.hls_palette(8, l=0.5, s=0.8))  # l 亮度  s 饱和度

在这里插入图片描述

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值