结构化图形绘制(FacetGrid)

可实现多行多列个性化绘制图形。

sns.FacetGrid(
data,
row=None,
col=None,
hue=None,
col_wrap=None,
sharex=True,
sharey=True,
height=3,
aspect=1,
palette=None,
row_order=None,
col_order=None,
hue_order=None,
hue_kws=None,
dropna=True,
legend_out=True,
despine=True,
margin_titles=False,
xlim=None,
ylim=None,
subplot_kws=None,
gridspec_kws=None,
size=None,
)
Docstring: Multi-plot grid for plotting conditional relationships.
Init docstring:
Initialize the matplotlib figure and FacetGrid object. This class maps a dataset onto multiple axes arrayed in a grid of rows
and columns that correspond to *levels* of variables in the dataset.
The plots it produces are often called "lattice", "trellis", or
"small-multiple" graphics. It can also represent levels of a third varaible with the ``hue``
parameter, which plots different subets of data in different colors.
This uses color to resolve elements on a third dimension, but only
draws subsets on top of each other and will not tailor the ``hue``
parameter for the specific visualization the way that axes-level
functions that accept ``hue`` will. When using seaborn functions that infer semantic mappings from a
dataset, care must be taken to synchronize those mappings across
facets. In most cases, it will be better to use a figure-level function
(e.g. :func:`relplot` or :func:`catplot`) than to use
:class:`FacetGrid` directly. The basic workflow is to initialize the :class:`FacetGrid` object with
the dataset and the variables that are used to structure the grid. Then
one or more plotting functions can be applied to each subset by calling
:meth:`FacetGrid.map` or :meth:`FacetGrid.map_dataframe`. Finally, the
plot can be tweaked with other methods to do things like change the
axis labels, use different ticks, or add a legend. See the detailed
code examples below for more information. See the :ref:`tutorial <grid_tutorial>` for more information. Parameters
----------
data : DataFrame
Tidy ("long-form") dataframe where each column is a variable and each
row is an observation.
row, col, hue : strings
Variables that define subsets of the data, which will be drawn on
separate facets in the grid. See the ``*_order`` parameters to
control the order of levels of this variable.
col_wrap : int, optional
"Wrap" the column variable at this width, so that the column facets
span multiple rows. Incompatible with a ``row`` facet.
share{x,y} : bool, 'col', or 'row' optional
If true, the facets will share y axes across columns and/or x axes
across rows.
height : scalar, optional
Height (in inches) of each facet. See also: ``aspect``.
aspect : scalar, optional
Aspect ratio of each facet, so that ``aspect * height`` gives the width
of each facet in inches.
palette : palette name, list, or dict, optional
Colors to use for the different levels of the ``hue`` variable. Should
be something that can be interpreted by :func:`color_palette`, or a
dictionary mapping hue levels to matplotlib colors.
{row,col,hue}_order : lists, optional
Order for the levels of the faceting variables. By default, this
will be the order that the levels appear in ``data`` or, if the
variables are pandas categoricals, the category order.
hue_kws : dictionary of param -> list of values mapping
Other keyword arguments to insert into the plotting call to let
other plot attributes vary across levels of the hue variable (e.g.
the markers in a scatterplot).
legend_out : bool, optional
If ``True``, the figure size will be extended, and the legend will be
drawn outside the plot on the center right.
despine : boolean, optional
Remove the top and right spines from the plots.
margin_titles : bool, optional
If ``True``, the titles for the row variable are drawn to the right of
the last column. This option is experimental and may not work in all
cases.
{x, y}lim: tuples, optional
Limits for each of the axes on each facet (only relevant when
share{x, y} is True.
subplot_kws : dict, optional
Dictionary of keyword arguments passed to matplotlib subplot(s)
methods.
gridspec_kws : dict, optional
Dictionary of keyword arguments passed to matplotlib's ``gridspec``
module (via ``plt.subplots``). Requires matplotlib >= 1.4 and is
ignored if ``col_wrap`` is not ``None``. See Also
--------
PairGrid : Subplot grid for plotting pairwise relationships.
relplot : Combine a relational plot and a :class:`FacetGrid`.
catplot : Combine a categorical plot and a :class:`FacetGrid`.
lmplot : Combine a regression plot and a :class:`FacetGrid`.
#导入数据
tips = sns.load_dataset('tips', data_home='./seaborn-data')
tips

#设置风格
sns.set_style('white')
#col设置网格的分类列,row设置分类行
ax = sns.FacetGrid(tips, col='time', row='sex')

散点图

ax = sns.FacetGrid(tips, col='time', row='sex')
#利用map方法在网格内绘制散点图sns.scatterplot = plt.scatter,sns的散点图更美观
ax = ax.map(sns.scatterplot, 'total_bill', 'tip')

#hue设置分类绘制
ax = sns.FacetGrid(tips, col='time', hue='sex')
ax = ax.map(sns.scatterplot, 'total_bill', 'tip')
#添加图例
ax = ax.add_legend()

#定义一个文本函数
def annotate(data, **kws):
n = len(data)
ax = plt.gca()
ax.text(.1, .6, f"N={n}", transform=ax.transAxes) ax = sns.FacetGrid(tips, col='time')
ax = ax.map(sns.scatterplot, 'total_bill', 'tip')
#添加文本标签
ax = ax.map_dataframe(annotate)

#其它参数
#margin_titles设置边缘标头
ax = sns.FacetGrid(tips, col='sex', row='time', margin_titles=True)
ax.map(sns.scatterplot, 'total_bill', 'tip')
#set_axis_labels设置x轴和y轴标签
ax.set_axis_labels('Totall bill($)', 'Tip($)')
#set_titles设置标头(会出现重叠现象)
ax.set_titles(col_template='{col_name} patrons', row_template='{row_name}')
#坐标上下限和刻度设置
ax.set(xlim=(0,60), ylim=(0, 12), xticks=[10, 30, 50], yticks=[2, 6, 10])
#存储图片
ax.savefig('facet_plot.png')

#despine设置是否显示上和右边框线
ax = sns.FacetGrid(tips, col='sex', row='time', margin_titles=True, despine=False)
ax.map(sns.scatterplot, 'total_bill', 'tip')
#设置图形间隔
ax.fig.subplots_adjust(wspace=0, hspace=0)

#QQ图:检验样本数据概率分布(例如正态分布)的方法,直观的表示观测值与预测值之间的差异,或两个数之间的差异
from scipy import stats
#定义QQ图函数
def qqplot(x, y, **kwargs):
_, xr = stats.probplot(x, fit=False) #拟合概率图
_, yr = stats.probplot(y, fit=False)
sns.scatterplot(xr, yr, **kwargs) #**kwargs表示关键字参数,可以用字典形式传入参数 g = sns.FacetGrid(tips, col='smoker', hue='sex')
g = g.map(qqplot, 'total_bill', 'tip')
g = g.add_legend()

直方图

#直方图
g = sns.FacetGrid(tips, col='time', row='smoker')
g = g.map(plt.hist, 'total_bill')

#bins设置直方图个数,color设置颜色
bins = np.arange(0, 65, 5)
g = sns.FacetGrid(tips, col='time', row='smoker')
g = g.map(plt.hist, 'total_bill', bins=bins, color='r')

#height设置高度,aspece设宽高比
bins = np.arange(0, 65, 5)
g = sns.FacetGrid(tips, col='time', row='smoker', height=4, aspect=.5)
g = g.map(plt.hist, 'total_bill', bins=bins, color='r')

#col_order设置显示顺序
bins = np.arange(0, 65, 5)
g = sns.FacetGrid(tips, col='smoker', col_order=['Yea', 'No'])
g = g.map(plt.hist, 'total_bill', bins=bins, color='m')

直方密度图

#直方密度图
g = sns.FacetGrid(tips, col='time', row='smoker')
g = g.map(sns.distplot, 'total_bill')

折线图

#导入数据
att = sns.load_dataset('attention', data_home='./seaborn-data')
#col_wrap设置列数,height设置高度,marker设置标记点样式
g = sns.FacetGrid(att, col='subject', col_wrap=5, height=1.5)
g = g.map(plt.plot, 'solutions', 'score', marker='.')

Seaborn结构化图形绘制(FacetGrid)的更多相关文章

  1. d3.js 之SVG:矢量化图形绘制

    SVG Scalable Vector Graphics 是一个成熟的W3C标准,被设计用来在web和移动平台 上展示可交互的图形.和HTML类似,SVG也支持CSS和JavaScript.尽管可以使 ...

  2. seaborn线性关系数据可视化:时间线图|热图|结构化图表可视化

    一.线性关系数据可视化lmplot( ) 表示对所统计的数据做散点图,并拟合一个一元线性回归关系. lmplot(x, y, data, hue=None, col=None, row=None, p ...

  3. CMM模型,结构化开发方法和面向对象开发方法的比较,UML(统一建模语言),jackson开发方法

    CMM模型 一.CMM简介 CMM,英文全称为Capability Maturity Model for Software,即:软件成熟度模型. CMM的核心是把软件开发视为一个过程.它是对于软件在定 ...

  4. 【Windows编程】系列第五篇:GDI图形绘制

    上两篇我们学习了文本字符输出以及Unicode编写程序,知道如何用常见Win32输出文本字符串,这一篇我们来学习Windows编程中另一个非常重要的部分GDI图形绘图.Windows的GDI函数包含数 ...

  5. 图形绘制 Canvas Paint Path 详解

    图形绘制简介        Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0.大部分2D使用的api都在android.grap ...

  6. XHTML 结构化:使用 XHTML 重构网站

    http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...

  7. 结构化您的Python工程

    我们对于"结构化"的定义是您关注于怎样使您的项目最好地满足它的对象性,我们 需要去考虑如何更好地利用Python的特性来创造简洁.高效的代码.在实践层面, "结构化&qu ...

  8. (转)GPU图形绘制管线

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人”第二章. 图形绘制管线描述GPU渲染流程, ...

  9. 妙味,结构化模块化 整站开发my100du

    ********************************************************************* 重要:重新审视的相关知识 /* 妙味官网:www.miaov ...

  10. Python中的结构化数据分析利器-Pandas简介

    Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发tea ...

随机推荐

  1. 【LeetCode回溯算法#01】图解组合问题

    组合问题 力扣题目链接(opens new window) 给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2 ...

  2. 浅入 ABP 系列(6):数据库配置

    浅入 ABP 系列(6):数据库配置 版权护体作者:痴者工良,微信公众号转载文章需要 <NCC开源社区>同意. 目录 浅入 ABP 系列(6):数据库配置 创建标准的 EFCore 数据库 ...

  3. 今天接到一个根据excel来更新数据库的需求,用php写个小脚本

    需求大概内容是,excel中有些条目需要删除.有些需要新增,就需要基于这份excel生成删.增的SQL. 要求是这样的:蓝色要删除的,黄色是要新增的,白色和灰色的不用管. 我第一时间就在想:还得识别单 ...

  4. C++ 模板的笔记2

    C++模板的笔记2 关于可变参函数模板借鉴了一部分笔记,感谢大佬 类模板中的嵌套 类模板可以嵌套其他类模板,就像普通类可以嵌套其他普通类一样.嵌套的类模板可以访问外部类模板的成员,包括私有成员. 示例 ...

  5. Java 在三个数字中找出最大值

    1 int aa1 = 11000000; 2 int aa2 = 20000; 3 int aa3 = 6000; 4 5 //第一种 6 int max = (aa1 > aa2)? aa1 ...

  6. XAF Blazor TabbedMdi

    开源项目地址:https://gitee.com/easyxaf/blazor-tabbed-mdi 前言 XAF在WinForm中采用了多文档界面(MDI),但在Blazor中却没有,在官网中也有人 ...

  7. period 发音 per + iod 没有ri音 (per=round od=hod=way)

    period 发音 per + iod 没有ri音 pɪər iə d peri-在周围 + od-=hod-路,引申词义时期,阶段,句号等. per = round period 美: [ˈpɪrɪ ...

  8. openssl 版本兼容问题 备忘录

    第三方依赖openssl,但openssl却有版本不同符号不兼容的问题,由于条件限制不得不使用固定版本的openssl,又或者同时有两个第三方依赖不同版本的openssl,只能靠手动,为了备忘. 1. ...

  9. [置顶] drools规则引擎因为内存泄露导致的内存溢出

    进入这个问题之前,先了解一下drools: 在很多行业应用中比如银行.保险领域,业务规则往往非常复杂,并且规则处于不断更新变化中,而现有很多系统做法基本上都是将业务规则绑定在程序代码中. 主要存在的问 ...

  10. 直播预约 | 邀您共同探讨“云XR技术如何改变元宇宙的虚拟体验”

    随着数字化时代的到来,元宇宙成为了人们关注的焦点.它是一个虚拟的世界,融合了现实与虚拟的元素,让人们可以以全新的方式进行交互.创作和体验. 云XR技术是元宇宙建设的重要支撑技术之一,元宇宙需要具备高度 ...