From 0d3ea911835c6afa65fb4096d8cba6786ae4168b Mon Sep 17 00:00:00 2001 From: "Joseph.L.Meng" Date: Tue, 26 Mar 2019 09:45:03 +1100 Subject: [PATCH] Update 9.md --- docs/9.md | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/docs/9.md b/docs/9.md index 32b01ef..5438331 100644 --- a/docs/9.md +++ b/docs/9.md @@ -1,8 +1,8 @@ # 选择调色板 -颜色在图像风格中比起其他元素显得更为重要。当合理有效地使用颜色时,数据模式会被凸显出来;反之,则会被掩盖。这里有很多数据可视化中关于颜色使用的优秀资源,我推荐阅读这些 Rob Simmon 的[博客文章](https://earthobservatory.nasa.gov/blogs/elegantfigures/2013/08/05/subtleties-of-color-part-1-of-6/) 以及这篇更加学术性的[论文](https://cfwebprod.sandia.gov/cfdocs/CompResearch/docs/ColorMapsExpanded.pdf)。 此外,matplotlib 文档也提供了一篇很好的[教程](https://matplotlib.org/users/colormaps.html)来说明一些内置Colormap的感知属性。 +颜色在图像风格中比起其他元素显得更为重要。当合理有效地使用颜色时,数据模式会被凸显出来;反之,则会被掩盖。这里有很多数据可视化中关于颜色使用的优秀资源,我推荐阅读这些 Rob Simmon 的[博客文章](https://earthobservatory.nasa.gov/blogs/elegantfigures/2013/08/05/subtleties-of-color-part-1-of-6/)以及这篇更加学术性的[论文](https://cfwebprod.sandia.gov/cfdocs/CompResearch/docs/ColorMapsExpanded.pdf)。 此外,matplotlib 文档也提供了一篇很好的[教程](https://matplotlib.org/users/colormaps.html)来说明一些内置Colormap的感知属性。 -Seaborn让您在选择与您处理的数据和可视化过程中搭配的配色方案变得简单。 +seaborn让您在选择与您处理的数据类型和可视化过程中搭配的配色方案变得简单。 ```py import numpy as np @@ -14,21 +14,21 @@ sns.set() ## 创建调色板 -使用离散调色板过程中最重要函数是[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")。这个函数为许多(但不是全部)可以在seborn中生成颜色的方式提供了一个接口,并且在任何具有`palette`参数的函数的内部都可以使用(以及某些需要多种颜色时具有`color`参数的情况)。 +使用离散调色板过程中最重要函数是[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")。这个函数为许多(但不是全部)可以在seaborn中生成颜色的方式提供了一个接口,并且在任何具有`palette`参数的函数的内部都可以使用(以及某些需要多种颜色时具有`color`参数的情况)。 -[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette") 会接受所有的serborn调色板或者matplotlib Colormap (除了 `jet`, 您永远都不应该使用它). 它还可以获取以任何有效matplotlib格式(RGB元组、十六进制颜色代码或HTML颜色名字)指定的颜色列表。返回值始终是RGB元组的列表。 +[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette") 会接受所有的seaborn调色板或者matplotlib Colormap (除了 `jet`, 您永远都不应该使用它). 它还可以获取以任何有效matplotlib格式(RGB元组、十六进制颜色代码或HTML颜色名字)指定的颜色列表。返回值始终是RGB元组的列表。 最后,在没有参数的情况下调用[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")函数将会返回当前默认的颜色循环。 函数[`set_palette()`](../generated/seaborn.set_palette.html#seaborn.set_palette "seaborn.set_palette")接受相同的参数,并将为所有图像设置默认的颜色循环。您也可以在`with`语句中调用[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")来临时改变调色板。([参见](#palette-contexts)) -在不了解数据特征的情况下,通常也不可能知道哪种配色方案或Colormap最适合一组数据。接下来,我们将通过三种常见的配色方案 _定性调色板_, _顺序调色板_, 和 _发散调色板_ 来拆分介绍使用[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")的使用方法以及其他seaborn函数。 +在不了解数据特征的情况下,通常也不可能知道哪种调色板或Colormap最适合一组数据。接下来,我们将通过三种常见的调色板 _定性调色板_, _顺序调色板_, 和 _发散调色板_ 来拆分介绍[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")函数的使用方法以及其他seaborn函数。 ## 定性调色板 当您想要区分不具有内在顺序的离散数据块时,定性(分类)调色板是最佳方案。 -导入Seaborn的同时,会引入默认的颜色循环,由6种颜色构成。并将唤起标准matplotlib颜色循环,看起来也更加赏心悦目。 +导入seaborn的同时,会引入默认的颜色循环,由6种颜色构成。并将调用标准matplotlib颜色循环,看起来也更加赏心悦目。 ```py current_palette = sns.color_palette() @@ -44,7 +44,7 @@ sns.palplot(current_palette) ### 使用循环颜色系统 -当您要区分任意数量的类别而不强调任何类别时,最简单的方法是在圆形颜色空间中绘制间距相等的颜色(在此颜色空间中,色调会发生变化,同时保持亮度和饱和度不变)。这是大多数seaborn函数在处理当需要区分的数据集超过颜色循环中的6种颜色时时所使用的默认方法。 +当您要区分任意数量的类别而不强调任何类别时,最简单的方法是在循环颜色空间中绘制间距相等的颜色(在此颜色空间中,色调会发生变化,同时保持亮度和饱和度不变)。这是大多数seaborn函数在处理当需要区分的数据集超过颜色循环中的6种颜色时时所使用的默认方法。 最为常用的方法是使用`hls`颜色空间——一种简单的RGB值变体。 @@ -55,7 +55,7 @@ sns.palplot(sns.color_palette("hls", 8)) ![http://seaborn.pydata.org/_images/color_palettes_10_0.png](img/f35d431e80f906b39aa82e1bd8390361.jpg) -[`hls_palette()`](../generated/seaborn.hls_palette.html#seaborn.hls_palette "seaborn.hls_palette")函数允许您控制颜色的亮度和饱和度。 +[`hls_palette()`](../generated/seaborn.hls_palette.html#seaborn.hls_palette "seaborn.hls_palette")函数允许您控制颜色的亮度(lightness)和饱和度(saturation)。 ```py sns.palplot(sns.hls_palette(8, l=.3, s=.8)) @@ -66,7 +66,7 @@ sns.palplot(sns.hls_palette(8, l=.3, s=.8)) 然而,由于人类视觉系统的工作方式,RGB强度很高的颜色也不一定看起来同样强烈。[我们认为](https://en.wikipedia.org/wiki/Color_vision)黄色和绿色是相对较亮的,蓝色是相对较暗的,当目标是与`hls`系统保持一致性时可能会带来一些问题。 -为了解决这一问题,seborn提供了一个[husl](http://www.hsluv.org/)系统(后来更名为HSLuv)的接口,这也使选择间隔均匀的色调变得容易,同时使外观亮度和饱和度都更加均匀。 +为了解决这一问题,seaborn提供了一个[husl](http://www.hsluv.org/)系统(后来更名为HSLuv)的接口,这也使选择间隔均匀的色调变得容易,同时使亮度和饱和度都更加均匀。 ```py sns.palplot(sns.color_palette("husl", 8)) @@ -79,9 +79,9 @@ sns.palplot(sns.color_palette("husl", 8)) ### 使用Color Brewer调色板 -[Color Brewer](http://colorbrewer2.org/)提供了另一种美观的配色方案(包括sequential palettes和 diverging palettes,详情见下文)。这些也作为matplotlib Colormap存在,但并没有得到很好的处理。在seaborn中,当您需要定性(qualitative)的Color Brewer方案时,你总是会得到离散的颜色,但这意味着在某些情况下颜色会循环重复。 +[Color Brewer](http://colorbrewer2.org/)为定性调色板提供了另一种美观的配色方案(同样包含顺序调色板包括和发散调色板,详情见下文)。这些也作为matplotlib Colormap存在,但并没有得到很好的处理。在seaborn中,当您需要定性(qualitative)的Color Brewer方案时,你总是会得到离散的颜色,但这意味着在某些情况下颜色会循环重复。 -Color Brewer网站的一个很好的特点是它对色盲比较友好。[色盲](https:/en.wikipea.org/wiki/Color_Blinity)有很多种,最为常见的是红绿色盲。通常,对于需要根据颜色进行元素区分时,尽量避免使用红色和绿色会是一个好主意。 +Color Brewer的一个很好的特点是它对色盲比较友好。[色盲](https:/en.wikipea.org/wiki/Color_Blinity)有很多种,最为常见的是红绿色盲。通常,对于需要根据颜色进行元素区分时,应该尽量避免使用这两种颜色。 ```py sns.palplot(sns.color_palette("Paired")) @@ -99,7 +99,7 @@ sns.palplot(sns.color_palette("Set2")) 为了帮助您从Color Brewer库中选取配色方案,seaborn提供了[`choose_colorbrewer_palette()`](../generated/seaborn.choose_colorbrewer_palette.html#seaborn.choose_colorbrewer_palette "seaborn.choose_colorbrewer_palette")函数。这个函数能够启动交互式组件来帮助您浏览各种选项,修改不同的参数。但是只能在Jupyter notebook中使用。 -当然,您可能只希望手动指定一组您喜欢的颜色。[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")接受一个颜色列表,操作起来也很简单。 +当然,您可能只希望手动指定一组您喜欢的颜色。[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")函数会接受一个颜色列表,操作起来也很简单。 ```py flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"] @@ -134,9 +134,9 @@ sns.palplot(sns.xkcd_palette(colors)) ## 顺序调色板 -第二类主要的调色板被称为“顺序调色板”(sequential),当数据集的范围从相对低值(不感兴趣)到相对高值(很感兴趣)时,最好使用顺序调色板。尽管在某些情况下您可能需要顺序调色板中的离散颜色,在[`kdeplot()`](../generated/seaborn.kdeplot.html#seaborn.kdeplot "seaborn.kdeplot")和[`heatmap()`](../generated/seaborn.heatmap.html#seaborn.heatmap "seaborn.heatmap")函数中使用它们来作为Colormap则更为常见(以及类似的matplotlib函数)。 +第二类主要的调色板被称为“顺序调色板”(sequential),当数据集的范围从相对低值(不感兴趣)到相对高值(很感兴趣)时,最好使用顺序调色板,尽管在某些情况下您可能需要顺序调色板中的离散颜色。在[`kdeplot()`](../generated/seaborn.kdeplot.html#seaborn.kdeplot "seaborn.kdeplot")和[`heatmap()`](../generated/seaborn.heatmap.html#seaborn.heatmap "seaborn.heatmap")函数中使用它们来作为Colormap则更为常见(以及类似的matplotlib函数)。 -在这种情况下使用`jet`(或其他彩虹调色板)等Colormap是很常见的,因为色调范围给人的印象是提供有关数据的额外信息。然而,具有较大色调变化的Colormap往往会引入数据中不存在的不连续性,并且我们的视觉系统无法自然地将彩虹映射到诸如“高”或“低”的定量区别。结果是,这些可视化结果更像是一个谜题,它们掩盖了数据中的模式,而不是揭示它们。`jet` 调色板使用了最亮的颜色(黄色和青色)的中间数据值,导致效果是强调无趣的(和任意的)值,而不是强调极端的值。 +在这种情况下使用`jet`(或其他彩虹调色板)等Colormap是很常见的,因为色调范围给人的印象是提供有关数据的额外信息。然而,具有较大色调变化的Colormap往往会引入数据中不存在的不连续性,并且我们的视觉系统无法自然地将彩虹光谱映射到诸如“高”或“低”的定量区分。导致来这些可视化的结果更加令人困惑,因为它们掩盖了数据中的模式,而不是揭示它们。`jet` 调色板使用了最亮的颜色(黄色和青色)的中间数据值,导致效果是强调无趣的(和任意的)值,而不是强调极端的值。 对于连续性的数据,最好使用色调变化幅度较小,而亮度和饱和度变化幅度较大的配色方案。这种方法会很自然地吸引人们注意数据中相对重要的部分。 @@ -159,7 +159,7 @@ sns.palplot(sns.color_palette("BuGn_r")) ![http://seaborn.pydata.org/_images/color_palettes_27_0.png](img/35ba6c0dab54401105c2b869de17084b.jpg) -seaborn同样添加了一个技巧允许您创建“深色” 调色板,它没有一个很宽的动态范围。当您需要按顺序映射直线或点时这可能会很有用,因为颜色较亮的线条会比较难以区分。 +seaborn同样添加了一个小窍门来帮助您创建“深色”调色板,它没有一个很宽的动态范围。在当您需要按顺序映射直线或点时这可能会很有用,因为颜色较亮的线条会比较难以区分。 ```py sns.palplot(sns.color_palette("GnBu_d")) @@ -168,11 +168,11 @@ sns.palplot(sns.color_palette("GnBu_d")) ![http://seaborn.pydata.org/_images/color_palettes_29_0.png](img/12d91b101a0391d48ce055f66e4a910a.jpg) -您可能想要使用[`choose_colorbrewer_palette()`](../generated/seaborn.choose_colorbrewer_palette.html#seaborn.choose_colorbrewer_palette "seaborn.choose_colorbrewer_palette")来尝试多种选项,当您希望传递给seaborn或者matplotlib返回值为Colormap对象时,您可以将`as_cmap`对象设置为`True`。 +您可能想要使用[`choose_colorbrewer_palette()`](../generated/seaborn.choose_colorbrewer_palette.html#seaborn.choose_colorbrewer_palette "seaborn.choose_colorbrewer_palette")函数来尝试多种选项,当您希望传递给seaborn或者matplotlib的返回值为Colormap对象时,您可以将`as_cmap`对象设置为`True`。 ### 顺序 “cubehelix” 调色板 -[cubehelix](https://www.mrao.cam.ac.uk/~dag/CUBEHELIX/)调色板系统使顺序调色板的亮度线性变化,色调也会产生一些变化。这意味着您的Colormap在转换为黑白模式时(用于打印)的信息将得到保留,且对色盲友好。 +[cubehelix](https://www.mrao.cam.ac.uk/~dag/CUBEHELIX/)调色板系统使顺序调色板的亮度产生线性变化,色调也会产生一些变化。这意味着您的Colormap在转换为黑白模式时(用于打印)的信息将得到保留,且对色盲友好。 Matplotlib内置了默认的cubehelix版本: @@ -185,7 +185,7 @@ sns.palplot(sns.color_palette("cubehelix", 8)) Seborn为cubehelix系统提供了一个接口,以便您可以制作各种调色板,这些调色板都具有良好的线性亮度渐变。 -由seborn [`cubehelix_palette()`](../generated/seaborn.cubehelix_palette.html#seaborn.cubehelix_palette "seaborn.cubehelix_palette") 函数返回的默认调色板与matplotlib的默认值稍有不同,因为它不会围绕色轮旋转很远,也不会覆盖很宽的强度范围。它还反转顺序,以便更重要的值颜色会更暗: +由seborn [`cubehelix_palette()`](../generated/seaborn.cubehelix_palette.html#seaborn.cubehelix_palette "seaborn.cubehelix_palette") 函数返回的默认调色板与matplotlib的默认值稍有不同,因为它不会围绕色轮旋转很远,也不会覆盖很宽的强度范围。它还反转顺序,以便让更重要的值的颜色更暗: ```py sns.palplot(sns.cubehelix_palette(8)) @@ -193,7 +193,7 @@ sns.palplot(sns.cubehelix_palette(8)) ![http://seaborn.pydata.org/_images/color_palettes_34_0.png](img/be20ca200667c3c8849b95c4a6aa846e.jpg) -[`cubehelix_palette()`](../generated/seaborn.cubehelix_palette.html#seaborn.cubehelix_palette "seaborn.cubehelix_palette") 的其他参数控制调色板的外观。您将更改的两个主要参数为 `start` (介于0到3之间的值)和 `rot` —— 旋转次数(任意值,但可能在-1和1之间)。 +[`cubehelix_palette()`](../generated/seaborn.cubehelix_palette.html#seaborn.cubehelix_palette "seaborn.cubehelix_palette") 函数的其他参数控制调色板的外观。您将更改的两个主要参数为 `start` (介于0到3之间的值)和 `rot` —— 旋转次数(任意值,但可能在-1和1之间)。 ```py sns.palplot(sns.cubehelix_palette(8, start=.5, rot=-.75)) @@ -211,7 +211,7 @@ sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0, light=.95, reverse= ![http://seaborn.pydata.org/_images/color_palettes_38_0.png](img/c4e8f173e5519de01ed11bca06911371.jpg) -如同其他seaborn函数,您将默认得到一个颜色列表。但您也可以通过修改 `as_cmap=True` 将调色板作为Colormap对象的返回值来传递给 seaborn 或 matplotlib函数。 +如同其他seaborn函数,您将默认得到一个颜色列表。但您也可以通过修改 `as_cmap=True` 将调色板作为Colormap对象的返回值来传递给seaborn或matplotlib函数。 ```py x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T @@ -226,7 +226,7 @@ sns.kdeplot(x, y, cmap=cmap, shade=True); ### 自定义调色板 -For a simpler interface to custom sequential palettes, you can use [`light_palette()`](../generated/seaborn.light_palette.html#seaborn.light_palette "seaborn.light_palette") or [`dark_palette()`](../generated/seaborn.dark_palette.html#seaborn.dark_palette "seaborn.dark_palette"), which are both seeded with a single color and produce a palette that ramps either from light or dark desaturated values to that color. These functions are also accompanied by the [`choose_light_palette()`](../generated/seaborn.choose_light_palette.html#seaborn.choose_light_palette "seaborn.choose_light_palette") and [`choose_dark_palette()`](../generated/seaborn.choose_dark_palette.html#seaborn.choose_dark_palette "seaborn.choose_dark_palette") functions that launch interactive widgets to create these palettes. +为了更简单地生成自定义顺序调色板,您可以使用 [`light_palette()`](../generated/seaborn.light_palette.html#seaborn.light_palette "seaborn.light_palette") 或 [`dark_palette()`](../generated/seaborn.dark_palette.html#seaborn.dark_palette "seaborn.dark_palette") 函数。它们都是以某个颜色为种子,从明向暗或从暗向明渐变,产生顺序调色板。与这些函数相搭配的还有 [`choose_light_palette()`](../generated/seaborn.choose_light_palette.html#seaborn.choose_light_palette "seaborn.choose_light_palette") 和 [`choose_dark_palette()`](../generated/seaborn.choose_dark_palette.html#seaborn.choose_dark_palette "seaborn.choose_dark_palette") 来提供交互式组件便于创建调色板。 ```py sns.palplot(sns.light_palette("green")) @@ -304,7 +304,7 @@ sns.palplot(sns.color_palette("RdBu_r", 7)) ![http://seaborn.pydata.org/_images/color_palettes_55_0.png](img/a6f00f18e0a24fee984824847391d181.jpg) -matplotlib库中内置的 `coolwarm` 调色板也是一个很好的选择。请注意,这个colormap的中间值和极值之间的对比度较小。 +matplotlib库中内置的 `coolwarm` 调色板也是一个很好的选择。请注意,这个Colormap的中间值和极值之间的对比度较小。 ```py sns.palplot(sns.color_palette("coolwarm", 7)) @@ -351,7 +351,6 @@ sns.palplot(sns.diverging_palette(255, 133, l=60, n=7, center="dark")) ## 设置默认调色板 -The [`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette") function has a companion called [`set_palette()`](../generated/seaborn.set_palette.html#seaborn.set_palette "seaborn.set_palette"). The relationship between them is similar to the pairs covered in the [aesthetics tutorial](aesthetics.html#aesthetics-tutorial). [`set_palette()`](../generated/seaborn.set_palette.html#seaborn.set_palette "seaborn.set_palette") accepts the same arguments as [`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"), but it changes the default matplotlib parameters so that the palette is used for all plots. 与[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")函数相伴随的有[`set_palette()`](../generated/seaborn.set_palette.html#seaborn.set_palette "seaborn.set_palette")。 两者之间的关系与[美学教程](aesthetics.html#aesthetics-tutorial)中介绍的[`set_palette()`](../generated/seaborn.set_palette.html#seaborn.set_palette "seaborn.set_palette")函数和[`color_palette()`](../generated/seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")函数接受相同参数的关系相类似。但它会更改默认的matplotlib参数,以便调色板应用于所有图像。 ```py -- GitLab