实现目标:论文出图,图中的英文为Times New Roman,同时中文为微软雅黑等其他字体。
存在问题:单独实现任一一种字体的统一都很简单(请参考ubuntu16.04中解决matplotlib画图中文无法显示问题 - 小白的学习笔记),但同时实现就很麻烦。
编译环境:我的编译环境是在vscode中的ubunt wsl,用jupyter notebook做数据分析。
解决方法:从matplotlib.font_manager中导入FontProperties,采用中英文字体分别表达的方式
示例代码:
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
chinese_font = FontProperties(fname='/路径/微软雅黑.ttf')
english_font = FontProperties(fname='/路径/times new roman.ttf')
x = [1,2,3]
y = [4,5,6]
fig, ax = plt.subplots()
ax.plot()
ax.set_xlabel('abc',fontproperties=english_font , fontsize=14)
ax.set_xlabel('中文',fontproperties=chinese_font , fontsize=14)
plt.show()
效果:中文为微软雅黑,英文为Times New Roman
参考文献:
ubuntu16.04中解决matplotlib画图中文无法显示问题 - 小白的学习笔记
vscode解决matplotlib不显示中文问题_瑶光光的博客-优快云博客_vscode python matplotlib.rc
ChatGPT:OpenAI