马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
ubuntu 利用 Times New Roman 字体在 Matplotlib 中画图并调解字体巨细
在数据可视化中,定制字体和样式是常见需求之一,特殊是在须要与论文、陈诉或品牌筹划保持划一时,利用特定字体如 Times New Roman 会显得尤为告急。然而,直接在 matplotlib 中设置自界说字体大概会遇到一些题目,比如字体无法加载、无法调解巨细或参数辩论等。
1. 安装 Times New Roman 字体
在 Linux 体系上,Times New Roman 字体并不是默认安装的。因此,我们须要手动安装该字体。以下是在 Ubuntu 上安装 Times New Roman 字体的步调:
- sudo apt update
- sudo apt install ttf-mscorefonts-installer
复制代码 安装完成后,Times New Roman 字体通常会被存储在路径 /usr/share/fonts/truetype/msttcorefonts/ 中。
验证字体是否安装乐成
利用以下下令查抄字体是否精确安装:
- fc-list | grep "Times New Roman"
复制代码 输出示例:
- /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 加载字体文件。以下是加载字体的完备代码:
- from matplotlib import font_manager as fm
- # 加载 Times New Roman 字体
- font_path = "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf"
- times_new_roman_font = fm.FontProperties(fname=font_path)
- # 检查字体是否加载成功
- print("Loaded font name:", times_new_roman_font.get_name())
复制代码 假如输出为 Times New Roman,阐明字体加载乐成。
3. 在 Matplotlib 中利用 Times New Roman
示例:应用于标题和轴标签
我们可以通过 fontproperties 参数将字体应用于标题、轴标签等文本元素。比方:
- import matplotlib.pyplot as plt
- # 示例数据
- x = [1, 2, 3]
- y = [4, 5, 6]
- # 创建绘图
- plt.figure(figsize=(6, 6))
- plt.plot(x, y, label="Sample Line")
- # 应用字体到标题和轴标签
- plt.title("Title in Times New Roman", fontproperties=times_new_roman_font, fontsize=20)
- plt.xlabel("X-axis", fontproperties=times_new_roman_font, fontsize=15)
- plt.ylabel("Y-axis", fontproperties=times_new_roman_font, fontsize=15)
- # 图例
- plt.legend(prop=times_new_roman_font, loc='upper left')
- plt.show()
复制代码 4. 遇到的题目及办理方案
题目 1:findfont: Font family ‘Times New Roman’ not found
在某些情况下,纵然精确安装了字体,Matplotlib 仍大概无法辨认字体,报错雷同:
- findfont: Font family 'Times New Roman' not found
复制代码 办理方法:
- 确保字体路径精确。
- 革新字体缓存:
- 删除 Matplotlib 的字体缓存:
- rm -rf ~/.cache/matplotlib
复制代码 题目 2:AttributeError: PathCollection.set() got an unexpected keyword argument ‘fontproperties’
假如实验在 plt.scatter() 中利用 fontproperties 参数,会报错雷同:
- AttributeError: PathCollection.set() got an unexpected keyword argument 'fontproperties'
复制代码 缘故原由:
fontproperties 参数并不实用于 scatter 函数,而是用于设置文本(如标题、图例)的字体。
办理方法:
将字体设置移到 plt.legend() 或其他支持 fontproperties 的函数中。修改后的代码如下:
- plt.scatter(x, y, label="Scatter Example", alpha=0.5)
- plt.legend(prop=times_new_roman_font)
复制代码 题目 3:无法调解字体巨细
在 plt.legend() 中同时利用 fontsize 和 prop 参数时,大概会导致字体巨细无法调解的题目。
缘故原由:
fontsize 参数与 prop 参数辩论,当利用 prop=FontProperties 时,字体巨细应该在 FontProperties 中指定。
办理方法:
通过 FontProperties 设置字体巨细,而不是直接利用 fontsize 参数。比方:
- # 设置字体大小
- times_new_roman_font = fm.FontProperties(fname=font_path, size=20)
- # 应用到图例
- plt.legend(loc='lower left', prop=times_new_roman_font)
复制代码 5. 利用全局字体设置
假如渴望将 Times New Roman 设置为整个图形的默认字体,可以通过 Matplotlib 的 rcParams 实现:
- from matplotlib import rcParams
- # 设置全局字体
- rcParams['font.family'] = times_new_roman_font.get_name()
- rcParams['font.size'] = 12
- # 创建绘图
- plt.plot([1, 2, 3], [4, 5, 6], label="Example Line")
- plt.legend(loc='upper left')
- plt.title("Global Font Example")
- plt.show()
复制代码 6. 调解字体巨细和分辨率
当字体太大或太小时,可以通过以下方法调解:
- 调解字体巨细:通过 FontProperties 的 size 参数控制字体巨细。
- 调解图像分辨率:通过设置 DPI(每英寸像素点)改善体现效果。比方:
- plt.figure(dpi=100) # 默认是 100,可以增加到 200 或更高
复制代码 总结
通过本文的先容,我们实现了以下目标:
- 在 Ubuntu 体系中安装并验证 Times New Roman 字体。
- 乐成在 Matplotlib 中加载 Times New Roman 字体,并应用于标题、轴标签和图例。
- 办理了字体无法加载、fontproperties 参数辩论等常见题目。
- 学会通过 FontProperties 控制字体巨细,并全局应用字体设置。
终极效果展示(示例代码):
- import matplotlib.pyplot as plt
- from matplotlib import font_manager as fm
- # 加载字体
- font_path = "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf"
- times_new_roman_font = fm.FontProperties(fname=font_path, size=20)
- # 绘图
- plt.figure(dpi=100)
- plt.plot([1, 2, 3], [4, 5, 6], label="Sample Line")
- plt.title("Final Example", fontproperties=times_new_roman_font)
- plt.xlabel("X-axis", fontproperties=times_new_roman_font)
- plt.ylabel("Y-axis", fontproperties=times_new_roman_font)
- plt.legend(loc='upper left', prop=times_new_roman_font)
- plt.show()
复制代码 |