如何在 Matplotlib 中重新整理文字?


要在 Matplotlib 中重新整理文字,我們可以採取以下步驟:

  • 設定圖形大小並調整子圖之間和周圍的填充。
  • 建立圖形和一組子圖。
  • 向軸中新增文字。
  • 編寫自定義的方法來根據鍵“z”和“c”更新文字。
  • 將函式操作與key_press_event繫結。
  • 繪製包含該圖形的畫布。
  • 使用文字對圖形進行動畫處理。
  • 要顯示圖形,請使用show()方法。

示例

from matplotlib import pyplot as plt, animation

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
text = ax.text(.5, .5, 'First Text')
def action(event):
   if event.key == "z":
      text.set_text("zoom")
   elif event.key == "c":
      text.set_text("cool")

fig.canvas.mpl_connect('key_press_event', action)
fig.canvas.draw()
animation = animation.ArtistAnimation(fig, [(text,)])

plt.show()

輸出

更新於:10-Jun-2021

2K+ 次瀏覽

開啟你的 職業 生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.