前言
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的画图操作成为简单,并且更为美观。