数据可视化:Seaborn

安装Seaborn

进入虚拟环境,在终端中键入

pip install seaborn

即可安装。

初步使用Seaborn

在使用seaborn之前,我们先了解一下seaborn是什么,seaborn是以matplotlib为底层的更简便的python第三方库,它可以更快捷地去设置图形的一些参数,让图表元素改变比例,颜色,背景等等的工具包,使用Seaborn可以给我们美化的图表。

一,风格设置

除了各种绘图方式外,图形的美观程度可能是我们最关心的了。将它放到第一部分,因为风格设置是一些通用性的操作,对于各种绘图方法都适用。  

 

Seaborn 支持的风格有5种:

  • darkgrid 黑背景-白格
  • whitegrid 白背景-白格
  • dark 黑背景
  • white 白背景
  • ticks  

接下来,我们将用代码演示一下各自的效果。

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


#seaborn风格设置
y = np.random.randint(50,100,20)
x = range(20)
sns.set_style('white')
plt.subplot(231)
plt.plot(x,y)
sns.set_style('dark')
plt.subplot(232)
plt.plot(x,y)
sns.set_style('whitegrid')
plt.subplot(233)
plt.plot(x,y)
sns.set_style('darkgrid')
plt.subplot(234)
plt.plot(x,y)
sns.set_style('ticks')
plt.subplot(235)
plt.plot(x,y)


plt.show()

 

 

 

 二,移除轴脊柱

white和ticks两个风格都能够移除顶部和右侧的不必要的轴脊柱。通过matplotlib参数是做不到这一点的,但是你可以使用 seaborn的despine() 方法来移除它们

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


#seaborn风格设置
y = np.random.randint(50,100,20)
x = range(20)
sns.set_style('white')
plt.subplot(231)
plt.plot(x,y)
sns.set_style('dark')
plt.subplot(232)
plt.plot(x,y)
sns.set_style('whitegrid')
plt.subplot(233)
plt.plot(x,y)
sns.set_style('darkgrid')
plt.subplot(234)
plt.plot(x,y)
sns.set_style('ticks')
plt.subplot(235)
plt.plot(x,y)
sns.despine()



plt.show()

 

 三,图像风格管理

简单来说,这就是修改图像的规格:粗细,大小等属性,我们调用seanborn.set_context()方法来设置。

 

同样地,我们使用代码来演示效果:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

x = range(10)
y = np.random.randint(10,100,10)

#调用set_context来设置
plt.subplot(221)
plt.plot(x,y)
sns.set_context("paper")
plt.subplot(222)
plt.plot(x,y)
sns.set_context("notebook")
plt.subplot(223)
plt.plot(x,y)
sns.set_context("talk")
plt.subplot(224)
plt.plot(x,y)
sns.set_context("poster")

plt.show()

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值