使用matplotlib绘制图表时候如果标题或坐标轴有中文,会出现Warning,同时图形中的会显示为乱码。
UserWarning: Glyph 25991 (\N{CJK UNIFIED IDEOGRAPH-6587}) missing from current font. func(*args, **kwargs)
可以通过matplotlib设置字体,采用Seaborn的方法更简洁一点,但是发现不少文章推荐的sns.set方法在seaborn的新版本中已经弃用(seaborn 0.12.2),官方推荐用set_theme替换,问题解决。
查了下官网API函数,如下说明:
seaborn.set_theme
seaborn.set_theme(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=True, rc=None)
Parameters: 参数:
- context:上下文字符串或字典,缩放参数。
- style:样式字符串或字典,轴样式参数 。
- palette:调色板字符串或序列,调色板。
- font:字体字符串,字体系列,请参阅 matplotlib 字体管理器。
- font_scale:浮点数, 可选
- color_codes:布尔值
- rc:字典或无,用于覆盖上述内容的 rc 参数映射字典。
举例
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(font="SimHei" , font_scale=1 , rc=custom_params)