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+ 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告