失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python Matplotlib绘图实现:中文宋体 英文新罗马(科研人必备);解决Font family

python Matplotlib绘图实现:中文宋体 英文新罗马(科研人必备);解决Font family

时间:2023-11-27 09:14:45

相关推荐

python Matplotlib绘图实现:中文宋体 英文新罗马(科研人必备);解决Font family

1、合并字体

将电脑自带的宋体和Times New Roman字体合并为新字体,我命名为SongNTR.ttf

/nowar-fonts/Warcraft-Font-Merger(上述网站可以合并字体)

2、将合并后的字体放置到matplotlib字体文件夹中

文件夹路径通过以下方式可以获得:

import matplotlib.pyplot as pltimport numpy as npimport matplotlibprint(matplotlib.matplotlib_fname())

输出路径:

c:\users\dell\appdata\local\programs\python\python38\lib\site-packages\matplotlib\mpl-data\matplotlibrc

3、修改matplotlibrc文件

文件位于 C:\Users\dell\AppData\Local\Programs\Python\Python38\Lib\site-packages\matplotlib\mpl-data

打开后修改257行和265行,其中257行去掉注释,265行去掉注释并添加字体 SongNTR

font.family: sans-serif#font.style: normal#font.variant: normal#font.weight: normal#font.stretch: normal#font.size: 10.0#font.serif: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, seriffont.sans-serif: SongNTR, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif#font.cursive: Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursive#font.fantasy: Chicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasy

4、修改.matplotlib缓存文件 fontlist-v330.json

位于 C:\Users\dell.matplotlib

打开后插入以下内容:

{"fname": "fonts\\ttf\\SongNTR.ttf","name": "SongNTR","style": "normal","variant": "normal","weight": 700,"stretch": "normal","size": "scalable","__class__": "FontEntry"},

4、重启python

5、测试

设置字体,实现中文:宋体,英文:Times New Roman

plt.rcParams['font.sans-serif'] = ['SongNTR'] ##设置字体

参考:添加链接描述

添加链接描述

python Matplotlib绘图实现:中文宋体 英文新罗马(科研人必备);解决Font family [‘sans-serif‘] not found.

如果觉得《python Matplotlib绘图实现:中文宋体 英文新罗马(科研人必备);解决Font family》对你有帮助,请点赞、收藏,并留下你的观点哦!

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