Python Matplotlib library plt.title and other Chinese garbled problems

Reprinted from: https://www.cnblogs.com/shanger/p/13021452.html

Many python drawing programs involve the following two lines of code

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

Have been incomprehensible, thoroughly figured out today!

matplotlib is a third-party library that provides powerful drawing functions for python. Its configuration file is the .rc file, which specifies permanent default values ​​for almost all attributes of matplotlib output graphics. (Graphic properties include form size, dots per inch, line width, color, style, axis, coordinate and network properties, text, font, etc.)

During the code execution, there are two ways to change the operating parameters, so as to achieve the purpose of modifying matplotlib's output graphics properties.

1. Use the parameter dictionary rcParams to access and modify the loaded configuration items. (Note: rc should be the abbreviation of run configuration de)

import matplotlib as mpl
mpl.rcParams['lines.color'] = 'blue' # 此后的线条颜色为蓝色

2. Modify the configuration by passing attribute keywords to the matplotlib.rc() function.

import matplotliib as mpl

mpl.rc('lines', color='blue') # 后续所有图形使用的线条颜色均为蓝色

Therefore, the two lines of code at the beginning of this article modify the already loaded configuration items through the parameter dictionary rcParams. The functions are as follows:

plt.rcParams['font.sans-serif'] = 'SimHei'   # 使图形中的中文正常编码显示
plt.rcParams['axes.unicode_minus'] = False   # 使坐标轴刻度表签正常显示正负号

Among them, sans-serif represents the sans serif in the font, and SimHe is bold.

For font details, please refer to: https://www.cnblogs.com/shanger/articles/11842910.html

Guess you like

Origin blog.csdn.net/u010472858/article/details/108302781