import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(sum(map(ord, "aesthetics")))
%matplotlib inline
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() # plot with matplotlib defaults
import seaborn as sns
sinplot() # plot with seaborn defaults
Seaborn中有两种类型的函数(第一个函数返回参数当前设置,第二个函数设置参数)
管理图表样式
设置布局元素
Seaborn有五个预设好的主题:darkgrid, whitegrid, dark, white,和ticks
默认主题是darkgrid,白线灰底让网络不会影响代表数据的线的显示
sinplot()
whitegrid主题非常简洁,更适用于数据元素较大的布局
sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x9d5e780>
对于大多数布局,(尤其是当你主要想要利用图表来提供给人对于数据模式的印象),网格便不那么重要了.
sns.set_style("dark")
sinplot()
sns.set_style("white")
sinplot()
有时你可能想在布局上添加一些额外的结构,比如说将轴线分割成线段的ticks
sns.set_style("ticks")
sinplot()
主题white和ticks都可以通过去除上方和右方不必要的轴线,通过调用seaborn的函数despine()来去除轴线
sns.set_style("ticks")
sinplot()
sns.despine()
# 有些布局也可以通过调整轴线距数据的偏移来改善,这也能在despine()里完成,设置offset参数
# 当ticks不能覆盖轴线的整个范围时,trim参数可以限制显示的轴线的范围
sns.set_style("white")
sns.violinplot(data=data)
sns.despine(offset=10, trim=True) # offset调整轴线与图形的距离,trim=True限制轴线的范围(从0到5)
# 控制移除哪条轴线
sns.set_style("white")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True) # 默认移除上侧、右侧轴线
在with语句里使用axes_style()函数来临时设置控制布局的参数.这也允许你用不同的风格来制作图表
with sns.axes_style("darkgrid"):
plt.subplot(211)
sinplot()
plt.subplot(212)
sinplot(-1) # 参数-1使得画出的图形相对于x轴对称
seaborn.set_style(style=None, rc=None)
A dictionary of parameters or the name of a preconfigured set.
Parameter mappings to override the values in the preset seaborn style dictionaries. This only updates parameters that are considered part of the style definition.
sns.axes_style() # 调用没有参数的函数,返回所有参赛的当前设置
{'axes.axisbelow': True, 'axes.edgecolor': '.15', 'axes.facecolor': 'white', 'axes.grid': False, 'axes.labelcolor': '.15', 'axes.linewidth': 1.25, 'figure.facecolor': 'white', 'font.family': [u'sans-serif'], 'font.sans-serif': [u'Arial', u'Liberation Sans', u'Bitstream Vera Sans', u'sans-serif'], 'grid.color': '.8', 'grid.linestyle': u'-', 'image.cmap': u'Greys', 'legend.frameon': False, 'legend.numpoints': 1, 'legend.scatterpoints': 1, 'lines.solid_capstyle': u'round', 'text.color': '.15', 'xtick.color': '.15', 'xtick.direction': u'out', 'xtick.major.size': 0.0, 'xtick.minor.size': 0.0, 'ytick.color': '.15', 'ytick.direction': u'out', 'ytick.major.size': 0.0, 'ytick.minor.size': 0.0}
sns.set_style("darkgrid", {"axes.facecolor": "c"}) # 用dict将一系列控制参数赋值给axes_style()函数和set_style()函数的rc参数
sinplot()
seaborn.set_context(context=None, font_scale=1, rc=None)
A dictionary of parameters or the name of a preconfigured set.
Separate scaling factor to independently scale the size of the font elements.
Parameter mappings to override the values in the preset seaborn context dictionaries. This only updates parameters that are considered part of the context definition.
sns.set() # 重置context默认设置
有4种布局(context),按相对大小排序分别是:paper, notebook, talk,和poster
布局设置影响标题(label)、线型(lines)和其他一些绘图元素,默认布局是notebook,四种布局分别将参数设置为0.8,1,1.3,1.6
sns.set_context("paper")
plt.figure(figsize=(8, 6))
sinplot()
sns.set_context("notebook")
plt.figure(figsize=(8, 6))
sinplot()
sns.set_context("talk")
plt.figure(figsize=(8, 6))
sinplot()
sns.set_context("poster")
plt.figure(figsize=(8, 6))
sinplot()
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5}) # 设置context、字体大小以及其他参数
sinplot()
在with语句里使用plotting_context()函数来临时设置控制布局的参数.这也允许你用不同的风格来制作图表
with sns.plotting_context("paper"):
plt.subplot(211)
sinplot()
plt.subplot(212)
sinplot(-1) # 参数-1使得画出的图形相对于x轴对称