seaborn从入门到精通03-绘图功能实现05-构建结构化的网格绘图

简介: seaborn从入门到精通03-绘图功能实现05-构建结构化的网格绘图

1a9bed745ac14360a656b89ca93191cf.jpg


FacetGrid


导入库与查看tips和diamonds 数据


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as  mpl
import seaborn as sns
sns.set_theme(style="darkgrid")
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
tips = sns.load_dataset("tips",cache=True,data_home=r"./seaborn-data")
tips.head()


3f470b79f5c718980721b668e3327666_984ec1c1b7fb4aedbca13f8f772a5cf8.png

diamonds = sns.load_dataset("diamonds",cache=True,data_home=r"./seaborn-data")
print(diamonds.head())

73bdaa66f2130b88751ac065129d13be_422cad48f6264e68869cb8338785a3fa.png

anscombe = sns.load_dataset("anscombe",cache=True,data_home=r"./seaborn-data")
print(anscombe.head())

4bbf3bd838e3262bb4e765f78d239f83_862939db24dd4fd2a0080e97d5e63d61.png

penguins = sns.load_dataset("penguins",cache=True,data_home=r"./seaborn-data")
print(penguins.head())

606271007ca6f923d23e6690892ba7c0_97f7b43330ff4957b2def059a562b455.png

iris = sns.load_dataset("iris",cache=True,data_home=r"./seaborn-data")
print(iris.head())

483956853663d6d1fc761d7f81b34bbd_00dc51311b374c0384c766a459b857f6.png


构建网格多子图-Building structured multi-plot grids

When exploring multi-dimensional data, a useful approach is to draw multiple instances of the same plot on different subsets of your dataset. This technique is sometimes called either “lattice” or “trellis” plotting, and it is related to the idea of “small multiples”. It allows a viewer to quickly extract a large amount of information about a complex dataset. Matplotlib offers good support for making figures with multiple axes; seaborn builds on top of this to directly link the structure of the plot to the structure of your dataset.

在研究多维数据时,一种有用的方法是在数据集的不同子集上绘制同一图表的多个实例。这种技术有时被称为“格子”或“格子”绘图,它与“小倍数”的思想有关。它允许查看者快速提取关于复杂数据集的大量信息。Matplotlib为制作多轴图形提供了良好的支持;Seaborn在此基础上构建,直接将图的结构链接到数据集的结构。

The figure-level functions are built on top of the objects discussed in this chapter of the tutorial. In most cases, you will want to work with those functions. They take care of some important bookkeeping that synchronizes the multiple plots in each grid. This chapter explains how the underlying objects work, which may be useful for advanced applications.

图形级函数构建在本章教程中讨论的对象之上。在大多数情况下,您将希望使用这些函数。它们负责一些重要的簿记,使每个网格中的多个图同步。本章解释了底层对象是如何工作的,这可能对高级应用程序很有用。


案例1-多个小子图-FacetGrid

The FacetGrid class is useful when you want to visualize the distribution of a variable or the relationship between multiple variables separately within subsets of your dataset. A FacetGrid can be drawn with up to three dimensions: row, col, and hue. The first two have obvious correspondence with the resulting array of axes; think of the hue variable as a third dimension along a depth axis, where different levels are plotted with different colors.

当您希望在数据集的子集中分别可视化变量的分布或多个变量之间的关系时,FacetGrid类非常有用。FacetGrid最多可以用三个维度绘制:row, col, and hue。前两个与得到的轴数组有明显的对应关系;可以将色调变量看作是沿着深度轴的第三维度,其中不同的层次用不同的颜色绘制。

Each of relplot(), displot(), catplot(), and lmplot() use this object internally, and they return the object when they are finished so that it can be used for further tweaking.

relplot()、displot()、catplot()和lmplot()中的每一个都在内部使用该对象,并在完成时返回该对象,以便用于进一步调整。


g = sns.FacetGrid(tips, col="time")

2cf46c20cb26c29438de5958125247d9_420b899a19604db6aef1fdecf1035112.png


按照col和row进行网格布局:


g=sns.FacetGrid(tips, col="time", row="sex")

