Seaborn是基于matplotlib的Python可视化库。 它提供了一个高级界面来绘制有吸引力的统计图形。Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,不需要经过大量的调整就能使你的图变得精致。但应强调的是,应该把Seaborn视为matplotlib的补充,而不是替代物。

一、整体布局风格设置

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
%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*0.5) * (7 - i) * flip) sinplot()#采用的是matplot默认的一些参数画图

#接下来采用的是seaborn的画图的风格,首先看看默认的变化
sns.set()
sinplot()

#seaborn主要分为五种主题风格:darkgrid whitegrid dark white ticks
sns.set_style("whitegrid") data = np.random.normal(size=(20, 6)) + np.arange(6) / 2 sns.boxplot(data=data)

高斯分布的概率密度函数

numpy中

numpy.random.normal(loc=0.0, scale=1.0, size=None)

参数的意义为:

  loc:float

  概率分布的均值,对应着整个分布的中心center

  scale:float

  概率分布的标准差,对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高

  size:int or tuple of ints

  输出的shape,默认为None,只输出一个值

  我们更经常会用到np.random.randn(size)所谓标准正太分布(μ=0, σ=1),对应于np.random.normal(loc=0, scale=1, size)

所以上面从正态分布随机产生20*6的二维数组,加上后面的6*1的一维数组。

boxplot 箱线图

"盒式图" 或叫 "盒须图" "箱形图",,其绘制须使用常用的统计量,能提供有关数据位置和分散情况的关键信息,尤其在比较不同的母体数据时更可表现其差异。

如上图所示,标示了图中每条线表示的含义,其中应用到了分位值(数)的概念。

主要包含五个数据节点,将一组数据从大到小排列,分别计算出他的上边缘,上四分位数,中位数,下四分位数,下边缘。

具体用法如下:seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)

Parameters:

x, y, hue: names of variables in data or vector data, optional #设置 x,y 以及颜色控制的变量

Inputs for plotting long-form data. See examples for interpretation.

data : DataFrame, array, or list of arrays, optional #设置输入的数据集

Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

order, hue_order : lists of strings, optional #控制变量绘图的顺序

Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

sns.set_style("dark")

sinplot()

sns.set_style("white")

sinplot()

sns.set_style("ticks")

sinplot()

sinplot()

sns.despine() #在ticks基础上去掉上面的和右边的端线

二、风格细节的设置

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline #这句话的意思就是可以直接显示图
sns.violinplot(data)
sns.despine(offset=30) #显示的是图形的最低点距横轴的距离

sns.set_style("whitegrid")

sns.boxplot(data=data, palette="deep")

sns.despine(left = True) #把左边的轴去掉了。

with sns.axes_style("darkgrid"): #通过with可以显示一种一个子图的风格。
plt.subplot(211)
sinplot() plt.subplot(212)
sinplot(-1)

#另一种调节图中线的粗细程度的风格:paper talk poster notebook,前三种风格的线越来越粗,最后一个notebook是可以通过参数的修改的
sns.set() sns.set_context("paper") 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 = 2, rc={"lines.linewidth" : 4.5}) #这个是可以的设置的参数的,font_scale是改变坐标轴上的大小,rc中的line.linewidth是改变线的粗细

plt.figure(figsize=(8, 6))

sinplot()

三、调色板

color_palette()能传入任何Matplotlib所支持的颜色

set_palette()设置所有图的颜色

#显示默认的颜色 deep, muted, pastel, bright, dark, colorblind......
current_palette = sns.color_palette() sns.palplot(current_palette)

圆形画板

当需要更多分类要区分时,最简单的方法就是在一个圆形的颜色空间中画出均匀间隔的颜色(这样的色调会保持亮度和饱和度不变)。这是大多数的需要使用的比默认颜色循环中的设置的颜色更多的默认方案。

最常用的方法是使用hls的颜色空间,这是RGB值的一个简单转换。

#比如我想画个12个不同的颜色
sns.palplot(sns.color_palette("hls", 12))

data = np.random.normal(size = (20, 8)) + np.arange(8) / 2

sns.boxplot(data = data, palette = sns.color_palette("hls", 8))

hls_palette()函数来控制颜色的亮度和饱和度

l------亮度 lightness

s------饱和 saturation

sns.palplot(sns.hls_palette(8, l = 0.3, s = 0.8))

sns.palplot(sns.color_palette("Paired", 8)) #Paired意思是比如8就是4对颜色,每一对颜色都是一浅一深。

使用xkcd颜色来命名颜色

xkcd包含了一套众包努力的针对随机RGB色的命名。产生了954个可以随机通过xkcd_rgb字典中调用的命名颜色。

plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3)

plt.plot([0, 1], [0, 2], sns.xkcd_rgb["medium green"], lw=3)

plt.plot([0, 1], [0, 3], sns.xkcd_rgb["denim blue"], lw=3)

#其他xkcd的一些颜色
colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"] sns.palplot(sns.xkcd_palette(colors))

连续色板

色彩随数据变换,比如数据越来越重要则颜色越来越深

sns.palplot(sns.color_palette("Blues"))

如果想要翻转渐变,可以在面板名称中添加一个_r后缀

sns.palplot(sns.color_palette("BuGn_r"))

cubehelix_palette()调色板

色调线性变换

sns.palplot(sns.color_palette("cubehelix", 8))

