如何將引數傳遞到 fig.canvas.mpl_connect('key_press_event', on_key) 中的 on_key?


要在 fig.canvas.mpl_connect('key_press_event', on_key) 中將引數傳遞給 on_key,我們可以採取以下步驟 −

  • 設定影像大小並調整子圖之間和周圍的內邊距。
  • 建立一個影像和一組子圖。
  • 設定軸的 x y 刻度。
  • 將函式繫結到事件。
  • 要顯示影像,請使用 show() 方法。

示例

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)

def onkey(event):
   if event.key.isalpha():
      if event.xdata is not None and event.ydata is not None:
         ax.plot(event.xdata, event.ydata, 'bo-')
         fig.canvas.draw()

cid2 = fig.canvas.mpl_connect('key_press_event', onkey)
plt.show()

輸出

更新時間: 03-Jun-2021

558 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.