Plot aesthetics主题和调色板(seaborn)

学习seaborn库最好的资料就是它的官网,seaborn官方网站,它的tutorial指南分为三部分:plotting function(画图的函数),multi-plot grid(多图网格),plot asethetics(图形美学)。plot asethetics又分为Controlling figure aesthetics(控制图像美学)和Choosing color palettes(选择调色板)。

看英语的时候会经常觉得,没有合适的词汇可以表达它的意思。推荐大家直接去官网看tutorial,认真看的话,大多应该是知道在讲什么的,实在不行,也可以找翻译软件。

Controlling figure aesthetics

在这里插入图片描述

seaborn figure aesthetics

seaborn有五个预先配置好的主题,分别为:darkgrid, whitegrid, dark, white, and ticks,它们适合于不同的应用和个人偏好,默认使用的主题为darkgrid。下面通过图形来看不同主题时的图形。

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["axes.unicode_minus"]=False#用来正常显示负号

sns.set_style()#默认为darkgrid
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述
官网上讲,下面的这种whitegrid主题更适合heavy data 元素的绘图,我理解为数据变量多。

sns.set_style("whitegrid")#设置theme为whitegrid
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

sns.set_style("dark")#设置theme为dark
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

sns.set_style("white")#设置theme为white
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

sns.set_style("ticks")#设置theme为ticks
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

Removing axes spines

在theme设置为white或者ticks时,我们可以通过函数去除它的axes spines
在这里插入图片描述
sns.despine()前面几个参数比较好理解,最后一个参数trim:如果为 True,则将脊柱限制为每个非旋转轴上最小和最大的主刻度。

sns.set_style("ticks")
plt.bar([1,2,3,4],[24,34,12,23])
sns.despine()   #默认top=True,right=True,去除上边和右边的spines

在这里插入图片描述

sns.set_style("ticks")
plt.bar([1,2,3,4],[24,34,12,23])
sns.despine(trim=True)

在这里插入图片描述

Overriding elements of the seaborn styles

我们可以通过下面的代码查看style

sns.axes_style()

Out:

{'axes.facecolor': 'white',
 'axes.edgecolor': '.15',
 'axes.grid': False,
 'axes.axisbelow': True,
 'axes.labelcolor': '.15',
 'figure.facecolor': 'white',
 'grid.color': '.8',
 'grid.linestyle': '-',
 'text.color': '.15',
 'xtick.color': '.15',
 'ytick.color': '.15',
 'xtick.direction': 'out',
 'ytick.direction': 'out',
 'lines.solid_capstyle': 'round',
 'patch.edgecolor': 'w',
 'image.cmap': 'rocket',
 'font.family': ['sans-serif'],
 'font.sans-serif': ['Arial',
  'DejaVu Sans',
  'Liberation Sans',
  'Bitstream Vera Sans',
  'sans-serif'],
 'patch.force_edgecolor': True,
 'xtick.bottom': True,
 'xtick.top': False,
 'ytick.left': True,
 'ytick.right': False,
 'axes.spines.left': True,
 'axes.spines.bottom': True,
 'axes.spines.right': True,
 'axes.spines.top': True}

我们可以自己通过传入参数去设置自己想要的样式

sns.set_style("darkgrid", {"axes.facecolor": ".9"})

Scaling plot elements

seaborn有四个预先配置好的context,分别为:paper, notebook, talk, and poster。默认的context为notebook

sns.set_context("paper")#设置context为paper
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

sns.set_context()#默认context为notebook
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

sns.set_context("talk")#设置context为talk
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述

sns.set_context("poster")#设置context为poster
plt.bar([1,2,3,4],[24,34,12,23])

在这里插入图片描述
跟上面设置style差不多,我们同样可以通过给函数传参得到自己想要的context

sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})

Choosing color palettes

在这里插入图片描述
seanborn的tutorial给大家列举出来了几个学习色彩好的资源,下面我有用矩形框出来,感兴趣可以去看下。
在这里插入图片描述

Building color palettes

sns.color_palette()需要我们掌握,它可以接收任何seaborn调色板的名字,以及matplotlib的colormap,它还可以接收色彩的列表,其中颜色可以用十六进制、RGB或者HTML 颜色的名字表示。下面是这个函数的参数和返回结果。
其中第三个参数是每种颜色去饱和的程度(saturation的意思是饱和度),饱和度高的时候画面颜色会非常鲜艳,饱和度为0的时候为黑白色。
在这里插入图片描述

