1、艺术化的图表控制。python绘图库Seaborn

导入库

%matplotlib inline 
#jupyter notebook 中的魔法函数,如果不是使用该软件请使用plt.show()用于显示图像
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(sum(map(ord,"aesthetics")))  # 定义种子

1、定义一个含偏移的正弦图像,来比较传统的matplotlib和seaborn的不同:

def sinplot(flip=1):
    x = np.linspace(0,14,100)
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*.5)*(7-i)*flip)
sinplot()

在这里插入图片描述
此图为普通matplotlib绘制图形

2、使用seaborn绘图:

import seaborn as sns
sinplot()#看上去和上面毫无区别,使用了seaborn默认风格

在这里插入图片描述

3、使用sns.set_style(“whitegrid”)可以选择五种主题风格5种主题风格

darkgrid
whitegrid
dark
white
ticks

sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)

在这里插入图片描述

sns.set_style("dark")
sinplot()

dark

sns.set_style("ticks")#四边加刻度线,但是并没有加上,与官方文档不符。
sinplot()

在这里插入图片描述

删除边框要使用despine()函数

sns.set_style("white")
sinplot() 
sns.despine()# 默认删除上方和右方的边框

在这里插入图片描述

4、使用despine可以删除边框或控制四个边框移动

sns.violinplot(data=data)#小提琴图
sns.despine(offset=10, trim=True);#四个边框到原位置距离为10,top与right参数默认为True,表示不显示,left与bottom默认参数为false,表示显示边框
sns.despine(offset={'left':50,'right':10,'top':20,'bottom':50},top=False, right=False,trim=True); # 四个边框到原位置的距离;top与right参数默认为True

在这里插入图片描述

sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True) # 删除左边边框,上边和右边默认删除

在这里插入图片描述

5、设置样式除了使用set_style(),也可以使用axes_style(),一般的,axes_style()与with语句搭配使用。

如:

# sns也允许用with语句中套用axes_style()达到临时设置参数的效果(仅对with块内的绘图函数起作用)。这也允许创建不同风格的坐标轴。
with sns.axes_style("darkgrid"):#with语句使用"darkgrid"风格,注意,sns.axes_style一般与with共用,sns.axes_style为with准备
    plt.subplot(211)
    sinplot()
plt.subplot(212)#with语句外的图依然用"whitegrid"风格
sinplot(-1)

在这里插入图片描述
axes_style()与set_style()的对比:
相同点:
1、均可用来定制seanborn样式,参数一样,都有(style=None, rc=None)两个参数,参数字典传递给axes_style()和set_style()的rc参数可实现定制样式
不同点:
1、返回值不同,axes_style()返回的是dict数据(包含了其样式),set_style()返回的是none值
2、axes_style()与with语句搭配使用,而set_style()可直接设置样式。

如果您想要定制seanborn的样式,可以将参数字典传递给axes_style()和set_style()的rc参数。
注意,只能通过该方法覆盖样式定义的一部分参数。
(然而,更高层次的set()函数接受任何matplotlib参数的字典)。

sns.set()#重置seanborn参数,全局设置为默认样式了。
for d in sns.axes_style("darkgrid").items(): #查看默认样式   
    print(d)
sinplot()
'''
('figure.facecolor', 'white')
('axes.labelcolor', '.15')
('xtick.direction', 'out')
('ytick.direction', 'out')
('xtick.color', '.15')
('ytick.color', '.15')
('axes.axisbelow', True)
('grid.linestyle', '-')
('text.color', '.15')
('font.family', ['sans-serif'])
('font.sans-serif', ['Arial', 'DejaVu Sans', 'Liberation Sans', 'Bitstream Vera Sans', 'sans-serif'])
('lines.solid_capstyle', 'round')
('patch.edgecolor', 'w')
('patch.force_edgecolor', True)
('image.cmap', 'rocket')
('xtick.top', False)
('ytick.right', False)
('axes.grid', True)
('axes.facecolor', '#EAEAF2')
('axes.edgecolor', 'white')
('grid.color', 'white')
('axes.spines.left', True)
('axes.spines.bottom', True)
('axes.spines.right', True)
('axes.spines.top', True)
('xtick.bottom', False)
('ytick.left', False)
'''

在这里插入图片描述
如何对其中的某一项参数调整?可以在set_style()或sns.axes_style()的rc参数中传入字典进行调整

sns.set_style("darkgrid", {"axes.facecolor": ".17"})
sinplot()

在这里插入图片描述

6、set_context()#设置图片中文字标签线条等大小的函数,参数有四个,分别是paper,notebook, talk, and poster

# sns.set_context()#设置图片中文字标签线条等大小的函数,参数有四个,分别是paper,notebook, talk, and poster。notebook的样式
plt.figure(figsize=(10,18))

sns.set()#重置参数

sns.set_context("paper")
plt.subplot(511)
sinplot(-1)

sns.set_context("notebook")
plt.subplot(512)
sinplot(-1)

sns.set_context("talk")
plt.subplot(513)
sinplot(-1)

sns.set_context("poster")
plt.subplot(514)
sinplot(-1)

sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})#更改context还可以独立地扩展字体元素的大小。(这个选项也可以通过顶级set()函数获得)
plt.subplot(515)
sinplot(-1)

在这里插入图片描述
默认参数为notebook

# default 默认设置
sns.set_context()#同sns.set_context("notebook")
plt.figure(figsize=(8,6))
sinplot()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值