我在这里找到一个代码示例:how can move rectangle patch with click mouse python3,每次将鼠标移动到图形上的某个位置时,都允许使用鼠标移动矩形补丁,我想在图像上使用此示例并添加相同的内容并用鼠标移动ractangle我在图像上添加了矩形,但是我不能用鼠标移动
我需要一些帮助,如何在图像中移动例如矩形的示例:
import numpy as np
import netCDF4
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def on_press(event):
xpress, ypress = event.xdata, event.ydata
w = rect.get_width()
h = rect.get_height()
rect.set_xy((xpress-w/2, ypress-h/2))
ax.lines = []
ax.axvline(xpress, c='b')
ax.axhline(ypress, c='b')
fig.canvas.draw()
fic='ainfrared_01905080000.nc'
path='/home/data/'
nc = netCDF4.Dataset(path+fic,'r')
IR=nc.variables['IR][:]
x = y = 500
# Display the image
axes = plt.subplot(111)
axes.imshow(IR,origin='lower', cmap=plt.cm.gist_yarg)
rect = patches.Rectangle((x,y),800,800,linewidth=1,edgecolor='g',facecolor='none')
# Add the patch to the Axes
axes.add_patch(rect)
plt.show()
提前谢谢