Matplotlib 中的字体粗细[重复]

问题描述 投票:0回答:1

我有一个图形,我想将轴标签设为粗体。问题是文本包含的数学表达式不会变成粗体。其他部分都OK。

fig = plt.figure(figsize=(4,3))
predicts = np.random.random(1000)*10e9
plt.plot(predicts,predicts, ".", ms=6)
plt.xlabel("Actual NPV (\$) x$10^9$", fontsize=8, fontweight="bold")
plt.ylabel("Predicted NPV (\$) x$10^9$", fontsize=8, fontweight="bold")
# plt.rcParams["font.weight"] = "regular"
plt.yticks(np.arange(9.4, 10.2, 0.2))
R2 = np.round(r2_score(predicts, predicts),2)
plt.text(x=9.4, y=10, s=f"$R^2$={R2}")
plt.rcParams["font.size"] = 8
fig.tight_layout()

输出如附图所示。

如何将 10 的 9 次方也加粗?

我尝试过在轴标签中使用 fontweight,但它不起作用。

python matplotlib
1个回答
0
投票

您可以在内联数学中使用

\mathbf{}
,例如,

plt.xlabel("Actual NPV (\$) x$\mathbf{10^9}$", fontsize=8, fontweight="bold")
© www.soinside.com 2019 - 2024. All rights reserved.