做者:xiaoyu
微信公众号:Python数据科学
知乎:python数据分析师python
最近在作几个项目的数据分析,每次用到seaborn
进行可视化绘图的时候老是忘记具体操做。虽然seaborn
的官方网站已经详细的介绍了使用方法,可是毕竟是英文,并且查找不是很方便。所以博主想从零开始将seaborn
学习一遍,作一个总结,也但愿供你们使用参考。
seaborn
同matplotlib
同样,也是Python进行数据可视化分析的重要第三方包。但seaborn
是在 matplotlib
的基础上进行了更高级的API封装,使得做图更加容易,图形更加漂亮。微信
博主并不认为seaborn
能够替代matplotlib
。虽然seaborn
能够知足大部分状况下的数据分析需求,可是针对一些特殊状况,仍是须要用到matplotlib
的。换句话说,matplotlib
更加灵活,可定制化,而seaborn
像是更高级的封装,使用方便快捷。dom
应该把seaborn
视为matplotlib
的补充,而不是替代物。post
seaborn
的学习内容主要包含如下几个部分:学习
风格管理字体
绘图方法网站
结构网格人工智能
本次将主要介绍风格管理的使用。spa
除了各类绘图方式外,图形的美观程度多是咱们最关心的了。将它放到第一部分,由于风格设置是一些通用性的操做,对于各类绘图方法都适用。3d
让咱们先看一个例子。
%matplotlib inline import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns np.random.seed(sum(map(ord, "aesthetics")))
咱们定义了一个简单的方程来绘制一些偏置的正弦波,用来帮助咱们查看不一样的图画风格是什么样子的。
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)
matplotlib
默认参数下绘制结果是这样的:
sinplot()
转换为seaborn
默认绘图,能够简单的用set()
方法。
sns.set() sinplot()
Seaborn
将 matplotlib
的参数划分为两个独立的组合。第一组是设置绘图的外观风格的,第二组主要将绘图的各类元素按比例缩放的,以致能够嵌入到不一样的背景环境中。
操控这些参数的接口主要有两对方法:
axes_style()
, set_style()
plotting_context()
, set_context()
每对方法中的第一个方法(axes_style()
, plotting_context()
)会返回一组字典参数,而第二个方法(set_style()
, set_context()
)会设置matplotlib的默认参数。
有五种seaborn
的风格,它们分别是:darkgrid, whitegrid, dark, white, ticks。它们各自适合不一样的应用和我的喜爱。默认的主题是darkgrid。
sns.set_style("whitegrid") data = np.random.normal(size=(20, 6)) + np.arange(6) / 2 sns.boxplot(data=data);
sns.set_style("dark") sinplot()
sns.set_style("white") sinplot()
sns.set_style("ticks") sinplot()
white 和 ticks两个风格都可以移除顶部和右侧的没必要要的轴脊柱。经过matplotlib
参数是作不到这一点的,可是你能够使用seaborn
的despine()
方法来移除它们:
sinplot() sns.despine()
一些绘图也能够针对数据将轴脊柱进行偏置,固然也是经过调用despine()
方法来完成。而当刻度没有彻底覆盖整个轴的范围时,trim
参数能够用来限制已有脊柱的范围。
f, ax = plt.subplots() sns.violinplot(data=data) sns.despine(offset=10, trim=True);
你也能够经过despine()
控制哪一个脊柱将被移除。
sns.set_style("whitegrid") 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)
若是你想定制化seaborn
风格,你能够将一个字典参数传递给axes_style()
和set_style()
的参数rc
。并且你只能经过这个方法来覆盖风格定义中的部分参数。
若是你想要看看这些参数都是些什么,能够调用这个方法,且无参数,这将会返回下面的设置:
sns.axes_style() {'axes.axisbelow': True, 'axes.edgecolor': '.8', 'axes.facecolor': 'white', 'axes.grid': True, 'axes.labelcolor': '.15', 'axes.linewidth': 1.0, 'figure.facecolor': 'white', 'font.family': [u'sans-serif'], 'font.sans-serif': [u'Arial', u'DejaVu Sans', u'Liberation Sans', u'Bitstream Vera Sans', u'sans-serif'], 'grid.color': '.8', 'grid.linestyle': u'-', 'image.cmap': u'rocket', '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": ".9"}) sinplot()
有一套的参数能够控制绘图元素的比例。
首先,让咱们经过set()
重置默认的参数:
sns.set()
有四个预置的环境,按大小从小到大排列分别为:paper, notebook, talk, poster。其中,notebook是默认的。
sns.set_context("paper") sinplot()
sns.set_context("talk") sinplot()
sns.set_context("poster") sinplot()
你能够经过使用这些名字中的一个调用set_context()
来设置参数,而且你能够经过提供一个字典参数值来覆盖参数。当改变环境时,你也能够独立的去缩放字体元素的大小。
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5}) sinplot()
一样的,你能够经过嵌入with
语句临时的控制绘图的比例。
介绍了Seaborn的5中绘图风格
下一节将会介绍颜色风格的使用。
关注微信公众号Python数据科学,获取 120G
人工智能 学习资料。