常见报错:
UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font.
中文内容不被识别。
解决方案:
# 设置中文编码
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
# 坐标轴上负号的正常显示
plt.rcParams["axes.unicode_minus"] = False
另外一种方式是直接修改配置文件:
import matplotlib
# 找到配置文件地址
matplotlib.matplotlib_fname()
删除如下配置项的 # 号,并添加常用字体:
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
复制字体文件到matplotlib包下的目录:
…\python37\lib\site-packages\matplotlib\mpl-data\fonts\ttf
2、 字体设置
font:字体集(font family)、字体大小和样式设置;
matplotlib.font_manager.FontProperties(family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None, math_fontfamily=None)
family:按优先级降序排列的字体列表。此列表可能包括一个通用的字体族名称,如’sans-serif’, ‘serif’, ‘cursive’, ‘fantasy’, 或“monospace”。在这种情况下,在查找字体过程中,将从关联的rcParam中查找实际的字体。 默认: rcParams[“font.family”] (default: [‘sans-serif’])
样式:正常、斜体或倾斜( ‘normal’, ‘italic’ or ‘oblique’. )
拉伸:数字值在 0-1000 范围内,或者在 “ultra-condensed”、“extra-condensed”、“condensed”、“semi-condensed”、“normal”、“semi-expanded”、“expanded” 、 "extra-expanded"中选择一个。
字体浓淡:数字值在 0-1000 范围内或者 ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’中选一个.
字体大小:相对值’xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’ 或者绝对值, e.g., 10. 默认: rcParams[“font.size”] (default: 10.0)
数字字体:用于渲染数学文本的字体家族。支持值包括:‘dejavusans’, ‘dejavuserif’, ‘cm’, ‘stix’, ‘stixsans’ 和 ‘custom’,默认:rcParams[“mathtext.fontset”](默认:‘dejavusans’)
不知道字体选择什么颜色了,可以查询官网关于颜色的定义:
https://matplotlib.org/stable/gallery/color/named_colors.html
基本颜色:
tableau颜色
CSS颜色
3、参考文档
plt.rcParams[‘axes.unicode_minus‘] = False #解决保存图像是负号‘ 这句话是什么意思?