python可视化进阶---seaborn1.4 分布数据可视化 - 散点图 jointplot() / pairplot()

简介: 一、分布数据可视化 - 散点图jointplot() / pairplot()加载模块

一、分布数据可视化 - 散点图

jointplot() / pairplot()

加载模块

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as sci

1、综合散点图 - jointplot()

1.1散点图 + 分布图

示例1:

#创建数据
rs = np.random.RandomState(2)
df = pd.DataFrame(rs.randn(200,2), columns = ['A','B'])
sns.jointplot(x=df['A'], y=df['B'], #设置xy轴,显示columns名称
              data = df,  #设置数据
              color = 'b', #设置颜色
              s = 50, edgecolor = 'w', linewidth = 1,#设置散点大小、边缘颜色及宽度(只针对scatter)
              stat_func=sci.pearsonr,
              kind = 'scatter',#设置类型:'scatter','reg','resid','kde','hex'
              #stat_func=<function pearsonr>,
              space = 0.1, #设置散点图和布局图的间距
              size = 8, #图表大小(自动调整为正方形))
              ratio = 5, #散点图与布局图高度比,整型
              marginal_kws = dict(bins=15, rug =True), #设置柱状图箱数,是否设置rug
              )

20180911141851160.png

图1.散点分布图

注意:如果不显示r值(pearsonr),可以在参数中添加stat_func=sci.pearsonr,有就不用添加了

示例2:六边形图

sns.jointplot(x=df['A'], y=df['B'], #设置xy轴,显示columns名称
              data = df,  #设置数据
              color = 'b', #设置颜色
              #s = 50, edgecolor = 'w', linewidth = 1,#设置散点大小、边缘颜色及宽度(只针对scatter)
              stat_func=sci.pearsonr,
              kind = 'hex',#设置类型:'scatter','reg','resid','kde','hex'
              space = 0.1, #设置散点图和布局图的间距
              size = 8, #图表大小(自动调整为正方形))
              ratio = 5, #散点图与布局图高度比,整型
             # marginal_kws = dict(bins=15, rug =True) #设置柱状图箱数,是否设置rug
              )

20180911142105665.png

六边形图也可以叫做蜂巢图

示例3:六边形图

#创建数据
df = pd.DataFrame(rs.randn(500,2), columns = ['A', 'B'])
with sns.axes_style('white'):
    sns.jointplot(x=df['A'], y=df['B'], data = df, kind = 'hex',
                  color = 'k',stat_func=sci.pearsonr,
                  marginal_kws = dict(bins = 20))

201809111423143.png

示例4: 密度图

#创建数据
rs = np.random.RandomState(15)
df = pd.DataFrame(rs.randn(300,2), columns = ['A', 'B'])
#创建密度图
g = sns.jointplot(x = df['A'], y = df['B'], data = df,
                  kind = 'kde', color = 'k', stat_func= sci.pearsonr,
                  shade_lowest = False)
#添加散点图
g.plot_joint(plt.scatter, c = 'w', s = 30, linewidth = 1, marker='+')

2018091114243770.png

1.2可拆分绘制的散点图

#plot_joint() + ax_marg_x.hist() + ax_marg_y.hist()

示例1:拆分图

#设置风格
sns.set_style('white')
#导入数据
tips = sns.load_dataset('tips')
print(tips.head())
#创建一个绘图表格区域,设置好x,y对应数据
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g.plot_joint(plt.scatter, color = 'm', edgecolor = 'white') #设置框内图表,scatter
g.ax_marg_x.hist(tips['total_bill'], color='b', alpha = .6,
                 bins = np.arange(0,60,3))                  #设置x轴为直方图,注意bins是数组
g.ax_marg_y.hist(tips['tip'], color = 'r', alpha = .6,
                 orientation = 'horizontal',
                 bins = np.arange(0,12,1)) #设置x轴直方图,注意需要orientation参数
from scipy import stats
g.annotate(stats.pearsonr)
#设置标注,可以为pearsonar, spearmanr
plt.grid(linestyle = '--')

20180911142712355.png

示例2:两个条形图在一个函数里进行设置

#创建一个绘图表格区域,设置好x,y对应数据
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g = g.plot_joint(plt.scatter, color = 'g', s = 40, edgecolor = 'white') #绘制散点图
plt.grid(linestyle = '--')
g.plot_marginals(sns.distplot, kde = True, color = 'g')   #绘制x,y直方图

20180911142853463.png

示例3:kde - 密度图

#创建一个绘图表格区域,设置好x,y对应数据
g = sns.JointGrid(x = 'total_bill', y = 'tip', data = tips)
g = g.plot_joint(sns.kdeplot, cmap = 'Reds_r')     #绘制密度图
plt.grid(linestyle = '--')
g.plot_marginals(sns.kdeplot, shade = True, color = 'r') #绘制x,y轴密度图

20180911142959191.png

2.矩阵散点图 - pairplot()

示例1:普通矩阵图示意

