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天前
|
存储 关系型数据库 数据库
利用Python与SQLite构建轻量级数据库应用
在当今日益增长的数据处理需求下,数据库成为存储、检索和管理数据的关键技术。然而,对于小型项目或快速原型开发,大型数据库系统可能显得过于庞大和复杂。本文将介绍如何利用Python编程语言与SQLite轻量级数据库,快速搭建一个功能齐全、易于维护的数据库应用。我们将探讨SQLite数据库的特点、Python对SQLite的支持,并通过一个实际案例展示如何构建一个简单的数据库应用,为读者提供一种高效、灵活的解决方案。
|
2天前
|
机器学习/深度学习 人工智能 数据可视化
Python编程入门:从零开始探索编程的奇妙世界
这篇教程引导初学者入门Python编程,从安装Python开始,逐步讲解基本语法,如`print()`、变量、条件判断、循环以及自定义函数。文章强调了Python在数据处理、数据分析、人工智能和机器学习等领域的重要性,并鼓励学习者探索Python的广泛应用,开启编程之旅。
|
3天前
|
数据可视化 API Python
Python零基础“圣经”!300W小白从入门到精通首选!
今天分享的这本书在让你尽快学会 Python基础知识的同时,能够编写并正确的运行程序(游戏、数据可视化、Web应用程序) 最大的特色在于,在为初学者构建完整的 Python 语言知识体系的同时,面向实际应用情境编写代码样例,而且许多样例还是 后续实践项目部分的伏笔。实践项目部分的选题经过精心设计,生动详尽 又面面俱到。相信这本书能够得到更多 Python 初学者的喜爱。
小白入门必备!计算机科学教程的Python精要参考PDF开放下载!
随着互联网产业的高速发展,在网络上早已积累了极其丰富的Python学习资料,任何人都可以基于这些资源,自学掌握 Python。 但实际上,网络上充斥的资源太多、太杂且不成体系,在没有足够的编程/工程经验之前,仅靠“看”线上资源自学,的确是一件非常困难的事。
|
5天前
|
Linux 开发工具 Python
初学者从无到有的Python语言如何入门,这份Python学习路线赶紧带走_python 从无到(1)
初学者从无到有的Python语言如何入门,这份Python学习路线赶紧带走_python 从无到(1)
初学者从无到有的Python语言如何入门,这份Python学习路线赶紧带走_python 从无到(1)
|
5天前
|
缓存 API 数据库
构建高效Python Web应用:Flask框架与RESTful API设计原则
【5月更文挑战第20天】 在现代Web开发中,构建一个轻量级且高效的后端服务至关重要。本文将深入探讨如何使用Python的Flask框架结合RESTful API设计原则来创建可扩展和易于维护的Web应用程序。我们将通过分析Flask的核心特性,以及如何利用它来实现资源的合理划分、接口的版本控制和请求处理优化等,来指导读者打造高性能的API服务。文中不仅提供了理论指导,还包括了实践案例,旨在帮助开发者提升开发效率,并增强应用的稳定性和用户体验。
|
6天前
|
数据采集 算法 Python
2024年Python最全python基础入门:高阶函数,小米面试编程题
2024年Python最全python基础入门:高阶函数,小米面试编程题
|
6天前
|
存储 数据采集 数据挖掘
真正零基础Python入门:手把手教你从变量和赋值语句学起
真正零基础Python入门:手把手教你从变量和赋值语句学起
|
6天前
|
数据可视化 数据挖掘 Python
【Python DataFrame专栏】DataFrame的可视化探索:使用matplotlib和seaborn
【5月更文挑战第20天】本文介绍了使用Python的pandas、matplotlib和seaborn库进行数据可视化的步骤,包括创建示例数据集、绘制折线图、柱状图、散点图、热力图、箱线图、小提琴图和饼图。这些图表有助于直观理解数据分布、关系和趋势,适用于数据分析中的探索性研究。
【Python DataFrame专栏】DataFrame的可视化探索:使用matplotlib和seaborn
|
6天前
|
数据采集 存储 数据挖掘
Python DataFrame初学者指南:轻松上手构建数据表格
【5月更文挑战第19天】本文是针对初学者的Pandas DataFrame指南,介绍如何安装Pandas、创建DataFrame(从字典或CSV文件)、查看数据(`head()`, `info()`, `describe()`)、选择与操作数据(列、行、缺失值处理、数据类型转换、排序、分组聚合)以及保存DataFrame到CSV文件。通过学习这些基础,你将能轻松开始数据科学之旅。