sns.palplot(sns.cubehelix_palette(8, start = 0.5, rot = -0.75))

sns.palplot(sns.cubehelix_palette(8, start = 0.75, rot = -0.150))

light_palette()和dark_palette()调用定制连续调色板

sns.palplot(sns.light_palette("green"))

sns.palplot(sns.dark_palette("purple"))

sns.palplot(sns.light_palette("navy", reverse=True))

四、单变量分析绘图

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy import stats, integrate
%matplotlib inline x = np.random.normal(size = 100) #正态分布 sns.distplot(x, kde=False) #绘制直方图

sns.distplot(x, bins = 20, kde = False) #通过bins可以改变条的数目

x = np.random.gamma(6, size = 200)

sns.distplot(x, kde = False, fit = stats.gamma) #通过fit进行拟合

根据均值和协方差生成数据

#生成两个变量是DateFrame格式的
mean, cov = [0, 1], [(1, 0.5), (0.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200) df = pd.DataFrame(data, columns=["x", "y"]) df

观测两个变量之间的分布关系最好用散点图

sns.jointplot(x="x", y ="y", data=df);

x, y = np.random.multivariate_normal(mean, cov, 1000).T

with sns.axes_style("white"):
sns.jointplot(x=x, y=y, kind="hex", color='k')

seaborn模块的基本使用的更多相关文章

  1. 数据分析 - seaborn 模块

    seaborn 模块 简述 对 matplotlib 模块进行了二次封装, 底层依旧使用还是 matplotlib 的, 但是在此基础上增加了很多的易用性模板, 更加方便使用 引用使用 import ...

  2. Pandas模块

    前言: 最近公司有数据分析的任务,如果使用Python做数据分析,那么对Pandas模块的学习是必不可少的: 本篇文章基于Pandas 0.20.0版本 话不多说社会你根哥!开干! pip insta ...

  3. seaborn教程1——风格选择

    原文链接:https://segmentfault.com/a/1190000014915873 Seaborn学习大纲 seaborn的学习内容主要包含以下几个部分: 风格管理 绘图风格设置 颜色风 ...

  4. Python绘图与可视化

    Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...

  5. Python的pandas

    pandas 是python中很重要的组件,网上关于pandas 的文章也很多,比如Python科学计算之Pandas 和 Python数据分析入门 Pandas基于两种数据类型:series与dat ...

  6. Python数据分析入门

    Python数据分析入门 最近,Analysis with Programming加入了Planet Python.作为该网站的首批特约博客,我这里来分享一下如何通过Python来开始数据分析.具体内 ...

  7. 吴裕雄 数据挖掘与分析案例实战(5)——python数据可视化

    # 饼图的绘制# 导入第三方模块import matplotlibimport matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['S ...

  8. python绘图 转

    Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...

  9. Python运用于数据分析的简单教程

    Python运用于数据分析的简单教程 这篇文章主要介绍了Python运用于数据分析的简单教程,主要介绍了如何运用Python来进行数据导入.变化.统计和假设检验等基本的数据分析,需要的朋友可以参考下 ...

随机推荐

  1. Jenkins控制台乱码修改

    原文地址:https://www.jianshu.com/p/8b9df45df401 方案一. 设置jenkins所在服务器环境变量,右键我的电脑→属性→高级系统设置→环境变量,添加JAVA_TOO ...

  2. 034 Android NavigationView和DrawerLayout实现抽屉式导航设计(侧边栏效果)

    1.创建带侧滑效果的activity 右击,new---->activity---->选择NavgationDrawer Activity 2.xml文件布局 (1)activity_ma ...

  3. [.Net] 一句话Linq(递归查询)

    功能查询起止日期范围内连续的月份列表. /* Period */ cbxPeriod.DataSource = Enumerable.Range(, ).Select(t => DateTime ...

  4. lnmp二级域名配置相关

    阿里云那域名解析那有误读 我在偏远的电信网选择中国联通解析死活解析不出来 以上这么配置就对了....选择默认.瞬间解析出来.... 出于对nginx 配置不够熟悉 后来一点点理出来. 不带www 也正 ...

  5. 多线程(7)— JDK对锁优化的努力

    JDK内部的“锁”优化策略 1. 锁偏向 锁偏向是针对加锁操作的优化手段,核心思想是:如果一个线程获得了锁,那么锁就进入偏向模式,当这个线程再次请求锁时,无须再做任何同步操作,这样就节省了大量有关锁申 ...

  6. Spring+SpringMVC+Mybatis(SSM)框架集成搭建

    Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...

  7. 14 IF函数

    情景 某销售表,如果员工的销售额大于5000元,提成为10%,否则为8% IF函数 =IF(表达式,值1,值2) 判断表达式,如果表达式为真,显示值1,如果表达式为假,显示值2 示例

  8. unbantu...

    待更新装个中文输入法装半天,还不好用,难受 注销到一个语句 sudo systemctl restart lightdm

  9. 【转载】Sqlserver根据生日计算年龄

    在Sqlserver中,可以根据存储的出生年月字段计算出该用户的当前年龄信息,主要使用到DateDiff函数来实现.DateDiff函数的格式为DATEDIFF(datepart,startdate, ...

  10. Machine Learning Technologies(10月20日)

    Linear regression SVM(support vector machines) Advantages: ·Effective in high dimensional spaces. ·S ...