#设置风格
sns.set_style('white')
#读取数据
iris = sns.load_dataset('iris')
print(iris.head())
sns.pairplot(iris,
             kind = 'scatter', #散点图/回归分布图{'scatter', 'reg'})
             diag_kind = 'hist', #直方图/密度图{'hist', 'kde'}
             hue = 'species',   #按照某一字段进行分类
             palette = 'husl',  #设置调色板
             markers = ['o', 's', 'D'], #设置不同系列的点样式(这里根据参考分类个数)
             size = 2  #图标大小
             )

20180911143255150.png

示例2:只提取局部变量进行对比

g = sns.pairplot(iris, vars = ['sepal_width', 'sepal_length'],
             kind = 'reg', diag_kind = 'kde',
             hue = 'species', palette = 'husl')

20180911143404114.png

示例3:其它参数设置

#其它参数设置
sns.pairplot(iris, diag_kind = 'kde', markers = '+',
             plot_kws = dict(s = 50, edgecolor = 'b', linewidth = 1),
             #设置点样式
             diag_kws = dict(shade = True)
             )#设置密度图样式

20180911143704165.png

示例4:可拆分绘制的散点图

# map_diag() + map_offdiag()
g = sns.PairGrid(iris, hue= 'species', palette = 'hls',
                 vars = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
                #可筛选创建一个绘图表格区域,设置好x,y对应的数据,按照species分类
#对角线图表,plt.hist/sns.kdeplot
g.map_diag(plt.hist,
           histtype = 'step', #可选:'bar','barstacked', 'step', 'stepfilled'
           linewidth = 1, edgecolor = 'w')
#其它图表:plt.scatter/plt.bar...
g.map_offdiag(plt.scatter, edgecolor = 'w', s = 40, linewidth = 1)
#设置点颜色、大小、描边宽度
g.add_legend() #添加图例()

20180911143951682.png

示例5:上三角和下三角#map_diag() + map_lower() + map_upper()

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

20180911144129387.png

相关文章
|
7月前
|
数据可视化 关系型数据库 MySQL
基于python大数据的的海洋气象数据可视化平台
针对海洋气象数据量大、维度多的挑战,设计基于ECharts的可视化平台,结合Python、Django与MySQL,实现数据高效展示与交互分析,提升科研与决策效率。
|
8月前
|
机器学习/深度学习 数据可视化 搜索推荐
基于python的汽车数据可视化、推荐及预测系统
本研究围绕汽车数据可视化、推荐及预测系统展开,结合大数据与人工智能技术,旨在提升用户体验与市场竞争力。内容涵盖研究背景、意义、相关技术如 Python、ECharts、协同过滤及随机森林回归等,探讨如何挖掘汽车数据价值,实现个性化推荐与智能预测,为汽车行业智能化发展提供支持。
|
7月前
|
数据采集 Web App开发 数据可视化
Python零基础爬取东方财富网股票行情数据指南
东方财富网数据稳定、反爬宽松,适合爬虫入门。本文详解使用Python抓取股票行情数据,涵盖请求发送、HTML解析、动态加载处理、代理IP切换及数据可视化,助你快速掌握金融数据爬取技能。
4915 1
|
8月前
|
数据采集 Web App开发 自然语言处理
新闻热点一目了然:Python爬虫数据可视化
新闻热点一目了然:Python爬虫数据可视化
|
7月前
|
Java 数据挖掘 数据处理
(Pandas)Python做数据处理必选框架之一!(一):介绍Pandas中的两个数据结构;刨析Series:如何访问数据;数据去重、取众数、总和、标准差、方差、平均值等;判断缺失值、获取索引...
Pandas 是一个开源的数据分析和数据处理库,它是基于 Python 编程语言的。 Pandas 提供了易于使用的数据结构和数据分析工具,特别适用于处理结构化数据,如表格型数据(类似于Excel表格)。 Pandas 是数据科学和分析领域中常用的工具之一,它使得用户能够轻松地从各种数据源中导入数据,并对数据进行高效的操作和分析。 Pandas 主要引入了两种新的数据结构:Series 和 DataFrame。
691 0
|
7月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
7月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
8月前
|
存储 监控 API
Python实战:跨平台电商数据聚合系统的技术实现
本文介绍如何通过标准化API调用协议,实现淘宝、京东、拼多多等电商平台的商品数据自动化采集、清洗与存储。内容涵盖技术架构设计、Python代码示例及高阶应用(如价格监控系统),提供可直接落地的技术方案,帮助开发者解决多平台数据同步难题。
|
8月前
|
数据采集 关系型数据库 MySQL
python爬取数据存入数据库
Python爬虫结合Scrapy与SQLAlchemy,实现高效数据采集并存入MySQL/PostgreSQL/SQLite。通过ORM映射、连接池优化与批量提交,支持百万级数据高速写入,具备良好的可扩展性与稳定性。
|
8月前
|
JSON API 数据安全/隐私保护
Python采集淘宝评论API接口及JSON数据返回全流程指南
Python采集淘宝评论API接口及JSON数据返回全流程指南

推荐镜像

更多