ubuntu 利用 Times New Roman 字体在 Matplotlib 中画图并调解字体巨细

[复制链接]
发表于 2025-12-6 07:11:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
ubuntu 利用 Times New Roman 字体在 Matplotlib 中画图并调解字体巨细



在数据可视化中,定制字体和样式是常见需求之一,特殊是在须要与论文、陈诉或品牌筹划保持划一时,利用特定字体如 Times New Roman 会显得尤为告急。然而,直接在 matplotlib 中设置自界说字体大概会遇到一些题目,比如字体无法加载、无法调解巨细或参数辩论等。

1. 安装 Times New Roman 字体

在 Linux 体系上,Times New Roman 字体并不是默认安装的。因此,我们须要手动安装该字体。以下是在 Ubuntu 上安装 Times New Roman 字体的步调:
  1. sudo apt update
  2. sudo apt install ttf-mscorefonts-installer
复制代码
安装完成后,Times New Roman 字体通常会被存储在路径 /usr/share/fonts/truetype/msttcorefonts/ 中。
验证字体是否安装乐成

利用以下下令查抄字体是否精确安装:
  1. fc-list | grep "Times New Roman"
复制代码
输出示例:
  1. /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf: Times New Roman:style=Regular
复制代码

2. 在 Matplotlib 中加载 Times New Roman 字体

为了在 Matplotlib 中利用 Times New Roman 字体,我们须要通过 matplotlib.font_manager 加载字体文件。以下是加载字体的完备代码:
  1. from matplotlib import font_manager as fm
  2. # 加载 Times New Roman 字体
  3. font_path = "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf"
  4. times_new_roman_font = fm.FontProperties(fname=font_path)
  5. # 检查字体是否加载成功
  6. print("Loaded font name:", times_new_roman_font.get_name())
复制代码
假如输出为 Times New Roman,阐明字体加载乐成。

3. 在 Matplotlib 中利用 Times New Roman

示例:应用于标题和轴标签

我们可以通过 fontproperties 参数将字体应用于标题、轴标签等文本元素。比方:
  1. import matplotlib.pyplot as plt
  2. # 示例数据
  3. x = [1, 2, 3]
  4. y = [4, 5, 6]
  5. # 创建绘图
  6. plt.figure(figsize=(6, 6))
  7. plt.plot(x, y, label="Sample Line")
  8. # 应用字体到标题和轴标签
  9. plt.title("Title in Times New Roman", fontproperties=times_new_roman_font, fontsize=20)
  10. plt.xlabel("X-axis", fontproperties=times_new_roman_font, fontsize=15)
  11. plt.ylabel("Y-axis", fontproperties=times_new_roman_font, fontsize=15)
  12. # 图例
  13. plt.legend(prop=times_new_roman_font, loc='upper left')
  14. plt.show()
复制代码

4. 遇到的题目及办理方案

题目 1:findfont: Font family ‘Times New Roman’ not found

在某些情况下,纵然精确安装了字体,Matplotlib 仍大概无法辨认字体,报错雷同:
  1. findfont: Font family 'Times New Roman' not found
复制代码
办理方法:


  • 确保字体路径精确。
  • 革新字体缓存:
    1. fc-cache -fv
    复制代码
  • 删除 Matplotlib 的字体缓存:
    1. rm -rf ~/.cache/matplotlib
    复制代码

题目 2:AttributeError: PathCollection.set() got an unexpected keyword argument ‘fontproperties’

假如实验在 plt.scatter() 中利用 fontproperties 参数,会报错雷同:
  1. AttributeError: PathCollection.set() got an unexpected keyword argument 'fontproperties'
复制代码
缘故原由:
fontproperties 参数并不实用于 scatter 函数,而是用于设置文本(如标题、图例)的字体。
办理方法:
将字体设置移到 plt.legend() 或其他支持 fontproperties 的函数中。修改后的代码如下:
  1. plt.scatter(x, y, label="Scatter Example", alpha=0.5)
  2. plt.legend(prop=times_new_roman_font)
复制代码

题目 3:无法调解字体巨细

在 plt.legend() 中同时利用 fontsize 和 prop 参数时,大概会导致字体巨细无法调解的题目。
缘故原由:
fontsize 参数与 prop 参数辩论,当利用 prop=FontProperties 时,字体巨细应该在 FontProperties 中指定。
办理方法:
通过 FontProperties 设置字体巨细,而不是直接利用 fontsize 参数。比方:
  1. # 设置字体大小
  2. times_new_roman_font = fm.FontProperties(fname=font_path, size=20)
  3. # 应用到图例
  4. plt.legend(loc='lower left', prop=times_new_roman_font)
复制代码

5. 利用全局字体设置

假如渴望将 Times New Roman 设置为整个图形的默认字体,可以通过 Matplotlib 的 rcParams 实现:
  1. from matplotlib import rcParams
  2. # 设置全局字体
  3. rcParams['font.family'] = times_new_roman_font.get_name()
  4. rcParams['font.size'] = 12
  5. # 创建绘图
  6. plt.plot([1, 2, 3], [4, 5, 6], label="Example Line")
  7. plt.legend(loc='upper left')
  8. plt.title("Global Font Example")
  9. plt.show()
复制代码

6. 调解字体巨细和分辨率

当字体太大或太小时,可以通过以下方法调解:

  • 调解字体巨细:通过 FontProperties 的 size 参数控制字体巨细。
  • 调解图像分辨率:通过设置 DPI(每英寸像素点)改善体现效果。比方:
    1. plt.figure(dpi=100)  # 默认是 100,可以增加到 200 或更高
    复制代码

总结

通过本文的先容,我们实现了以下目标:

  • 在 Ubuntu 体系中安装并验证 Times New Roman 字体。
  • 乐成在 Matplotlib 中加载 Times New Roman 字体,并应用于标题、轴标签和图例。
  • 办理了字体无法加载、fontproperties 参数辩论等常见题目。
  • 学会通过 FontProperties 控制字体巨细,并全局应用字体设置。
终极效果展示(示例代码):
  1. import matplotlib.pyplot as plt
  2. from matplotlib import font_manager as fm
  3. # 加载字体
  4. font_path = "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf"
  5. times_new_roman_font = fm.FontProperties(fname=font_path, size=20)
  6. # 绘图
  7. plt.figure(dpi=100)
  8. plt.plot([1, 2, 3], [4, 5, 6], label="Sample Line")
  9. plt.title("Final Example", fontproperties=times_new_roman_font)
  10. plt.xlabel("X-axis", fontproperties=times_new_roman_font)
  11. plt.ylabel("Y-axis", fontproperties=times_new_roman_font)
  12. plt.legend(loc='upper left', prop=times_new_roman_font)
  13. plt.show()
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表