我希望图中的所有文本均为 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/