300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Python matplotlib绘图如何显示中文的问题【有报错没有解决】

Python matplotlib绘图如何显示中文的问题【有报错没有解决】

时间:2021-02-18 12:23:30

相关推荐

Python matplotlib绘图如何显示中文的问题【有报错没有解决】

一文详解matplotlib的配置文件以及配置方式

主要介绍路径问题和配置文件相关介绍

linux安装字体库(simSun为例)可以在终端中显示中文

Matlab绘图中下标、斜体及希腊字母的使用方法

python画图可能会报警告findfont: Font family [‘sans-serif’] not found. Falling back to Bitstream Vera Sans,这时需要在matplotlib库下更新字体信息。

我们可以把win10下的字体复制到matplotlib的文字字体路径/home/warmtree/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf里面:

扩展一些字体的英文,用时查询

Python实现matplotlib显示中文的方法详解

字体配置文件在如下路径:

遇到报错

/home/warmtree/anaconda3/lib/python3.7/site-packages/matplotlib/mathtext.py:843: MathTextWarning: Font 'default' does not have a glyph for '\u4f4d' [U+4f4d], substituting with a dummy symbol.MathTextWarning)/home/warmtree/anaconda3/lib/python3.7/site-packages/matplotlib/mathtext.py:843: MathTextWarning: Font 'default' does not have a glyph for '\u79fb' [U+79fb], substituting with a dummy symbol.MathTextWarning)/home/warmtree/anaconda3/lib/python3.7/site-packages/matplotlib/mathtext.py:843: MathTextWarning: Font 'default' does not have a glyph for '\u8377' [U+8377], substituting with a dummy symbol.MathTextWarning)/home/warmtree/anaconda3/lib/python3.7/site-packages/matplotlib/mathtext.py:843: MathTextWarning: Font 'default' does not have a glyph for '\u8f7d' [U+8f7d], substituting with a dummy symbol.MathTextWarning)

通过查找,我们发现其实错误的是中文的显示。Unicode编码转换

示例代码

#-*- coding: utf-8 -*-import matplotlib.pyplot as plt# 下述是为保证中文宋体,英文stix(近似Times New Roman),更具体说明见/p/118601703# 实际上做的是$$内应用LaTex语法,采用字体为mathtext.fontset=stix,其外默认的字体为宋体SimSunimport matplotlib as mplfrom matplotlib.font_manager import FontProperties#font_chinese = FontProperties(fname=r"/media/warmtree/Windows/Windows/Fonts/simsun.ttc", size=24)''' print(matplotlib.get_backend()) #返回matplotlib的后端print(matplotlib.get_cachedir()) #缓存目录print(matplotlib.get_configdir()) #配置目录print(matplotlib.get_data_path()) #数据路径print(matplotlib.get_home()) #用户目录'''mpl.rcParams['font.sans-serif'] = ['SimSun']#显示中文mpl.rcParams['font.serif'] = ['SimSun']plt.style.use('classic')plt.rcParams['legend.framealpha'] = 0 # 图例框完全透明plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号config1 = {"font.family": 'sans-serif',"font.size": 24,}plt.rcParams.update(config1)config2={'text.usetex': False,'mathtext.fontset': 'stix',}plt.rcParams.update(config2)ax = plt.figure().add_subplot(111)ax.set_ylabel(u'\u8377\u8f7d'+r'$P\rm{/kN}$')ax.set_xlabel(u'\u4f4d\u79fb'+r'${\it l\Delta}\rm{/mm}$')plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。