Fifth round: beautiful style and color

1. Matplotlib's drawing style (style)

In matplotlib, the easiest way to set the drawing style is to set the style separately when drawing the element.
But sometimes, when users are doing special reports, they often want to maintain the uniformity of the overall style without modifying each picture one by one. Therefore, the matplotlib library also provides four ways to modify the global style in batches.

1. Matplotlib pre-defined styles

matplotlib intimately provides many built-in styles for users to use. The method of use is very simple. You only need to enter the name of the style you want to use at the very beginning of the python script. Try to call different built-in styles to compare the differences.

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('default')
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5b718888>]

Insert picture description here

plt.style.use('ggplot')
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5b9c82c8>]

Insert picture description here

So what are the built-in styles in matplotlib for use? In total, there are 26 rich styles to choose from.

print(plt.style.available)
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']

2. User-defined stylesheet

Create a style list with the suffix mplstyle under any path, edit the file and add the following style content

axes.titlesize : 24
axes.labelsize : 20
lines.linewidth : 3
lines.markersize : 10
xtick.labelsize : 16
ytick.labelsize : 16

Observe the chart changes after referencing the custom stylesheet.

plt.style.use('file/presentation.mplstyle')
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5ba3a788>]

Insert picture description here

It is worth noting that matplotlib supports the reference of mixed styles. You only need to enter a style list when quoting. If the same parameter is involved in several styles, the style sheet on the right will overwrite the value on the left.

plt.style.use(['dark_background', 'file/presentation.mplstyle'])
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5cabfd88>]

Insert picture description here

3. Set rcparams

We can also change the style by modifying the default rc settings. All rc settings are saved in a variable called matplotlib.rcParams.
Drawing after modification, you can see that the drawing style has changed.

plt.style.use('default') # 恢复到默认样式
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5caf8448>]

Insert picture description here

mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.linestyle'] = '--'
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5cb68288>]

Insert picture description here

In addition, matplotlib also provides a more convenient way to modify styles, which can modify multiple styles at once.

mpl.rc('lines', linewidth=4, linestyle='-.')
plt.plot([1,2,3,4],[2,3,4,5])
[<matplotlib.lines.Line2D at 0x1ca5cbd1948>]

Insert picture description here

4. Modify the matplotlibrc file

Since matplotlib uses the matplotlibrc file to control the style, which is the rc setting mentioned in the previous section, we can also change the style by modifying the matplotlibrc file.

# 查找matplotlibrc文件的路径
mpl.matplotlib_fname()

After finding the path, you can directly edit the style file. The format of the file you see after opening is roughly like this. All style parameters are listed in the file. Find the parameter you want to modify, such as lines.linewidth: 8, and put the previous Remove the annotation symbol, and then the drawing will find the style and take effect.

Second, the color settings of matplotlib (color)

In visualization, how to choose the right color and matching combination also needs to be carefully considered, and the color selection must reflect the main idea of ​​the visualization image.
Analyzing the color from the perspective of visual coding, the color can be divided into 色相、亮度和饱和度three visual channels. Generally speaking::
色相There is no obvious order, and it is generally not used to express the level of data, but to express the type of data column.
明度和饱和度: Visually, it is easy to distinguish the level of priority, which is used as a visual channel for expressing the order or expressing the amount of data.
The specific knowledge about color theory is not the focus of this tutorial, please refer to the relevant extension materials for learning.
ECharts Data Visualization Lab
learns these 6 basic skills of visual color matching and restores the meaning of the data itself

In matplotlib, there are several ways to set colors:

1.RGB or RGBA

plt.style.use('default')
# 颜色用[0,1]之间的浮点数表示,四个分量按顺序分别为(red, green, blue, alpha),其中alpha透明度可省略
plt.plot([1,2,3],[4,5,6],color=(0.1, 0.2, 0.5))
plt.plot([4,5,6],[1,2,3],color=(0.1, 0.2, 0.5, 0.5))
[<matplotlib.lines.Line2D at 0x1ca5cc0f448>]

Insert picture description here

2.HEX RGB or RGBA

# 用十六进制颜色码表示,同样最后两位表示透明度,可省略
plt.plot([1,2,3],[4,5,6],color='#0f0f0f')
plt.plot([4,5,6],[1,2,3],color='#0f0f0f80')
[<matplotlib.lines.Line2D at 0x1ca5cdf8708>]

Insert picture description here

3. Gray scale

# 当只有一个位于[0,1]的值时,表示灰度色阶
plt.plot([1,2,3],[4,5,6],color='0.5')
[<matplotlib.lines.Line2D at 0x1ca5cd19b08>]

Insert picture description here

4. Basic color of single character

# matplotlib有八个基本颜色,可以用单字符串来表示,分别是'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w',对应的是blue, green, red, cyan, magenta, yellow, black, and white的英文缩写
plt.plot([1,2,3],[4,5,6],color='m')
[<matplotlib.lines.Line2D at 0x1ca5cd96248>]

Insert picture description here

5. Color name

# matplotlib提供了颜色对照表,可供查询颜色对应的名称
plt.plot([1,2,3],[4,5,6],color='tan')
[<matplotlib.lines.Line2D at 0x1ca5ba03c88>]

Insert picture description here


6. Use colormap to set a set of colors

Some charts support the use of colormap to configure a set of colors, so that more information can be expressed through color changes in visualization.

In matplotlib, there are five types of colormap:

  • Sequential. Usually a single tone is used, with gradual changes in brightness and gradual increase in color, used to express sequential information
  • Diverging. Change the brightness and saturation of two different colors that meet in an unsaturated color in the middle; this value should be used when the information drawn has a key intermediate value (such as terrain) or the data deviates from zero.
  • Cyclic. Change the brightness of two different colors to meet in an unsaturated color in the middle and at the beginning/end. The value used to wrap around the end point, such as phase angle, wind direction, or time of day.
  • Qualitative. Often variegated, used to indicate information that has no order or relationship.
  • Miscellaneous. Some variegated color combinations used in specific scenes, such as rainbows, oceans, terrain, etc.
x = np.random.randn(50)
y = np.random.randn(50)
plt.scatter(x,y,c=x,cmap='RdPu')
<matplotlib.collections.PathCollection at 0x1ca5ce33508>

Insert picture description here

operation

1) Check the official website of matplotlib, list the built-in colormaps of Sequential, Diverging, Cyclic, Qualitative, and Miscellaneous, and 代码绘图show them in the form of

2) Learn how to customize the colormap and apply it to any data set, draw an image, pay attention to the type of colormap to match the characteristics of the data set, and give a simple explanation


Guess you like

Origin blog.csdn.net/weixin_44322234/article/details/111630888