matplotlib 中文字体 汉字
matplotlib 中文字体测试
wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20220716-mmclassification/dataset/SimHei.ttf
# /home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf
findfont: Font family ['SimHei'] not found. Falling back to DejaVu Sans.
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20013 (\N{CJK UNIFIED IDEOGRAPH-4E2D}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 25991 (\N{CJK UNIFIED IDEOGRAPH-6587}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 23383 (\N{CJK UNIFIED IDEOGRAPH-5B57}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20307 (\N{CJK UNIFIED IDEOGRAPH-4F53}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 35797 (\N{CJK UNIFIED IDEOGRAPH-8BD5}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
findfont: Font family ['SimHei'] not found. Falling back to DejaVu Sans.
findfont: Font family ['SimHei'] not found. Falling back to DejaVu Sans.
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
原因:
字体文件放置的位置不对。
解决方法:
(1) 字体文件的位置:
../anaconda3/envs/yolov5/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/
(2)清除缓存
import matplotlib.pyplot as plt
%matplotlib inline
print(matplotlib.get_cachedir())
rm -rf /root/.cache/matplotlib
(3)重启kernel
(4)
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
print(matplotlib.get_cachedir())
matplotlib.rc("font",family='SimHei') # 中文字体
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
plt.plot([1,2,3], [100,500,300])
plt.title('matplotlib中文字体测试', fontsize=25)
plt.xlabel('X轴', fontsize=15)
plt.ylabel('Y轴', fontsize=15)
plt.show()
====================================================
一、方法一
修改局部字体样式,在需要显示中文的地方增加 fontproperties 属性。
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.title('自定义标题名称', fontproperties='SimHei')
plt.show()
二、方法二
修改全局字体样式,需要使用 matplotlib 模块的 rcParams
import matplotlib.pyplot as plt
#####################################
from matplotlib import rcParams
rcParams['font.family'] = 'SimHei'
#####################################
plt.plot([1, 2, 3, 4])
plt.title('自定义标题名称')
plt.show()
三、方法三
在代码中插入以下三行代码
from pylab import *
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
链接:https://blog.csdn.net/hfy1237/article/details/128218567
======================================================
可以直接从window系统中找到(C:/windows/Fonts),复制即可
常用中文字体
宋体:SimSun
黑体:SimHei
微软雅黑:Microsoft YaHei
微软正黑体:Microsoft JhengHei
新宋体:NSimSun
新细明体:PMingLiU
细明体:MingLiU
标楷体:DFKai-SB
仿宋:FangSong
楷体:KaiTi
隶书:LiSu
幼圆:YouYuan
华文细黑:STXihei
华文楷体:STKaiti
华文宋体:STSong
华文中宋:STZhongsong
华文仿宋:STFangsong
方正舒体:FZShuTi
方正姚体:FZYaoti
华文彩云:STCaiyun
华文琥珀:STHupo
华文隶书:STLiti
华文行楷:STXingkai
华文新魏:STXinwei
————————————————
REF
https://blog.csdn.net/hfy1237/article/details/128218567
https://blog.csdn.net/lemonbit/article/details/121433603
https://blog.csdn.net/BIT_666/article/details/122365074