2020-10-26
探索性数据分析 (EDA) 涉及两个基本步骤
-
数据分析 (数据预处理、清洗以及处理) 。
-
数据可视化 (使用不同类型的图来展示数据中的关系) 。
Pandas 是 Python 中最常用的数据分析库。Python 提供了大量用于数据可视化的库,Matplotlib 是最常用的,它提供了对绘图的完全控制,并使得绘图自定义变得容易。
但是,Matplotlib 缺少了对 Pandas 的支持。而 Seaborn 弥补了这一缺陷,它是建立在 Matplotlib 之上并与 Pandas 紧密集成的数据可视化库。
然而,Seaborn 虽然活干得漂亮,但是函数众多,让人不知道到底该怎么使用它们?不要怂,本文就是为了理清这点,让你快速掌握这款利器。
这篇文章主要涵盖如下内容,
-
Seaborn 中提供的不同的绘图类型。
-
Pandas 与 Seaborn 的集成如何实现以最少的代码绘制复杂的多维图?
-
如何在 Matplotlib 的辅助下自定义 Seaborn 绘图设置?
谁适合阅读这篇文章?
如果你具备 Matplotlib 和 Pandas 的基本知识,并且想探索一下 Seaborn,那么这篇文章正是不错的起点。
如果目前只掌握 Python,建议 翻阅文末相关文章,特别是 在掌握 Pandas 的基本使用之后再回到这里来或许会更好一些。
1 Matplotlib
尽管仅使用最简单的功能就可以完成许多任务,但是了解 Matplotlib 的基础非常重要,其原因有两个,
-
Seaborn 在底层使用 Matplotlib 绘图。
-
一些自定义项需要直接使用 Matplotlib。
这里对 Matplotlib 的基础作个简单概述。下图显示了 Matplotlib 窗口的各个要素。
需要了解的三个主要的类是图形 (Figure) ,图轴 (Axes) 以及坐标轴 (Axis) 。
-
图形 (Figure)
它指的就是你看到的整个图形窗口。同一图形中可能有多个子图 (图轴) 。在上面的示例中,在一个图形中有四个子图 (图轴) 。
-
图轴 (Axes)
图轴就是指图形中实际绘制的图。一个图形可以有多个图轴,但是给定的图轴只是整个图形的一部分。在上面的示例中,我们在一个图形中有四个图轴。
-
坐标轴 (Axis)
坐标轴是指特定图轴中的实际的 x-轴和 y-轴。
本帖子中的每个示例均假设已经加载所需的模块以及数据集,如下所示,
import pandas as pd import numpy as np from matplotlib import pyplot as plt import seaborn as sns tips = sns.load_dataset('tips') iris = sns.load_dataset('iris')
import matplotlib matplotlib.style.use('ggplot')
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
iris.head()
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
让我们通过一个例子来理解一下 Figure 和 Axes 这两个类。
dates = ['1981-01-01', '1981-01-02', '1981-01-03', '1981-01-04', '1981-01-05', '1981-01-06', '1981-01-07', '1981-01-08', '1981-01-09', '1981-01-10'] min_temperature = [20.7, 17.9, 18.8, 14.6, 15.8, 15.8, 15.8, 17.4, 21.8, 20.0] max_temperature = [34.7, 28.9, 31.8, 25.6, 28.8, 21.8, 22.8, 28.4, 30.8, 32.0] fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(10,5)); axes.plot(dates, min_temperature, label='Min Temperature'); axes.plot(dates, max_temperature, label = 'Max Temperature'); axes.legend();
plt.subplots() 创建一个 Figure 对象实例,以及 nrows x ncols 个 Axes 实例,并返回创建的 Figure 对象和 Axes 实例。在上面的示例中,由于我们传递了 nrows = 1 和 ncols = 1,因此它仅创建一个 Axes 实例。如果 nrows > 1 或 ncols > 1,则将创建一个 Axes 网格并将其返回为 nrows 行 ncols 列的 numpy 数组。
Axes 类最常用的自定义方法有,
Axes.set_xlabel() Axes.set_ylabel() Axes.set_xlim() Axes.set_ylim() Axes.set_xticks() Axes.set_yticks() Axes.set_xticklabels() Axes.set_yticklabels() Axes.set_title() Axes.tick_params()
下面是一个使用上述某些方法进行自定义的例子,
fontsize =20 fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(15,7)) axes.plot(dates, min_temperature, label='Min Temperature') axes.plot(dates, max_temperature, label='Max Temperature') axes.set_xlabel('Date', fontsize=fontsize) axes.set_ylabel('Temperature', fontsize=fontsize) axes.set_title('Daily Min and Max Temperature', fontsize=fontsize) axes.set_xticks(dates) axes.set_xticklabels(dates) axes.tick_params('x', labelsize=fontsize, labelrotation=30, size=15) axes.set_ylim(10,40) axes.set_yticks(np.arange(10,41,2)) axes.tick_params('y',labelsize=fontsize) axes.legend(fontsize=fontsize,loc='upper left', bbox_to_anchor=(1,1));
上面我们快速了解了下 Matplotlib 的基础知识,现在让我们进入 Seaborn。
2 Seaborn
Seaborn 中的每个绘图函数既是图形级函数又是图轴级函数,因此有必要了解这两者之间的区别。
-
如前所述,图形指的是你看到的整个绘图窗口上的图,而图轴指的是图形中的一个特定子图。
-
图轴级函数只绘制到单个 Matplotlib 图轴上,并不影响图形的其余部分。
-
而图形级函数则可以控制整个图形。
我们可以这么来理解这一点,图形级函数可以调用不同的图轴级函数在不同的图轴上绘制不同类型的子图。
sns.set_style('darkgrid')
2.1 图轴级函数
下面罗列的是 Seaborn 中所有图轴级函数的详细列表。
关系图Relational Plots
-
scatterplot( )
-
lineplot( )
类别图Categorical Plots
-
striplot( )、swarmplot( )
-
boxplot( )、boxenplot( )
-
violinplot( )、countplot( )
-
pointplot( )、barplot( )
分布图Distribution Plots
-
distplot( )
-
kdeplot( )
-
rugplot( )
回归图Regression Plots
-
regplot( )
-
residplot( )
矩阵图MatrixPlots( )
-
heatmap( )
使用任何图轴级函数需要了解的两点,
-
将输入数据提供给图轴级函数的不同方法。
-
指定用于绘图的图轴。
2.1.1 将输入数据提供给图轴级函数的不同方法
1、列表、数组或系列
将数据传递到图轴级函数的最常用方法是使用迭代器,例如列表 list,数组 array 或序列 series
total_bill = tips['total_bill'].values tip = tips['tip'].values fig = plt.figure(figsize=(10, 5)) sns.scatterplot(total_bill, tip, s=15);
tip = tips['tip'].values day = tips['day'].values fig = plt.figure(figsize=(10, 5)) sns.boxplot(day, tip, palette="Set2");