seaborn matlab,Seaborn 和 Matplotlib 数据可视化

简述

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

import pandas as pd # 数据分析库

import matplotlib.pyplot as plt

import seaborn as sns

在 Jupyter Notebook 中,为了让图片内嵌在网页中,可以打开如下开关%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 数组。

举个例子X = np.linspace(0, 2 * np.pi, 10)

plt.plot(X, np.sin(X), '-o')

plt.title('Sine curve')

plt.xlabel(r'$\alpha$')

plt.ylabel(r'sin($\alpha$)')

16f07e99f7f3340ebbdff0df01cf947a.png

设置标题及 X,Y 轴方法一plt.figure(figsize=(3, 2))

plt.title("Title")

plt.xlabel("X")

plt.ylabel("Y")

plt.plot(np.arange(10), np.sin(np.arange(10)))方法二f, ax = plt.subplots(figsize=(3, 2))

ax.set_xlabel("X")

ax.set_ylabel("Y")

ax.set_title("Title", fontsize=20)

ax.plot(np.arange(10), np.sin(np.arange(10)))

导出矢量图

在论文写作中,一般要求插入图片的格式为矢量图,Matplotlib 和 Seaborn 图片可以用如下代码导出plt.plot(.......)

# pdf 格式

plt.savefig('./filename.pdf',format='pdf')

# svg 格式

plt.savefig('./filename.svg',format='svg')

Seaborn 基础

Seaborn 要求原始数据的输入类型为 pandas 的 Dataframe 或 Numpy 数组,画图函数一般为如下形式

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。

举个例子,建立如下数据集X = np.linspace(0, 20, 10)

df = pd.DataFrame({"Input": X, "Linear": X, "Sin": np.sin(X)})Input Linear Sin

0 0.000000 0.000000 0.000000

1 2.222222 2.222222 0.795220

2 4.444444 4.444444 -0.964317

3 6.666667 6.666667 0.374151

4 8.888889 8.888889 0.510606

……

我们来拟合第一列与第二列sns.regplot(x='Input', y='Linear', data=df)

8055bab60e33b9ef6b97fb2ead317ab4.png

子图的绘制

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

subplots

一般形式为

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

举两个例子f, ax = plt.subplots(ncols=2, nrows=2, figsize=(8, 6))

X = np.arange(0.01, 10, 0.01)

ax[0, 0].plot(X, 2 * X - 1)

ax[0, 0].set_title("Linear")

ax[0, 1].plot(X, np.log(X))

ax[0, 1].set_title("Log")

ax[1, 0].plot(X, np.exp(X))

ax[1, 0].set_title("Exp")

ax[1, 1].plot(X, np.sin(X))

ax[1, 1].set_title("Sin")

af40a586ff33d675918910c6ca48e695.png

# 设置风格

sns.set(style="white", context="talk")

# 随机数生成器

rs = np.random.RandomState(7)

# Set up the matplotlib figure

f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(8, 6), sharex=True)

# Generate some sequential data

x = np.array(list("ABCDEFGHI"))

y1 = np.arange(1, 10)

sns.barplot(x, y1, palette="BuGn_d", ax=ax1)

ax1.set_ylabel("Sequential")

# Center the data to make it diverging

y2 = y1 - 5

sns.barplot(x, y2, palette="RdBu_r", ax=ax2)

ax2.set_ylabel("Diverging")

# Randomly reorder the data to make it qualitative

y3 = rs.choice(y1, 9, replace=False)

sns.barplot(x, y3, palette="Set3", ax=ax3)

ax3.set_ylabel("Qualitative")

# Finalize the plot

sns.despine(bottom=True)

plt.setp(f.axes, yticks=[])

plt.tight_layout(h_pad=3)

896d7b7ac03cfca7522c245a9ca2c323.png

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.

举几个例子plt.figure(figsize=(3, 3))

plt.subplot(221)

# 分成2x2,占用第二个,即第一行第二列的子图

plt.subplot(222)

# 分成2x1,占用第二个,即第二行

plt.subplot(212)

3c30f390fc1d7a390def4b655b20b42f.png

plt.figure(figsize=(3, 3))

plt.subplot(221)

plt.subplot(222)

plt.subplot(223)

plt.show()

2b8f3b5741b63fb7b188a59025846a1b.png

plt.figure(figsize=(3, 3))

plt.subplot(121)

plt.subplot(222)

plt.subplot(224)

plt.show()

84743f0aaa3d17233c39623562061149.png

