如何使用 Matplotlib 在繪圖外部的資料座標中編寫註釋?


我們可以使用 annotate() 方法將註釋放在繪圖外部。

步驟

  • 設定圖形大小,並調整子圖之間的間距和周圍的間距。

  • 使用 numpy 建立 xy 資料點。

  • 使用 subplots() 方法建立圖形和一組子圖。

  • 使用 scatter() 方法使用星形標記和銅色對映繪製 xy 資料點。

  • 若要將註釋放在繪圖外部,請相應地使用 xy 座標元組。

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

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.rand(100)
y = np.random.rand(100)
fig, ax = plt.subplots()
ax.scatter(x, y, c=y, marker="*", cmap="copper")
ax.annotate('Scatter points(outside the drawing)', xy=(0.30, 1.05), xycoords=ax.get_xaxis_transform())
plt.show()

輸出

更新日期: 2021-06-01

1000 次+瀏覽

開啟你的事業

完成該課程可獲得認證

開始
廣告
© . All rights reserved.