Seaborn 和 Matplotlib 数据可视化

简述

Python 中,数据可视化一般是通过较底层的 Matplotlib 库和较高层的 Seaborn 库实现的,本文主要介绍一些常用的图的绘制方法。在正式开始之前需要导入以下包

  1. import numpy as np # 线性代数库
  2. import pandas as pd # 数据分析库
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns

Jupyter Notebook 中,为了让图片内嵌在网页中,可以打开如下开关

  1. %matplotlib inline

另外,设置了不同的图像效果和背景风格,图片的显示也不一样。

Matplotlib 基础

函数基本形式

Matplotlib 要求原始数据的输入类型为 Numpy 数组,画图函数一般为如下形式(与 Matlab 基本一致)
plt.图名(x, y, 'marker 类型')

例如
plt.plot(x, y)
plt.plot(x, y, 'o-')
plt.plot(x, y, 'o-')
plt.scatter(x, y)
plt.scatter(x, y, linewidths=x,marker='o')

等等,参数 x,y 要求为 np 数组。

举个例子

  1. X = np.linspace(0, 2 * np.pi, 10)
  2. plt.plot(X, np.sin(X), '-o')
  3. plt.title('Sine curve')
  4. plt.xlabel(r'$\alpha$')
  5. plt.ylabel(r'sin($\alpha$)')
Seaborn 和 Matplotlib 数据可视化-图片1

设置标题及 X,Y 轴

  • 方法一
  1. plt.figure(figsize=(3, 2))
  2. plt.title("Title")
  3. plt.xlabel("X")
  4. plt.ylabel("Y")
  5. plt.plot(np.arange(10), np.sin(np.arange(10)))
  • 方法二
  1. f, ax = plt.subplots(figsize=(3, 2))
  2. ax.set_xlabel("X")
  3. ax.set_ylabel("Y")
  4. ax.set_title("Title", fontsize=20)
  5. ax.plot(np.arange(10), np.sin(np.arange(10)))

导出矢量图

在论文写作中,一般要求插入图片的格式为矢量图,MatplotlibSeaborn 图片可以用如下代码导出

  1. plt.plot(.......)
  2. # pdf 格式
  3. plt.savefig('./filename.pdf',format='pdf')
  4. # svg 格式
  5. plt.savefig('./filename.svg',format='svg')

Seaborn 基础

Seaborn 要求原始数据的输入类型为 pandasDataframeNumpy 数组,画图函数一般为如下形式
sns.图名(x='X轴 列名', y='Y轴 列名', data=原始数据df对象)


sns.图名(x='X轴 列名', y='Y轴 列名', hue='分组绘图参数', data=原始数据df对象)


sns.图名(x=np.array, y=np.array[, ...])

hue 的意思是 variable in data to map plot aspects to different colors

举个例子,建立如下数据集

  1. X = np.linspace(0, 20, 10)
  2. df = pd.DataFrame({"Input": X, "Linear": X, "Sin": np.sin(X)})
  1. Input Linear Sin
  2. 0 0.000000 0.000000 0.000000
  3. 1 2.222222 2.222222 0.795220
  4. 2 4.444444 4.444444 -0.964317
  5. 3 6.666667 6.666667 0.374151
  6. 4 8.888889 8.888889 0.510606
  7. ……

我们来拟合第一列与第二列

  1. sns.regplot(x='Input', y='Linear', data=df)
Seaborn 和 Matplotlib 数据可视化-图片2

子图的绘制

绘制子图一般使用 subplots 和 subplot 函数,我们分别介绍。

subplots

一般形式为
f, ax = plt.subplots(ncols=列数, nrows=行数[, figsize=图片大小, ...])

举两个例子

  1. f, ax = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))
  2.  
  3. X = np.arange(0.01, 10, 0.01)
  4. ax[0, 0].plot(X, 2 * X - 1)
  5. ax[0, 0].set_title("Linear")
  6. ax[0, 1].plot(X, np.log(X))
  7. ax[0, 1].set_title("Log")
  8. ax[1, 0].plot(X, np.exp(X))
  9. ax[1, 0].set_title("Exp")
  10. ax[1, 1].plot(X, np.sin(X))
  11. ax[1, 1].set_title("Sin")
Seaborn 和 Matplotlib 数据可视化-图片3
  1. # 设置风格
  2. sns.set(style="white", context="talk")
  3. # 随机数生成器
  4. rs = np.random.RandomState(7)
  5. # Set up the matplotlib figure
  6. f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(8, 6), sharex=True)
  7.  
  8. # Generate some sequential data
  9. x = np.array(list("ABCDEFGHI"))
  10. y1 = np.arange(1, 10)
  11. sns.barplot(x, y1, palette="BuGn_d", ax=ax1)
  12. ax1.set_ylabel("Sequential")
  13.  
  14. # Center the data to make it diverging
  15. y2 = y1 - 5
  16. sns.barplot(x, y2, palette="RdBu_r", ax=ax2)
  17. ax2.set_ylabel("Diverging")
  18.  
  19. # Randomly reorder the data to make it qualitative
  20. y3 = rs.choice(y1, 9, replace=False)
  21. sns.barplot(x, y3, palette="Set3", ax=ax3)
  22. ax3.set_ylabel("Qualitative")
  23.  
  24. # Finalize the plot
  25. sns.despine(bottom=True)
  26. plt.setp(f.axes, yticks=[])
  27. plt.tight_layout(h_pad=3)
Seaborn 和 Matplotlib 数据可视化-图片3

subplot

基本形式为
subplot(nrows, ncols, index, **kwargs)

