使用seaborn设置图像显示效果参数

前言

Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,并且更加炫酷。

demo

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
import seaborn as sns

x = np.linspace(0, 14, 100)
y1 = np.sin(x)
y2 = np.sin(x+2) * 1.25

style = ['darkgrid', 'dark', 'white', 'whitegrid', 'ticks']

# 1. axes_style and set_style

sns.set_style(style[0])
#print( sns.axes_style() )
''' 可以设置一些 显示 细节
{'xtick.color': '.15', 
'font.family': ['sans-serif'], 
'lines.solid_capstyle': 'round', 
'ytick.left': False, 
'axes.axisbelow': True, 
'font.sans-serif': ['Arial', 'DejaVu Sans', 'Liberation Sans', 'Bitstream Vera Sans', 'sans-serif'], 
'axes.spines.left': True, 
'image.cmap': 'rocket', 
'grid.color': 'white', 
'axes.edgecolor': 'white', 
'text.color': '.15', 
'axes.grid': True, 
'xtick.top': False, 
'xtick.direction': 'out', 
'patch.edgecolor': 'w', 
'axes.spines.bottom': True, 
'figure.facecolor': 'white', 
'axes.spines.right': True, 
'axes.spines.top': True, 
'grid.linestyle': '-', 
'ytick.color': '.15', 
'axes.facecolor': '#EAEAF2', 
'patch.force_edgecolor': True, 
'ytick.right': False, 
'axes.labelcolor': '.15', 
'xtick.bottom': False, 
'ytick.direction': 'out'}

'''
# 使用方法如下
sns.set_style(style[0], {'grid.color': 'red',})
# 作为一个字典 传入,显示的背景网格就是红色的

sns.set() # 可以清空所有样式
sns.lineplot(x, y1)
sns.lineplot(x, y2)

plt.show() # 图1


# 2. plotting_context() and set_context()
context = ['paper', 'notebook', 'talk', 'poster']
sns.set_context(context[0], rc = {'grid.linewidth': 3.0}) # 设置整个表的风格
# 同样,可以使用 sns.set() 进行样式的取消

sns.lineplot(x, y1)
sns.lineplot(x, y2)

plt.show() # 图2

# 可以看出,进入seaborn之后,使用plt也可以输出和seaborn一样的效果
plt.plot(x, y1)
plt.show() # 图3

画图如下:
图1:
在这里插入图片描述
图2:
在这里插入图片描述
图3:
在这里插入图片描述

可以看到seaborn的画图操作成为简单,并且更为美观。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值