如何獲取 Pyplot 中某個圖形的座標軸列表?


為了獲得一個圖形的座標軸列表,我們首先會建立一個圖形然後使用 get_axes() 方法來獲得座標軸並設定這些座標軸的標籤。

  • 使用 numpy 建立 x 和 y 軸,使用 figure() 方法建立 fig。建立一個新的圖形,或者啟用一個已有的圖形。
  • 使用 add_subplot() 方法。把一個 '~.axes.Axes' 新增到該圖形的子圖佈局中,其中 nrows=1, ncols=1 且 index=1.
  • 獲取 fig 的座標軸,並設定 xlabelylabel
  • 使用紅色繪製 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
xs = np.linspace(1, 10, 10)
ys = np.tan(xs)
fig = plt.figure()
ax = fig.add_subplot(111)
fig.get_axes()[0].set_ylabel("Y-Axis")
fig.get_axes()[0].set_xlabel("X-Axis")
ax.plot(xs, ys, c='red')
plt.show()

輸出

更新於: 06-05-2021

5 千瀏覽量

開啟你的 職業生涯

完成課程獲取認證

開始吧
廣告
© . All rights reserved.