b4ab34170e3d77883f6b454b6354567f_e4e00bf5286c406b8a1dd1d0200f7a8a.png


Initializing the grid like this sets up the matplotlib figure and axes, but doesn’t draw anything on them.

像这样初始化网格会设置matplotlib图和轴,但不会在上面绘制任何东西。

The main approach for visualizing data on this grid is with the FacetGrid.map() method. Provide it with a plotting function and the name(s) of variable(s) in the dataframe to plot. Let’s look at the distribution of tips in each of these subsets, using a histogram:

在这个网格上可视化数据的主要方法是使用FacetGrid.map()方法。为它提供一个绘图函数和数据框架中要绘图的变量名。让我们用直方图来看看小费在每个子集中的分布情况:


g=sns.FacetGrid(tips, col="time", row="sex")
g.map(sns.histplot, "tip")

98e117c127c246ee7202eea5039ceeb7_b8d8f6c5752f4ca3a39c0413451e7d04.png


This function will draw the figure and annotate the axes, hopefully producing a finished plot in one step. To make a relational plot, just pass multiple variable names. You can also provide keyword arguments, which will be passed to the plotting function:

这个函数将绘制图形并注释坐标轴,希望在一个步骤中生成一个完整的图形。要制作关系图,只需传递多个变量名。你也可以提供关键字参数,这些参数将被传递给绘图函数:


g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(sns.scatterplot, "total_bill", "tip", alpha=.7)
g.add_legend()

68740d073baec3292286d379f5da713b_ddd821e1caeb4df1abfafb1089803f48.png


案例2-绘制成对数据关系Plotting pairwise data relationships

It’s important to understand the differences between a FacetGrid and a PairGrid. In the former, each facet shows the same relationship conditioned on different levels of other variables. In the latter, each plot shows a different relationship (although the upper and lower triangles will have mirrored plots). Using PairGrid can give you a very quick, very high-level summary of interesting relationships in your dataset.

理解FacetGrid和PairGrid之间的区别是很重要的。在前者中,每个方面都表现出相同的关系,条件是其他变量的不同水平。在后者中,每个图都显示了不同的关系(尽管上三角形和下三角形将有镜像图)。使用PairGrid可以非常快速、非常高级地总结数据集中有趣的关系。


g = sns.PairGrid(iris,y_vars=["sepal_length","sepal_width","petal_length","petal_width"],
                  x_vars=["sepal_length","sepal_width","petal_length","petal_width"], hue="species")
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.kdeplot, lw=3, legend=False)
g.add_legend()

26109e8609816d974eba0cdb83e2fead_c60ff39c52304b75b5e7c9be55056fe2.png


总结

本文主要是seaborn从入门到精通系列第3篇,本文介绍了seaborn的绘图功能实现,本文是FacetGrid和PairGrid部分,同时介绍了较好的参考文档置于博客前面,读者可以重点查看参考链接。本系列的目的是可以完整的完成seaborn从入门到精通。重点参考连接


参考

seaborn官方

seaborn官方介绍

seaborn可视化入门

【宝藏级】全网最全的Seaborn详细教程-数据分析必备手册(2万字总结)

Seaborn常见绘图总结


FacetGrid

