如何绘制可拖动的多边形 [英] How to plot a draggable polygon
问题描述
关于这个 绘制可拖动矩形的 matplotlib 示例 我试图对多边形做同样的事情.
Regarding this matplotlib example that plots draggable rectangles I tried to do the same with polygons.
到目前为止,我能够绘制一个多边形并将其拖动到画布上的某个位置.如果我松开鼠标按钮,我将无法再次移动多边形,这就是我的问题.我想通过每次鼠标按下来拖放多边形.
So far I am able to plot a polygon and drag it somewhere on the canvas. If I release the mouse button I won't be able to move the polygon again and thats my problem. I would like to drag and drop the polygon by every mouse press as often as I want to.
我还注意到,在移动多边形之后,我仍然可以单击该多边形以前的位置,然后再次拖动它.所以初始 geometry
必须保存在某个地方,但我想它应该被覆盖.
I also noticed that after moving the polygon I can still click on the position where the polygon used to be and drag it again. So the initial geometry
must be saved somewhere but I guess it should be overwritten instead.
正如下面的评论中所建议的,我将添加一个补丁而不是一个集合,因为我只会绘制一个多边形(请参阅注释掉的旧代码).此外,我关闭了多边形以证明您只能通过在多边形内部单击而不是通过单击其边缘来拖动补丁.
As suggested in the comments below I will add a patch instead of a collection as I will only plot a single polygon (see old code commented out). Additionally I closed the polygon to demonstrate that you can drag the patch only by clicking inside the polygon but not by clicking on its edges.
如果我想再次拖动多边形,它将自动跳回其初始位置.
If I want to drag the polygon a second time it automatically jumps back to its initial position.
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
#from matplotlib.collections import PatchCollection
class DraggablePolygon:
lock = None
def __init__(self, polygon):
self.poly = polygon
self.press = None
def connect(self):
'connect to all the events we need'
self.cidpress = self.poly.figure.canvas.mpl_connect(
'button_press_event', self.on_press)
self.cidrelease = self.poly.figure.canvas.mpl_connect(
'button_release_event', self.on_release)
self.cidmotion = self.poly.figure.canvas.mpl_connect(
'motion_notify_event', self.on_motion)
def on_press(self, event):
'on button press we will see if the mouse is over us and store some data'
if event.inaxes != self.poly.axes: return
if DraggablePolygon.lock is not None: return
contains, attrd = self.poly.contains(event)
if not contains: return
x0, y0 = geometry[0]
self.press = x0, y0, event.xdata, event.ydata
DraggablePolygon.lock = self
def on_motion(self, event):
'on motion we will move the rect if the mouse is over us'
if DraggablePolygon.lock is not self:
return
if event.inaxes != self.poly.axes: return
x0, y0, xpress, ypress = self.press
dx = event.xdata - xpress
dy = event.ydata - ypress
xdx = [i+dx for i,_ in geometry]
ydy = [i+dy for _,i in geometry]
self.newGeometry = [[a, b] for a, b in zip(xdx, ydy)]
#polygon = Polygon(self.newGeometry, closed=False, fill=False, linewidth=3, color='#F97306')
#patches = []
#patches.append(polygon)
#plt.cla()
#p = PatchCollection(patches, match_original=True)
#ax.add_collection(p)
self.poly.set_xy(newGeometry) # this will set the vertices of the polygon
self.poly.figure.canvas.draw()
def on_release(self, event):
'on release we reset the press data'
if DraggablePolygon.lock is not self:
return
self.press = None
DraggablePolygon.lock = None
def disconnect(self):
'disconnect all the stored connection ids'
self.poly.figure.canvas.mpl_disconnect(self.cidpress)
self.poly.figure.canvas.mpl_disconnect(self.cidrelease)
self.poly.figure.canvas.mpl_disconnect(self.cidmotion)
fig = plt.figure()
ax = fig.add_subplot(111)
geometry = [[0.0,0.0],[0.1,0.05],[0.2,0.15],[0.3,0.20],[0.4,0.25],[0.5,0.30],
[0.6,0.25],[0.7,0.15],[0.8,0.05],[0.9,0.025],[1.0,0.0]]
patches = []
polygon = plt.Polygon(geometry, closed=True, fill=False, linewidth=3, color='#F97306')
#patches.append(polygon)
#p = PatchCollection(patches, match_original=True)
#ax.add_collection(p)
ax.add_patch(polygon)
dp = DraggablePolygon(polygon)
dp.connect()
plt.show()
我假设 geometry
和 newGeometry
的定义必须在代码中的不同位置,但经过几次尝试后,我找不到可行的解决方案.有没有人发现我犯的错误?
I assume that the definition of geometry
and newGeometry
has to be on a different position inside the code but after a few attempts I couldn't find a working solution. Does anyone find the mistakes I made?
推荐答案
问题下面的注释最终帮助找到了有效的代码.也许这不是最好,最pythonic的方法,但它确实满足了我的要求.
The comments below the question helped to find a working code finally. Maybe it's not the best and most pythonic way but it does what I want.
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
class DraggablePolygon:
lock = None
def __init__(self):
print('__init__')
self.press = None
fig = plt.figure()
ax = fig.add_subplot(111)
self.geometry = [[0.0,0.0],[0.1,0.05],[0.2,0.15],[0.3,0.20],[0.4,0.25],[0.5,0.30],
[0.6,0.25],[0.7,0.15],[0.8,0.05],[0.9,0.025],[1.0,0.0]]
self.newGeometry = []
poly = plt.Polygon(self.geometry, closed=True, fill=False, linewidth=3, color='#F97306')
ax.add_patch(poly)
self.poly = poly
def connect(self):
'connect to all the events we need'
print('connect')
self.cidpress = self.poly.figure.canvas.mpl_connect(
'button_press_event', self.on_press)
self.cidrelease = self.poly.figure.canvas.mpl_connect(
'button_release_event', self.on_release)
self.cidmotion = self.poly.figure.canvas.mpl_connect(
'motion_notify_event', self.on_motion)
def on_press(self, event):
'on button press we will see if the mouse is over us and store some data'
print('on_press')
if event.inaxes != self.poly.axes: return
if DraggablePolygon.lock is not None: return
contains, attrd = self.poly.contains(event)
if not contains: return
if not self.newGeometry:
x0, y0 = self.geometry[0]
else:
x0, y0 = self.newGeometry[0]
self.press = x0, y0, event.xdata, event.ydata
DraggablePolygon.lock = self
def on_motion(self, event):
'on motion we will move the rect if the mouse is over us'
if DraggablePolygon.lock is not self:
return
if event.inaxes != self.poly.axes: return
x0, y0, xpress, ypress = self.press
dx = event.xdata - xpress
dy = event.ydata - ypress
xdx = [i+dx for i,_ in self.geometry]
ydy = [i+dy for _,i in self.geometry]
self.newGeometry = [[a, b] for a, b in zip(xdx, ydy)]
self.poly.set_xy(self.newGeometry)
self.poly.figure.canvas.draw()
def on_release(self, event):
'on release we reset the press data'
print('on_release')
if DraggablePolygon.lock is not self:
return
self.press = None
DraggablePolygon.lock = None
self.geometry = self.newGeometry
def disconnect(self):
'disconnect all the stored connection ids'
print('disconnect')
self.poly.figure.canvas.mpl_disconnect(self.cidpress)
self.poly.figure.canvas.mpl_disconnect(self.cidrelease)
self.poly.figure.canvas.mpl_disconnect(self.cidmotion)
dp = DraggablePolygon()
dp.connect()
plt.show()
这篇关于如何绘制可拖动的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!