ubuntu 使用 Times New Roman 字体在 Matplotlib 中绘图并调整字体大小

ubuntu 使用 Times New Roman 字体在 Matplotlib 中绘图并调整字体大小

文章目录

      • [ubuntu 使用 Times New Roman 字体在 Matplotlib 中绘图并调整字体大小](#ubuntu 使用 Times New Roman 字体在 Matplotlib 中绘图并调整字体大小)
      • [1. 安装 Times New Roman 字体](#1. 安装 Times New Roman 字体)
      • [2. 在 Matplotlib 中加载 Times New Roman 字体](#2. 在 Matplotlib 中加载 Times New Roman 字体)
      • [3. 在 Matplotlib 中使用 Times New Roman](#3. 在 Matplotlib 中使用 Times New Roman)
      • [4. 遇到的问题及解决方案](#4. 遇到的问题及解决方案)
        • [**问题 1:findfont: Font family 'Times New Roman' not found**](#问题 1:findfont: Font family 'Times New Roman' not found)
        • [**问题 2:AttributeError: PathCollection.set() got an unexpected keyword argument 'fontproperties'**](#问题 2:AttributeError: PathCollection.set() got an unexpected keyword argument 'fontproperties')
        • [**问题 3:无法调整字体大小**](#问题 3:无法调整字体大小)
      • [5. 使用全局字体设置](#5. 使用全局字体设置)
      • [6. 调整字体大小和分辨率](#6. 调整字体大小和分辨率)
      • 总结

在数据可视化中,定制字体和样式是常见需求之一,特别是在需要与论文、报告或品牌设计保持一致时,使用特定字体如 Times New Roman 会显得尤为重要。然而,直接在 matplotlib 中设置自定义字体可能会遇到一些问题,比如字体无法加载、无法调整大小或参数冲突等。


1. 安装 Times New Roman 字体

在 Linux 系统上,Times New Roman 字体并不是默认安装的。因此,我们需要手动安装该字体。以下是在 Ubuntu 上安装 Times New Roman 字体的步骤:

bash 复制代码
sudo apt update
sudo apt install ttf-mscorefonts-installer

安装完成后,Times New Roman 字体通常会被存储在路径 /usr/share/fonts/truetype/msttcorefonts/ 中。

验证字体是否安装成功

使用以下命令检查字体是否正确安装:

bash 复制代码
fc-list | grep "Times New Roman"

输出示例:

bash 复制代码
/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 加载字体文件。以下是加载字体的完整代码:

python 复制代码
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 参数将字体应用于标题、轴标签等文本元素。例如:

python 复制代码
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

解决方法

  • 确保字体路径正确。

  • 刷新字体缓存:

    bash 复制代码
    fc-cache -fv
  • 删除 Matplotlib 的字体缓存:

    bash 复制代码
    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 的函数中。修改后的代码如下:

python 复制代码
plt.scatter(x, y, label="Scatter Example", alpha=0.5)
plt.legend(prop=times_new_roman_font)

问题 3:无法调整字体大小

plt.legend() 中同时使用 fontsizeprop 参数时,可能会导致字体大小无法调整的问题。

原因
fontsize 参数与 prop 参数冲突,当使用 prop=FontProperties 时,字体大小应该在 FontProperties 中指定。

解决方法

通过 FontProperties 设置字体大小,而不是直接使用 fontsize 参数。例如:

python 复制代码
# 设置字体大小
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 实现:

python 复制代码
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. 调整字体大小和分辨率

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

  1. 调整字体大小 :通过 FontPropertiessize 参数控制字体大小。

  2. 调整图像分辨率 :通过设置 DPI(每英寸像素点)改善显示效果。例如:

    python 复制代码
    plt.figure(dpi=100)  # 默认是 100,可以增加到 200 或更高

总结

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

  1. 在 Ubuntu 系统中安装并验证 Times New Roman 字体。
  2. 成功在 Matplotlib 中加载 Times New Roman 字体,并应用于标题、轴标签和图例。
  3. 解决了字体无法加载、fontproperties 参数冲突等常见问题。
  4. 学会通过 FontProperties 控制字体大小,并全局应用字体设置。

最终效果展示(示例代码):

python 复制代码
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()
相关推荐
TwoAI2 天前
Matplotlib:绘制你的第一张折线图与散点图
python·matplotlib
eqwaak02 天前
Matplotlib 动画显示进阶:交互式控制、3D 动画与未来趋势
python·tcp/ip·3d·语言模型·matplotlib
烟锁池塘柳04 天前
【已解决,亲测有效】解决使用Python Matplotlib库绘制图表中出现中文乱码(中文显示为框)的问题的方法
开发语言·python·matplotlib
浪浪山齐天大圣5 天前
python数据可视化之Matplotlib(8)-Matplotlib样式系统深度解析:从入门到企业级应用
python·matplotlib·数据可视化
伊织code5 天前
Matplotlib 2 -绘图、统计、网格、3D
3d·matplotlib·绘图
胡耀超5 天前
7、Matplotlib、Seaborn、Plotly数据可视化与探索性分析(探索性数据分析(EDA)方法论)
python·信息可视化·plotly·数据挖掘·数据分析·matplotlib·seaborn
摩羯座-185690305946 天前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
eqwaak07 天前
Matplotlib 动态显示详解:技术深度与创新思考
网络·python·网络协议·tcp/ip·语言模型·matplotlib
深兰科技15 天前
柳州市委常委、统战部部长,副市长潘展东率队首访深兰科技集团新总部,共探 AI 赋能制造大市与东盟合作新局
人工智能·beautifulsoup·numpy·pyqt·matplotlib·pygame·深兰科技
hAnGWS15 天前
Python可视化与交互-matplotlib库
python·交互·matplotlib