Matplotlib 中使用 LaTeX 字体轴标签的粗细


如需在 matplotlib 中使用 LaTeX 表示粗体轴标签,我们可以执行以下步骤:

  • 使用 numpy 创建 x 和 y 数据点。
  • 使用 subplot() 方法,将子图添加到当前图形中。
  • 使用 set_xticksset_yticks 方法,分别使用数据点 x 和 y 设置 x 和 y 轴刻度。
  • 使用 plot() 方法和 color=red 绘制 x 和 y。
  • 如需设置粗体,可以使用 LaTeX 表示。
  • 如需显示图形,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt, font_manager as fm
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.rcParams["font.fantasy"] = "Comic Sans MS"
x = np.array([1, 2, 3, 4])
y = np.exp(x)
ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_yticks(y)
ax1.plot(x, y, c="red")
ax1.set_xticklabels(["$\bf{one}$", "$\bf{two}$", "$\bf{three}$",
   "$\bf{four}$"], rotation=45)
ax1.set_yticklabels(["$\bf{:.2f}$".format(y[0]), "$\bf{:.2f}$".format(y[1]),
   "$\bf{:.2f}$".format(y[2]), "$\bf{:.2f}$".format(y[3])], rotation=45)
plt.tight_layout()
plt.show()

输出

更新日期:2021-5-15

3K+ 次浏览

开启您的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.