[英]matplotlib: event handling and navigation for inset axes
為了在另一個之上顯示一個 plot 我可以
import matplotlib.pyplot as plt
fig = plt.figure()
ax0 = fig.add_axes([0.1,0.1,0.8,0.8], zorder=1)
ax0.set_title('ax0')
ax1 = fig.add_axes([0.175,0.5,0.3,0.3], zorder=2)
ax1.set_title('ax1')
ax1.set_facecolor('lightgray')
ax1.set_xlim(2,3)
ax1.set_ylim(2,3)
ax2 = ax0.inset_axes([0.575, 0.5, 0.375, 0.375])
ax2.set_title('ax2')
ax2.set_facecolor('gray')
ax2.set_xlim(4,5)
ax2.set_ylim(4,5)
plt.show()
不幸的是,這兩個選項都顯示出不穩定的交互行為:
問題:有沒有一種簡單的方法可以在鼠標事件在 ax1 或 ax2 內時禁用 ax0 的 info/zoom/pan,或者我是否必須通過覆蓋事件函數和基於 ax1/ 的邊界框進行坐標轉換的痛苦來 go ax2,甚至是導航欄的子類?
PS:我只需要 1. 或 2. 工作,而不是兩者。 1.如果我可以按照我的意願關閉/打開 ax0,似乎更接近於開始的請求行為......
只需使用ax0.set_navigate(False)
即可阻止 ax0 響應導航命令。
Diziet Asahi's answer connected to 'axes_enter_event'為選項 1 解決了它。我無法讓它直接用於插入軸。 此外,zorder 現在似乎並不重要,因此已將其刪除。
參考代碼:
import matplotlib.pyplot as plt
fig = plt.figure()
ax0 = fig.add_axes([0.1,0.1,0.8,0.8])
ax0.set_title('ax0')
ax1 = fig.add_axes([0.175,0.5,0.3,0.3])
ax1.set_title('ax1')
ax1.set_facecolor('lightgray')
ax1.set_xlim(2,3)
ax1.set_ylim(2,3)
def enter_axes(event):
if not event.inaxes:
return
if event.inaxes == ax1:
ax0.set_navigate(False)
elif event.inaxes == ax0:
ax0.set_navigate(True)
fig.canvas.mpl_connect('axes_enter_event', enter_axes)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.