我試圖修改我在stackoverflow論壇上找到的一些代碼(How can I plot the same figure standalone and in a subplot in Matplotlib?第一個答案)。點擊沒有事件連接
如果單擊該子圖(僅顯示畫布上單擊的子圖),並在再次單擊時放大(顯示畫布上的所有子圖),它基本上會放大子圖。我嘗試修改代碼以通過多種方式將其調整到我的程序,但是我仍然遇到同樣的問題。帶有子圖的圖形畫布已正確創建,並且輸入了縮放子圖課程,但它似乎無法與我的點擊事件(它不會輸入'on_click')相關聯。
我試着找出什麼是錯誤的,並嘗試了幾個修改,但仍然遇到同樣的問題。我無法使用代碼,因爲它不適合我程序的其餘部分,因此我從中檢索到的主題中顯示的代碼。
import numpy as np
from matplotlib import pyplot as plt
class ZoomingSubplots(object):
''' zoom to subplot if subplot is clicked, unzoom when clicked again'''
def __init__(self, fig):
print 'class entered'
self.fig = fig
self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def zoom(self, selected_ax):
for ax in self.axes.flat:
ax.set_visible(False)
self._original_size = selected_ax.get_position()
selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
selected_ax.set_visible(True)
self._zoomed = True
def unzoom(self, selected_ax):
selected_ax.set_position(self._original_size)
for ax in self.axes.flat:
ax.set_visible(True)
self._zoomed = False
def on_click(self, event):
print 'click event'
if event.inaxes is None:
return
if self._zoomed:
self.unzoom(event.inaxes)
else:
self.zoom(event.inaxes)
self.fig.canvas.draw()
#make a figure with 9 random imshows
plots = 9 #number of plots
plotrows = 3 #subplot rows
plotcols = 3 #subplot columns
fig = plt.figure()
for i in range(plots):
arr = np.random.rand(10,10)
ax = fig.add_subplot(plotrows, plotcols, i+1)
ax.imshow(arr, interpolation = 'nearest')
ax.set_title('%s %i' % ('plot', i+1), fontsize = 10)
# connect with zoom class
ZoomingSubplots(fig)
plt.show()
一個扭捏這一個簡單的代碼中,你可以看到同樣的問題:
import numpy as np
from matplotlib import pyplot as plt
class ZoomingSubplots(object):
def __init__(self, fig):
print 'class entered'
self.fig = fig
self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def on_click(self, event):
print 'click event'
#make a figure with 9 random imshows
fig = plt.figure()
for i in range(9):
arr = np.random.rand(10,10)
ax = fig.add_subplot(3, 3, i+1)
ax.imshow(arr, interpolation = 'nearest')
# connect with zoom class
ZoomingSubplots(fig)
plt.show()