seaborn 子图
Data visualizations are essential in data analysis. The famous saying “one picture is worth a thousand words” holds true in the scope of data visualizations as well. In this post, I will explain a well-structured, very informative collection of subplots: FacetGrid.
数据可视化在数据分析中至关重要。 著名的谚语“一张图片值得一千个单词”在数据可视化的范围内也是如此。 在这篇文章中,我将解释一个结构良好,内容丰富的子图集合: FacetGrid 。
FacetGrid is basically a grid of subplots. Matplotlib supports creating figures with multiple axes and thus allows to have subplots in one figure. What FacetGrid puts on top of matplotlib’s subplot structure:
FacetGrid基本上是子图的网格。 Matplotlib支持创建具有多个轴的图形,因此允许在一个图形中包含子图。 FacetGrid在matplotlib的子图结构之上放了什么:
- Making the process easier and smoother (with less code) 使过程更轻松,更流畅(使用更少的代码)
- Transfering the structure of dataset to subplots 将数据集的结构转移到子图
The distribution of a variable or relationship among variables can easily be discovered with FacetGrids. They can have up to three dimensions: row, column, and hue. It will be more clear as we go through examples. So, let’s start.
使用FacetGrids可以轻松发现变量的分布或变量之间的关系。 它们最多可以具有三个维度: row , column和hue 。 通过示例我们将更加清楚。 所以,让我们开始吧。
As always we start with importing libraries.
与往常一样,我们从导入库开始。
Note: FacetGrid requires the data stored in a pandas dataframe where each row represents an observation and columns represent variables. Thus, we also import pandas.
注意 :FacetGrid要求将数据存储在pandas数据框中,其中每一行代表一个观测值,而各列代表变量。 因此,我们也进口大熊猫。
import numpy as np
import pandas as pdimport matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='darkgrid', color_codes=True)%matplotlib inline
We will use the built-in “tips” dataset of seaborn.
我们将使用seaborn的内置“提示”数据集。
tip = sns.load_dataset("tips")
tip.head()