颜色和排版一样,看似简单,实际上却很复杂。与数学公式和物理定理不同,它在很大程度上涉及艺术和品味。
[En]
Color is the same as typesetting, it seems simple, but it is actually very complex. To a large extent, it involves art and taste, unlike mathematical formulas and physical theorems.
但是,对 matplotlib 中的颜色有些基本的了解,可以让绘出的图形颜色不至于太突兀。
虽然不能说选择一个完美的配色,但至少看起来很舒服,很抢眼。
[En]
Although it can not be said to choose a perfect color match, at least it looks comfortable and eye-catching.
颜色的种类
颜色一般用 RGB 来表示,颜色空间主要有3种:
- RGB
- sRGB
- Lab
3种颜色空间都可以用 6位的16进制表示,只是显示出来的有些微的差别,在绘图时,一定要确定好自己用的是哪个颜色空间。

matplotlib 有默认的10个颜色和20个颜色,绘图时如果不指定颜色,那么就会用默认用这些颜色。
以下代码打印默认颜色:
[En]
The following code prints the default color:
import matplotlib.pyplot as plt
import matplotlib.colors as colors
cmap10 = plt.get_cmap("tab10")
cmap20 = plt.get_cmap("tab20")
[colors.to_hex(cmap10(i)) for i in range(10)]
[colors.to_hex(cmap20(i)) for i in range(20)]
颜色的运用
一般来说,颜色的使用取决于数据的情况。
[En]
Generally speaking, the use of color depends on the situation of the data.
序列性质的数据
例如,根据年份变化的数据,可以在折线图中使用单一的突出颜色。
[En]
For example, according to the year-varying data, you can use a single prominent color in the line chart.
红色和蓝色等与背景更明显的颜色就可以了。
[En]
Colors such as red and blue that are more distinct from the background are fine.
当多条虚线一起显示时,选择区分较大的颜色。
[En]
When multiple broken lines are displayed together, choose to distinguish between larger colors.
分组比较的数据
对于这类数据,不同组之间的数据以较大的颜色来区分。同一组中的数组可以用相同颜色系统的渐变显示。
[En]
For this kind of data, the data between different groups is distinguished by a larger color. Arrays in the same group can be displayed with gradients of the same color system.

结合地理信息相关的数据
类似的这类数据一般是定性展示,不像上面2种数据需要在Y轴严格定量。
例如,结合地理信息来显示降水的情况,通过颜色的深度来显示降水量,从地图上可以一目了然地看到降水量较大的地方。
[En]
For example, combined with geographic information to show the situation of precipitation, through the depth of color to show the amount of precipitation, from the map can be seen at a glance where the precipitation is larger.

总结
总之,颜色的选择就像排版一样,没有一定的规律,平时多积累,多注意观察别人画出的图形是很重要的。
[En]
In short, the choice of color is like typesetting, there are no certain rules, usually accumulate, pay more attention to observe the graphics drawn by others is very important.
看到一个更清晰的画面,一个很好的展示方式,记录下来,试着运用,培养绘画的感觉,慢慢画出一幅有自己特色的画。
[En]
See a clearer picture, a good way of display, record it, try to apply it, cultivate the feeling of drawing, and slowly draw a picture with its own characteristics.
Original: https://www.cnblogs.com/wang_yb/p/16382833.html
Author: wang_yb
Title: matplotlib可视化系列之【颜色】
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499357/
转载文章受原作者版权保护。转载请注明原作者出处!