In the current figure, create and return an Axes, at position index of a (virtual) grid of nrows by ncols axes. Indexes go from 1 to nrows *ncols, incrementing in row-major order.
If nrows, ncols and index are all less than 10, they can also be given as a single, concatenated, three-digit number.
For example, subplot(2, 3, 3) and subplot(233) both create an Axes at the top right corner of the current figure, occupying half of the figure height and a third of the figure width.

举几个例子

  1. plt.figure(figsize=(3, 3))
  2. plt.subplot(221)
  3. # 分成2x2,占用第二个,即第一行第二列的子图
  4. plt.subplot(222)
  5. # 分成2x1,占用第二个,即第二行
  6. plt.subplot(212)
Seaborn 和 Matplotlib 数据可视化-图片4
  1. plt.figure(figsize=(3, 3))
  2. plt.subplot(221)
  3. plt.subplot(222)
  4. plt.subplot(223)
  5. plt.show()
Seaborn 和 Matplotlib 数据可视化-图片4
  1. plt.figure(figsize=(3, 3))
  2. plt.subplot(121)
  3. plt.subplot(222)
  4. plt.subplot(224)
  5. plt.show()
Seaborn 和 Matplotlib 数据可视化-图片4
  1. def f(t):
  2. return np.exp(-t) * np.cos(2 * np.pi * t)
  3. t1 = np.arange(0, 5, 0.1)
  4. t2 = np.arange(0, 5, 0.02)
  5.  
  6. plt.subplot(221)
  7. plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')
  8.  
  9. plt.subplot(222)
  10. plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')
  11.  
  12. plt.subplot(212)
  13. plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
  14.  
  15. plt.show()
Seaborn 和 Matplotlib 数据可视化-图片4

直方图

准备数据

  1. X = np.arange(8)
  2. y = np.array([1, 4, 2, 3, 3, 5, 6, 3])
  3. df = pd.DataFrame({"X":X, "y":y})
  1. sns.barplot("X", "y", palette="RdBu_r", data=df)
  2. # 或者下面这种形式,但需要自行设置Xy轴的 label
  3. # sns.barplot(X, y, palette="RdBu_r")

调整 palette 参数可以美化显示风格。

统计图

先调一下背景和载入一下数据

  1. sns.set(style="darkgrid")
  2. titanic = sns.load_dataset("titanic")

统计图

  1. sns.countplot(x="class", data=titanic)
Seaborn 和 Matplotlib 数据可视化-图片5

hue 的统计图(为了显示美观,可以调一下大小)

  1. f, ax = plt.subplots(figsize=(8, 6))
  2. sns.countplot(x="class", hue="who", data=titanic, ax=ax)

描述变量分布

描述变量的分布规律,方差、均值、极值等,通常使用 boxplots 图(箱图)和 violins 图(小提琴图)。

  1. sns.set(style="whitegrid", palette="pastel", color_codes=True)
  2. # Load the example tips dataset
  3. tips = sns.load_dataset("tips")

violins 图

  1. sns.violinplot(x="day", y="total_bill", data=tips)
  2. sns.despine(left=True) # 不显示网格边框线
Seaborn 和 Matplotlib 数据可视化-图片6

如图,图的高矮代表 y 值的范围,图的胖瘦代表分布规律。
当然,也可以描述不同 label 的分布,下图就表示了男女在不同时间的消费差异

  1. sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True,
  2. inner="quart", palette={"Male": "b", "Female": "y"})
  3. sns.despine(left=True)

box 图

箱图和小提琴图的描述手段基本类似

  1. sns.boxplot(x="day", y="total_bill", data=tips, palette="PRGn")
  2. sns.despine(offset=10, trim=True) # 设置边框的风格
Seaborn 和 Matplotlib 数据可视化-图片7
  1. sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
  2. sns.despine(offset=10, trim=True)
Seaborn 和 Matplotlib 数据可视化-图片7

数据分布直方图

描述单变量的分布可以也使用数据分布直方图
准备一些数据

  1. mu,sigma=100,15
  2. x=mu+sigma*np.random.randn(10000)
  • Matplotlib 形式
  1. sns.set_color_codes()
  2. n,bins,patches=plt.hist(x,50,normed=1,facecolor='g',alpha=0.75)
  3. plt.xlabel('Smarts')
  4. plt.ylabel('Probability')
  5. plt.title('Histogram of IQ')
  6. plt.text(60,.025, r'$\mu=100,\ \sigma=15$')
  7. plt.axis([40,160,0,0.03])
  8. plt.grid(True)
  • Seaborn 形式
  1. sns.set_color_codes()
  2. plt.xlabel('Smarts')
  3. plt.ylabel('Probability')
  4. plt.title('Histogram of IQ')
  5. plt.text(60,.025, r'$\mu=100,\ \sigma=15$')
  6. sns.distplot(x, color="y")

描述相关性

一般的,描述相关性一般使用 pairplot 图和 heatmap 图。
先 load 一下数据集和设置背景风格

  1. sns.set(style="ticks")
  2. df = sns.load_dataset("iris")

pairplot 图

pairplot 图一般用来描述不同 label 在同一 feature 上的分布。

  1. sns.pairplot(df, hue="species")
Seaborn 和 Matplotlib 数据可视化-图片8

heatmap 图

heatmap 图一般用来描述 feature 的相关性矩阵

  1. sns.heatmap(df.corr(), square=True)

经过一些实践,下述代码的配色方案比较美观。

  1. colormap = plt.cm.viridis
  2. plt.figure(figsize=(12,12)) // 根据需要自行设置大小(也可省略)
  3. plt.title('Pearson Correlation of Features', y=1.05, size=15) // 加标题
  4. sns.heatmap(df.corr(),linewidths=0.1,vmax=1.0, square=True, cmap=colormap, linecolor='white', annot=True)
Seaborn 和 Matplotlib 数据可视化-图片9

发表评论

匿名网友

拖动滑块以完成验证
加载失败