欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > [Pyplot]设置图中字体为TimesNewRoman

[Pyplot]设置图中字体为TimesNewRoman

2025/5/2 14:44:15 来源:https://blog.csdn.net/Strengthennn/article/details/141667262  浏览:    关键词:[Pyplot]设置图中字体为TimesNewRoman

一、简介

本文介绍了如何在linux环境下在python中使用matplotlib.pyplot 绘制图表时,令其中的文字字体为Times New Roman

二、设置步骤

1. Linux下安装Times New Roman字体

$ sudo apt install ttf-mscorefonts-installer # 安装字体
$ sudo fc-cache # 使新安装的字体生效

注意,在安装字体时会暂停一次,需要手动输入yes同意字体开源协议才能继续进行安装。

2. 删除matplotlib的 fontcache

rm ~/.cache/matplotlib/ -rf

3. 在python代码中使用Times New Roman

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
# 设置使用 Times New Roman 字体
plt.rc('font',family='Times New Roman')
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)
# 设置x,y,z轴刻度线字号
ax.tick_params(labelsize=14)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()

运行结果如下:

pyplot result

三、参考

[1]. 在Linux(Ubuntu)下安装Arial、Times New Roman等字体
[2]. matplotlib 字体改为 Times New Roman

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词