相关文章
|
1月前
|
程序员 UED Python
Python入门:3.Python的输入和输出格式化
在 Python 编程中,输入与输出是程序与用户交互的核心部分。而输出格式化更是对程序表达能力的极大增强,可以让结果以清晰、美观且易读的方式呈现给用户。本文将深入探讨 Python 的输入与输出操作,特别是如何使用格式化方法来提升代码质量和可读性。
Python入门:3.Python的输入和输出格式化
|
22天前
|
机器学习/深度学习 设计模式 API
Python 高级编程与实战:构建 RESTful API
本文深入探讨了使用 Python 构建 RESTful API 的方法,涵盖 Flask、Django REST Framework 和 FastAPI 三个主流框架。通过实战项目示例,详细讲解了如何处理 GET、POST 请求,并返回相应数据。学习这些技术将帮助你掌握构建高效、可靠的 Web API。
|
1月前
|
开发者 Python
Python入门:8.Python中的函数
### 引言 在编写程序时,函数是一种强大的工具。它们可以将代码逻辑模块化,减少重复代码的编写,并提高程序的可读性和可维护性。无论是初学者还是资深开发者,深入理解函数的使用和设计都是编写高质量代码的基础。本文将从基础概念开始,逐步讲解 Python 中的函数及其高级特性。
Python入门:8.Python中的函数
|
1月前
|
存储 索引 Python
Python入门:6.深入解析Python中的序列
在 Python 中,**序列**是一种有序的数据结构,广泛应用于数据存储、操作和处理。序列的一个显著特点是支持通过**索引**访问数据。常见的序列类型包括字符串(`str`)、列表(`list`)和元组(`tuple`)。这些序列各有特点,既可以存储简单的字符,也可以存储复杂的对象。 为了帮助初学者掌握 Python 中的序列操作,本文将围绕**字符串**、**列表**和**元组**这三种序列类型,详细介绍其定义、常用方法和具体示例。
Python入门:6.深入解析Python中的序列
|
22天前
|
机器学习/深度学习 设计模式 测试技术
Python 高级编程与实战:构建自动化测试框架
本文深入探讨了Python中的自动化测试框架,包括unittest、pytest和nose2,并通过实战项目帮助读者掌握这些技术。文中详细介绍了各框架的基本用法和示例代码,助力开发者快速验证代码正确性,减少手动测试工作量。学习资源推荐包括Python官方文档及Real Python等网站。
|
1月前
|
缓存 算法 数据处理
Python入门:9.递归函数和高阶函数
在 Python 编程中,函数是核心组成部分之一。递归函数和高阶函数是 Python 中两个非常重要的特性。递归函数帮助我们以更直观的方式处理重复性问题,而高阶函数通过函数作为参数或返回值,为代码增添了极大的灵活性和优雅性。无论是实现复杂的算法还是处理数据流,这些工具都在开发者的工具箱中扮演着重要角色。本文将从概念入手,逐步带你掌握递归函数、匿名函数(lambda)以及高阶函数的核心要领和应用技巧。
Python入门:9.递归函数和高阶函数
|
1月前
|
存储 SQL 索引
Python入门:7.Pythond的内置容器
Python 提供了强大的内置容器(container)类型,用于存储和操作数据。容器是 Python 数据结构的核心部分,理解它们对于写出高效、可读的代码至关重要。在这篇博客中,我们将详细介绍 Python 的五种主要内置容器:字符串(str)、列表(list)、元组(tuple)、字典(dict)和集合(set)。
Python入门:7.Pythond的内置容器
|
25天前
|
数据采集 人工智能 数据挖掘
Python 编程基础与实战:从入门到精通
本文介绍Python编程语言,涵盖基础语法、进阶特性及实战项目。从变量、数据类型、运算符、控制结构到函数、列表、字典等基础知识,再到列表推导式、生成器、装饰器和面向对象编程等高级特性,逐步深入。同时,通过简单计算器和Web爬虫两个实战项目,帮助读者掌握Python的应用技巧。最后,提供进一步学习资源,助你在Python编程领域不断进步。
|
23天前
|
机器学习/深度学习 设计模式 API
Python 高级编程与实战:构建微服务架构
本文深入探讨了 Python 中的微服务架构,介绍了 Flask、FastAPI 和 Nameko 三个常用框架,并通过实战项目帮助读者掌握这些技术。每个框架都提供了构建微服务的示例代码,包括简单的 API 接口实现。通过学习本文,读者将能够使用 Python 构建高效、独立的微服务。
|
23天前
|
消息中间件 分布式计算 并行计算
Python 高级编程与实战:构建分布式系统
本文深入探讨了 Python 中的分布式系统,介绍了 ZeroMQ、Celery 和 Dask 等工具的使用方法,并通过实战项目帮助读者掌握这些技术。ZeroMQ 是高性能异步消息库,支持多种通信模式;Celery 是分布式任务队列,支持异步任务执行;Dask 是并行计算库,适用于大规模数据处理。文章结合具体代码示例,帮助读者理解如何使用这些工具构建分布式系统。