Qualitative color palettes(定性调色板)

当要区分没有固有排序的离散数据块时,定性(或分类)调色板是最佳的。
有六种默认的主题,调色板中的颜色见下图,纵坐标是亮度,横坐标是饱和度。我们可以看出默认的调色板,只是改变了颜色的亮度和饱和度,使用的色相都是一样的。
在这里插入图片描述

Using circular color systems

当要区分类别而且不强调任何类别的时候,最简单的方法是绘制均匀间隔的颜色,在保持饱和度和亮度不变的情况下,改变色相。

sns.palplot(sns.hls_palette(10, l=.5, s=0.4))    #可以自己控制亮度luminance和饱和度saturation。

在这里插入图片描述

sns.palplot(sns.hls_palette(6, l=.8, s=0.4))

在这里插入图片描述

Using categorical Color Brewer palettes

令人愉悦的分类调色板的另一个来源来自Color Brewer工具,点击这里,可以跳到Color Brewer网站Color Brewer的一个很好的特性是,它提供了一些关于哪些调色板是色盲安全的指导。
下面的函数可以让我们使用Color Brewer中的调色板,并且还是交互式的,真的很强大了!但是值得注意的是,这个函数只能在Jupyter notebook中使用。
在这里插入图片描述

sns.choose_colorbrewer_palette("sequential")

在这里插入图片描述

Using named colors from the xkcd color survey

xkcd发起了一项众包活动,为随机的RGB颜色命名。这产生了一组954命名的颜色,我们现在可以在seaborn中使用xkcd_rgb字典引用。

colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
sns.palplot(sns.xkcd_palette(colors))

在这里插入图片描述

Sequential color palettes(顺序调色板)

1.当数据范围从相对较低或不感兴趣的值到相对较高或感兴趣的值时,这种颜色映射是合适的。
2.当使用kdeplot() and heatmap()这两个函数时,会经常选择这种顺序调色板。
3.对于顺序数据,最好使用色调变化相对微妙的调色板,同时亮度和饱和度有较大变化。这种方法自然会吸引到数据中相对重要的部分

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

在这里插入图片描述

sns.palplot(sns.color_palette("Purples",10))

在这里插入图片描述

Sequential “cubehelix” palettes

The cubehelix color palette system使连续调色板的亮度线性增加或减少,色调的一些变化。这意味着当图像被转换成黑白(用于打印)或被色盲的人看到时,它将被保存下来。
感兴趣的可以去网站看下,这里就不做过多介绍。cubehelix system

Custom sequential palettes(自定义顺序调色板)

这种调色板适用于范围在相对无趣的低值和有趣的高值之间的数据。seaborn.light_palette()和seaborn.dark_palette(),在我们传入single color(比如blue,red,green这种)的时候,就可以生成色板。
这两个函数的参数和返回值如下:
在这里插入图片描述

sns.palplot(sns.light_palette("blue",8,False,False,"rgb"))

Out:
在这里插入图片描述

sns.palplot(sns.dark_palette("blue",8,False,False,"rgb"))

Out:
在这里插入图片描述

Diverging color palettes(分散调色板)

这种方式用于对大的低值和高值都感兴趣的数据,数据中通常还有一个定义良好的中点。
可以使用seaborn.diverging_palette()函数,在两种 HUSL 颜色之间创建一个分散的调色板。
在这里插入图片描述

sns.palplot(sns.diverging_palette(100, 23, s=85, l=25, n=7))

Out:
在这里插入图片描述

Setting the default color palettes(默认调色板)

set_palette()的参数和color_palette()是一样的,但是它改变matplotlib默认的参数,以至于这个调色板可以用于所有的图形。

写在后面的话:
1.把官方tutorial这部分看完之后,发现不同的data_type,会有不同的相对应的system供我们来选择掉色板
2.个人觉得函数名这些都很比较好记,难的是根据自己想要表述的信息去选择合适的色彩。
3.尽量避免使用红色和绿色,因为大量的viewers无法辨别。
4.hue:色相或色调
saturation:饱和度
lightness(亮度)
5.用ipython的话,上面好几个函数前面加choose_都可以得到交互式的色板。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值