python - Matplotlib 轴标签部分的斜体 - 应用于默认字体但不指定字体

标签 python matplotlib fonts label italic

我希望图中的所有文本均为 Times New Roman,因此我执行了以下操作:

import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "Times New Roman" # change default font

我只希望标签的“(x)”和“(z)”部分为斜体,所以我这样做了:

plt.xlabel('Underutilization $\it{(x)}$', labelpad=15)
plt.ylabel('Productivity $\it{(z)}$', labelpad=15)

但是,我在绘制时最终得到的是 Times New Roman 中的“Underutilization”,而是斜体无衬线字体(Python 的默认值)中的“(x)”。与 ylabel 相同。

我该如何解决这个问题?

最佳答案

$ 符号内的文本由 matplotlib 的 mathtext 决定(除非您使用 LaTex),并由 documentation 中概述的 rcParam mathtext.fontset 控制。通过

mathtext.fontset: ‘dejavusans’ (default)
  ‘dejavuserif’, ‘cm’ (Computer Modern), ‘stix’,
  ‘stixsans’ or ‘custom’

使用LaTex for the text rendering在字体选择上提供了更大的灵 active ,我相信会支持 Times New Roman。如果您使用 LaTex,那么当您调整 font.family 时,它应该调整数学字体。

关于python - Matplotlib 轴标签部分的斜体 - 应用于默认字体但不指定字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446785/

相关文章:

python - 在循环seaborn图中共享次要y轴

ios - 如何将点转换为 XCode 字体大小

css - 字体系列与字体粗细的成本

python - 如何更改 TLS 上下文选项

python - 将多个相似的 CSV 文件连接到一个大数据框中

python - 如何在 Python matplotlib 中的 LaTeXed 下标中包含字符串链

python - Matplotlib:3 个图绘制成 2 行,单个图像居中

html - 字体嵌入不起作用

python - 从源代码 : ModuleNotFoundError: No module named 'cv2' 构建 opencv

python - 如何获取所有IP地址