如何在 Matplotlib 中將 xlabel 設定為 X 軸的末端?


要在 matplotlib 中將xlabel 設定在 X 軸的末端,我們可以採取以下步驟 −

  • 使用 numpy 為建立資料點。

  • 使用 subplot() 方法,向當前影像中新增子圖。

  • 使用 plot() 方法繪製 x 和 log(x)

  • 使用set_label() 方法設定 X 軸上的標籤,並指定fontsize=16、loc=left、 color=red

  • 要將xlabel 設定在 X 軸的末端,請使用座標y

  • 要顯示圖形,請使用show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 2, 5)
ax = plt.subplot()
ax.plot(x, np.log(x))
ax.set_xticks(x)
label = ax.set_xlabel('X ->', fontsize=16, loc="left", c="red")
ax.xaxis.set_label_coords(1.0, -0.025)
plt.show()

輸出

更新時間:20-05-06

10K+ 瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告