def f(t):

return np.exp(-t) * np.cos(2 * np.pi * t)

t1 = np.arange(0, 5, 0.1)

t2 = np.arange(0, 5, 0.02)

plt.subplot(221)

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')

plt.subplot(222)

plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')

plt.subplot(212)

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

plt.show()

00f53330619a79284024ef011a31d1f2.png

直方图

准备数据X = np.arange(8)

y = np.array([1, 4, 2, 3, 3, 5, 6, 3])

df = pd.DataFrame({"X":X, "y":y})sns.barplot("X", "y", palette="RdBu_r", data=df)

# 或者下面这种形式,但需要自行设置Xy轴的 label

# sns.barplot(X, y, palette="RdBu_r")

ef3605dcc8837608d17c9457c9a5048e.png

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

统计图

先调一下背景和载入一下数据sns.set(style="darkgrid")

titanic = sns.load_dataset("titanic")

统计图sns.countplot(x="class", data=titanic)

900c6cf2d201a69ef2a781ee39cbffa6.png

带 hue 的统计图(为了显示美观,可以调一下大小)f, ax = plt.subplots(figsize=(8, 6))

sns.countplot(x="class", hue="who", data=titanic, ax=ax)

eda3678970475890e3ae114d8c92e3f8.png

描述变量分布

描述变量的分布规律,方差、均值、极值等,通常使用 boxplots 图(箱图)和 violins 图(小提琴图)。sns.set(style="whitegrid", palette="pastel", color_codes=True)

# Load the example tips dataset

tips = sns.load_dataset("tips")

violins 图sns.violinplot(x="day", y="total_bill", data=tips)

sns.despine(left=True) # 不显示网格边框线

79b2b3c588f91a33327f66c7099949ed.png

如图,图的高矮代表 y 值的范围,图的胖瘦代表分布规律。

当然,也可以描述不同 label 的分布,下图就表示了男女在不同时间的消费差异sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True,

inner="quart", palette={"Male": "b", "Female": "y"})

sns.despine(left=True)

fd3dc899e19f30e2c049c4eb95a79cdc.png

box 图

箱图和小提琴图的描述手段基本类似sns.boxplot(x="day", y="total_bill", data=tips, palette="PRGn")

sns.despine(offset=10, trim=True) # 设置边框的风格

bfff4d029dc9c05554135143b2ff1250.pngsns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")

sns.despine(offset=10, trim=True)

3b8d40bf713581d5bac5dd66cca46c74.png

数据分布直方图

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

准备一些数据mu,sigma=100,15

x=mu+sigma*np.random.randn(10000)Matplotlib 形式sns.set_color_codes()

n,bins,patches=plt.hist(x,50,normed=1,facecolor='g',alpha=0.75)

plt.xlabel('Smarts')

plt.ylabel('Probability')

plt.title('Histogram of IQ')

plt.text(60,.025, r'$\mu=100,\ \sigma=15$')

plt.axis([40,160,0,0.03])

plt.grid(True)

d1cffea3b53db3ed3d667db1e3d94e3a.png

Seaborn 形式sns.set_color_codes()

plt.xlabel('Smarts')

plt.ylabel('Probability')

plt.title('Histogram of IQ')

plt.text(60,.025, r'$\mu=100,\ \sigma=15$')

sns.distplot(x, color="y")

d3a7a80ad15b9704e8f4f6f50b26ec05.png

描述相关性

一般的,描述相关性一般使用 pairplot 图和 heatmap 图。

先 load 一下数据集和设置背景风格sns.set(style="ticks")

df = sns.load_dataset("iris")

pairplot 图

pairplot 图一般用来描述不同 label 在同一 feature 上的分布。sns.pairplot(df, hue="species")

4bdd1b634b8af09e7c11bed9302f0649.png

heatmap 图

heatmap 图一般用来描述 feature 的相关性矩阵sns.heatmap(df.corr(), square=True)

66cf6d632c3915131176fade4c0ed44a.png

经过一些实践,下述代码的配色方案比较美观。colormap = plt.cm.viridis

plt.figure(figsize=(12,12)) // 根据需要自行设置大小(也可省略)

plt.title('Pearson Correlation of Features', y=1.05, size=15) // 加标题

sns.heatmap(df.corr(),linewidths=0.1,vmax=1.0, square=True, cmap=colormap, linecolor='white', annot=True)

4197873fd4e36e3330b1f300f868a1e6.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值