python数据可视化之Seaborn(一)

写在开头:在Titani数据可视化中使用到了Seaborn的绘图,为了弄清楚Seaborn绘图的内容,因而开始分享它的相关内容。本文主要参考文献为路远老师的sns教程以及官方文档连接放在文章最后。全文至关因而一个学习的训练。html

Seaborn其实就是在matplotlib的基础上进行的高级封装,调用一些封装好了的图形设置。但其设计的灵活性不如matplotlib,所以Seaborn能够说是可以锦上添花的有利工具。python

Seaborn可视化内容安排

在Seaborn的学习中安排以下,
1、画风设置:会简单介绍一下绘图风格(一)与颜色风格(二)的设置;
2、绘图技巧:这里会介绍数据集(三)、分类数据(四)、线性关系(五)可视化的相关内容;
3、结构网络:本节主要介绍数据识别结构网络的绘图(六)。
首先咱们开始总体布局的介绍。web

1、Seaborn画风设置

绘图风格

为了对绘图的美观程度进行改善,咱们能够进行一些通用性设置,首先咱们引入一个例子来看一看,matplotlib绘图与seaborn绘图的区别。这里使用里ListedColormap()函数进行调色,具体调色名称可见最后参考文献中。咱们定义了一组同心圆,下面咱们来看看不用的绘图风格下,图形是什么样子的。segmentfault

#载入包
%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.colors import ListedColormap
np.random.seed(42)

#导入数据函数
def explot():
    color = ['Sienna','Coral','Crimson','GoldEnrod','ForestGreen']
    x = y = np.arange(-3, 3, 0.1)
    x, y = np.meshgrid(x,y)
    for i in range(1,5):
        cmap = ListedColormap(color[i])
        plt.contour(x, y, x**2 + y**2, [i], cmap=cmap)#x**2 + y**2 = i的圆形
    plt.axis('scaled')
    plt.show()

matplotlib默认参数下绘制的结果以下:数组

explot()

在这里插入图片描述
而使用sns的方法是能够用sns.set()进行简单的设置,输出图片以下,网络

sns.set()
explot()

在这里插入图片描述
Seaborn包含两组独立的参数组合,分别是风格控制和图形缩放两种方法。
风格控制:axes_style(), set_style()|返回一组字典参数。
图形缩放:plotting_context(), set_context()|会设置matplotlib默认参数。dom

Seaborn风格控制

在seaborn中有五种风格能够进行切换,分别是:ticks,white,dark,whitegrid,darkgrid,其中darkgrid是默认参数。下面来作几张图看看其中的模样。svg

#这里没有展现darkgrid的风格
data = np.random.normal(size=(20, 7))+np.sqrt(np.arange(7))
plt.figure(figsize=(20,20))
list = ['ticks', 'white', 'dark', 'whitegrid']
xlabel = ['whitegrid', 'ticks', 'white', 'dark']
for i in range(0,4):
    s = '22'+str(i+1)
    plt.subplot(int(s))
    sns.set_style(list[i])
    sns.boxplot(data=data)
    plt.xlabel(xlabel[i], fontsize=20)

在这里插入图片描述

对轴进行修改

在matplotlib的参数中没法移除图像顶部和右边没必要要的轴,因而能够经过Seaborn的despine()方法进行移除。函数

plt.figure(figsize=(20,5))
x = np.linspace(0, 14, 100)
plt.subplot(121)
for i in range(1, 7):
    plt.plot(x, np.sin(x + i * .5) * (7 - i) * 1)
    sns.despine()
plt.subplot(122)
for i in range(1, 7):
    plt.plot(x, np.sin(x + i * .5) * (7 - i) * 1)

在这里插入图片描述
左边是移除了轴脊柱的图像、右边是原matplotlib的图像。也可使用despine()方法中的trim参数来限制轴的范围,也能够经过调整控制哪一个脊柱被移除工具

sns.set_style("ticks")
sns.boxplot(data=data)
sns.despine(offset=10, trim=True)

在这里插入图片描述

sns.boxplot(data=data)
sns.despine(offset=10, trim=True, left=True, bottom=True)

在这里插入图片描述

临时切换风格

虽然能够经过set_style()来切换风格,但还可利用axes_style临时设置绘图风格。

plt.figure(figsize=(5,5))
with sns.axes_style("darkgrid"):
    explot()
plt.figure(figsize=(5,5))
explot()

在这里插入图片描述

更改seaborn风格参数

固然还能够根据本身喜爱来修改axes_style()和set_style()的参数,这里须要传递的是一个字典参数,咱们能够看看axes_style()里有哪些字典参数,

sns.axes_style()
{'axes.facecolor': 'white',
 'axes.edgecolor': '.15',
 'axes.grid': False,
 'axes.axisbelow': True,
 'axes.labelcolor': '.15',
 'figure.facecolor': 'white',
 'grid.color': '.8',
 'grid.linestyle': '-',
 'text.color': '.15',
 'xtick.color': '.15',
 'ytick.color': '.15',
 'xtick.direction': 'out',
 'ytick.direction': 'out',
 'lines.solid_capstyle': 'round',
 'patch.edgecolor': 'w',
 'image.cmap': 'rocket',
 'font.family': ['sans-serif'],
 'font.sans-serif': ['Arial',
  'DejaVu Sans',
  'Liberation Sans',
  'Bitstream Vera Sans',
  'sans-serif'],
 'patch.force_edgecolor': True,
 'xtick.bottom': True,
 'xtick.top': False,
 'ytick.left': True,
 'ytick.right': False,
 'axes.spines.left': True,
 'axes.spines.bottom': True,
 'axes.spines.right': True,
 'axes.spines.top': True}

因而就能够设置本身想要的参数

sns.set_style('white',{"axes.facecolor": ".7"})
x = np.linspace(0, 1, 100)
for i in range(1, 7):
    plt.plot(x, np.cos(x + i * .5) * (7 - i) * 0.1 + np.random.random(1))

在这里插入图片描述

图形缩放

在四个预置环境中,paper、notebook、talk、poster按大小从小到大依次排列,

sns.set()
list = ['paper', 'notebook', 'talk', 'poster']
plt.figure(figsize=(15,15))
x = np.linspace(0, 1, 100)
for k in range(0,4):
    s = '22'+str(k+1)
    plt.subplot(int(s))
    with sns.set_context(list[k])for i in range(1, 7):
       		plt.plot(x, np.cos(x + i * .5) * (7 - i) * 0.1)
    	plt.xlabel(list[k])

在这里插入图片描述
也可使用with语句,对应plotting_context()设置临时的缩放控制

plt.figure(figsize=(15,10))
x = np.linspace(0, 1, 100)
with sns.plotting_context('poster'):
    plt.subplot(121)
    for i in range(1, 7):
        plt.plot(x, np.cos(x + i * .5) * (7 - i) * 0.1)
plt.subplot(122)
for i in range(1, 7):
    plt.plot(x, np.cos(x + i * .5) * (7 - i) * 0.1)

在这里插入图片描述
结语
本文从绘图的风格控制函数和图形缩放上进行了讲解,下面将对颜色的空值进行介绍。
谢谢阅读。
参考
路远seaborn教程
seaborn文档