使用 Matplotlib 在圖形中的子圖上標註 A、B、C
要使用 matplotlib 在圖形中的子圖上標註 A、B 和 C,我們可以採用以下步驟
- 設定圖形尺寸並調整子圖之間的和子圖周圍的邊距。
- 使用 nrows=1 和 ncols=3 建立一個圖形和一組子圖。
- 對一個數組進行 1D 迭代。
- 為每個軸迭代並以影像的形式顯示資料。
- 在迴圈中放置文字 A、B 和 C。
- 使用 show() 方法顯示圖形。
示例
import numpy as np from matplotlib import pyplot as plt import string plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, axs = plt.subplots(1, 3) axs = axs.flat for index, ax in enumerate(axs): ax.imshow(np.random.randn(4, 4), interpolation='nearest', cmap="copper") ax.text(0.45, 1.1, string.ascii_uppercase[index], transform=ax.transAxes, size=20, weight='bold') plt.show()
輸出
廣告