python matplotlib notes: font management

Common errors:
UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font.
Error picture
Chinese content is not recognized.

solution:

# 设置中文编码
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
# 坐标轴上负号的正常显示
plt.rcParams["axes.unicode_minus"] = False

Another way is to modify the configuration file directly:

import matplotlib
# 找到配置文件地址
matplotlib.matplotlib_fname()

Delete the # sign in the following configuration items and add commonly used fonts:

font.family : Microsoft YaHei, sans-serif
font.serif : Microsoft YaHei, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif

Copy the font files to the directory under the matplotlib package:
…\python37\lib\site-packages\matplotlib\mpl-data\fonts\ttf

2. Font settings

font: font family, font size and style settings;

matplotlib.font_manager.FontProperties(family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None, math_fontfamily=None)

family: A list of fonts in descending order of priority. This list may include a generic font family name such as 'sans-serif', 'serif', 'cursive', 'fantasy', or "monospace". In this case, during the font lookup process, the actual font will be looked up from the associated rcParam. Default: rcParams["font.family"] (default: ['sans-serif'])
Style: normal, italic or slant ( 'normal', 'italic' or 'oblique'. )
Stretch: numeric value between 0- Within the range of 1000, or choose one among "ultra-condensed", "extra-condensed", "condensed", "semi-condensed", "normal", "semi-expanded", "expanded", "extra-expanded" .
Font intensity: Numeric value in the range of 0-1000 or 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi ', 'bold', 'heavy', 'extra bold', 'black', choose one.
Font size: relative value 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large' or absolute value, eg, 10. Default: rcParams["font.size"] (default: 10.0)
Numeric Fonts: A font family used for rendering mathematical text. Supported values ​​include: 'dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans' and 'custom', default: rcParams["mathtext.fontset"] (default: 'dejavusans')

I don’t know what color to choose for the font. You can check the official website’s definition of color:
https://matplotlib.org/stable/gallery/color/named_colors.html
Basic colors:
Insert image description here
tableau color
Insert image description here
CSS color
Insert image description here

3. Refer to the document
plt.rcParams['axes.unicode_minus'] = False #Solution to saving the image with a negative sign' What does this sentence mean?

Guess you like

Origin blog.csdn.net/weixin_39747882/article/details/128436711