Appending python class that uses interactive mode with user input? - python

I have a class which allows the user to create a region using the mouse. Now I want to create multiple regions by appending objects to a list.
However, when I try to append it just runs once and I get 2 copies of the same region. It seems that both are running in parallel at the same time. Is there an easy workaround for this problem?
import numpy as np
import sys
import matplotlib.pyplot as plt
import matplotlib.path as mplPath
import matplotlib.image as mpimg
class create_region():
def __init__(self, img):
if isinstance(img, np.ndarray):
plt.imshow(img)
fig = plt.gcf()
fig.set_size_inches(12,9)
else:
print('Error: please include an image np.ndarray, create_region(img)')
return
self.img = img
self.img_size = np.shape(img)
self.line = None
self.roicolor = (.415,1,.302) # Use neon green to stand out
self.markersize = 4
self.x_pts = []
self.y_pts = []
self.previous_pt = []
self.start_point = []
self.end_point = []
self.fig = fig
self.ax = plt.gca()
self.__ID1 = self.fig.canvas.mpl_connect(
'motion_notify_event', self.__motion_notify_callback)
self.__ID2 = self.fig.canvas.mpl_connect(
'button_press_event', self.__button_press_callback)
if sys.flags.interactive:
plt.show(block=False)
else:
plt.show()
def __motion_notify_callback(self, event):
if event.inaxes:
#ax = event.inaxes
x, y = event.xdata, event.ydata
# Move line around
if (event.button == None or event.button == 1) and self.line != None:
self.line.set_data([self.previous_pt[0], x],
[self.previous_pt[1], y])
self.fig.canvas.draw()
def __button_press_callback(self, event):
if event.inaxes:
x, y = event.xdata, event.ydata
ax = event.inaxes
# If you press the left button, single click
if event.button == 1 and event.dblclick == False:
if self.line == None: # if there is no line, create a line
self.line = plt.Line2D([x, x],
[y, y],
marker='o',
color=self.roicolor,
markersize = self.markersize)
self.start_point = [x,y]
self.previous_pt = self.start_point
self.x_pts=[x]
self.y_pts=[y]
ax.add_line(self.line)
self.fig.canvas.draw()
# add a segment
else: # if there is a line, create a segment
self.line = plt.Line2D([self.previous_pt[0], x],
[self.previous_pt[1], y],
marker = 'o',color=self.roicolor,
markersize = self.markersize)
self.previous_pt = [x,y]
self.x_pts.append(x)
self.y_pts.append(y)
event.inaxes.add_line(self.line)
self.fig.canvas.draw()
# close the loop and disconnect
elif ((event.button == 1 and event.dblclick==True) or
(event.button == 3 and event.dblclick==False)) and self.line != None:
self.fig.canvas.mpl_disconnect(self.__ID1) #joerg
self.fig.canvas.mpl_disconnect(self.__ID2) #joerg
self.line.set_data([self.previous_pt[0],
self.start_point[0]],
[self.previous_pt[1],
self.start_point[1]])
ax.add_line(self.line)
self.fig.canvas.draw()
self.line = None
if sys.flags.interactive:
pass
else:
#figure has to be closed so that code can continue
plt.close(self.fig)
# create zeros image
img=np.zeros((768,1024),dtype='uint8')
# try to append 2 different user regions to list
# seems both run simultaneously, returns 2 copies of 1 region
M = []
for i in range(2):
M.append(create_region(img))

Related

Moving verticle lines around in a Matplotlib plot?

I have been looking around for a while and cant seem to find much on moving elements around after they have been plotted. I have a series of vertical lines plotted and if they are to close together i would like to be able to space then out a bit more. The issue is that they cant be moved to the left ever. I have code that can evenly space all these with that constraint but now I0m focusing on just making sure they are not clumped together. here is an example picture of what I'm working with:
full view
zoomed in on a problem
Thee question really is if there is a way I am able to click and drag these red lines around so they are not to close to others? i need to be able to retrieve the new positions of all the lines after this is done after i have made them all nicely spaced but i assume this would be fairly simple after i have this mechanic in place?
I'm not looking for specific implementation just some help on places I could look to be able to make this click and drag utility possible.
This may not be possible in matplotlib itself and i may have to look outward into making some GUI to do this but i have no experience in this so probably not the best solution although probably the best.
Any insight into how I might be able to achieve the click drag utility will be greatly appreciated!
-Thank you
got it working from a movable polygon example here: https://matplotlib.org/stable/gallery/event_handling/poly_editor.html
import numpy as np
import pandas as pd
from matplotlib.lines import Line2D
from matplotlib.artist import Artist
global new_freqs
def dist(x, y):
"""
Return the distance between two points.
"""
d = x - y
return np.sqrt(np.dot(d, d))
def dist_point_to_segment(p, s0, s1):
"""
Get the distance of a point to a segment.
*p*, *s0*, *s1* are *xy* sequences
This algorithm from
http://www.geomalgorithms.com/algorithms.html
"""
v = s1 - s0
w = p - s0
c1 = np.dot(w, v)
if c1 <= 0:
return dist(p, s0)
c2 = np.dot(v, v)
if c2 <= c1:
return dist(p, s1)
b = c1 / c2
pb = s0 + b * v
return dist(p, pb)
class PolygonInteractor:
"""
A polygon editor.
Key-bindings
't' toggle vertex markers on and off. When vertex markers are on,
you can move them, delete them
'd' delete the vertex under point
'i' insert a vertex at point. You must be within epsilon of the
line connecting two existing vertices
"""
showverts = True
epsilon = 5 # max pixel distance to count as a vertex hit
def __init__(self, ax, poly, start_freqs):
if poly.figure is None:
raise RuntimeError('You must first add the polygon to a figure '
'or canvas before defining the interactor')
self.ax = ax
canvas = poly.figure.canvas
self.poly = poly
x, y = zip(*self.poly.xy)
self.line = Line2D(x, y,
marker='o', markerfacecolor='r',
animated=True)
self.ax.add_line(self.line)
ax.vlines(x,linestyle="--", ymin=0.0, ymax=1, alpha=0.9, color=col, gid="new_lines")
self.original_freqs = start_freqs #variable for storing the starting frequencies
self.orig_cols = col # array or starting colours
self.new_cols = self.orig_cols # array or new colours, same as original to begin with
self.issue_spacing = 1.0e6 # variable to store the value of kids to close together
self.cid = self.poly.add_callback(self.poly_changed)
self._ind = None # the active vert
canvas.mpl_connect('draw_event', self.on_draw)
canvas.mpl_connect('button_press_event', self.on_button_press)
canvas.mpl_connect('key_press_event', self.on_key_press)
canvas.mpl_connect('button_release_event', self.on_button_release)
canvas.mpl_connect('motion_notify_event', self.on_mouse_move)
self.canvas = canvas
def draw_new_positions(self):
for i in range(len(self.new_cols)):
if self.poly.xy[i,0] != self.original_freqs[i]:
if self.poly.xy[i,0] < self.original_freqs[i]:
self.new_cols[i] = "purple" #if the kid has moved backwward show purple
elif (self.poly.xy[i+1,0]-self.poly.xy[i,0]) < self.issue_spacing or (self.poly.xy[i,0]-self.poly.xy[i-1,0]) < self.issue_spacing :
self.new_cols[i] = "black" #if the kid to close the the ones next to it show black
else:
self.new_cols[i] = "orange" #if the kid has moved and is positioned ok show orange
else:
self.new_cols[i] = self.orig_cols[i]
new_lines = self.ax.vlines(self.poly.xy[:-1,0], ymin=-1, ymax=0, linestyle="--", color=self.new_cols, alpha=0.9, gid="new_lines") #new line to move where mouse is
self.ax.draw_artist(new_lines) # drawing the line on moving the mouse
self.canvas.blit(self.ax.bbox) # blitting the canvas to render moving
new_lines.remove()
def on_draw(self, event):
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.line)
# do not need to blit here, this will fire before the screen is
# updated
def poly_changed(self, poly):
"""This method is called whenever the pathpatch object is called."""
# only copy the artist props to the line (except visibility)
vis = self.line.get_visible()
Artist.update_from(self.line, poly)
self.line.set_visible(vis) # don't use the poly visibility state
def get_ind_under_point(self, event):
"""
Return the index of the point closest to the event position or *None*
if no point is within ``self.epsilon`` to the event position.
"""
# display coords
xy = np.asarray(self.poly.xy)
xyt = self.poly.get_transform().transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
d = np.hypot(xt - event.x, yt - event.y)
indseq, = np.nonzero(d == d.min())
ind = indseq[0]
if d[ind] >= self.epsilon:
ind = None
return ind
def on_button_press(self, event):
"""Callback for mouse button presses."""
if not self.showverts:
return
if event.inaxes is None:
return
if event.button != 1:
return
self._ind = self.get_ind_under_point(event)
def on_button_release(self, event):
"""Callback for mouse button releases."""
if not self.showverts:
return
if event.button != 1:
return
self._ind = None
def on_key_press(self, event):
"""Callback for key presses."""
if not event.inaxes:
return
if event.key == 't': #toggles the movable points on and off
self.showverts = not self.showverts
self.line.set_visible(self.showverts)
if not self.showverts:
self._ind = None
elif event.key == ' ': #prints the x vals of all polygon points (which are the new frequencies) to the console
new_freqs = self.poly.xy[:,0]
for i in range(len(new_freqs)-1):
print("{:.1f},".format(new_freqs[i]))
# print(len(new_freqs))
elif event.key == 'l': #save new frequencies to csv file and show final plot
new_freqs = self.poly.xy[:-1,0]
new_data = 0
new_data = np.zeros((len(new_freqs), 2))
new_data[:,0] = data["kid_id"]
new_data[:,1] = new_freqs
new_data_df = pd.DataFrame(data=new_data, columns=["kid_id", "f0"]) #makes a new data frame to save to csv with all new positions
new_data_df.to_csv("new_kid_positions.csv", index=False)
plt.close()
plt.figure("new array", dpi=150)
for i in range(len(new_data_df["f0"])):
if self.poly.xy[i,0] == self.original_freqs[i]:
col="green"
else:
col="orange"
plt.axvline(new_data_df["f0"][i]/1e6, color=col, linestyle="--", linewidth=1.5, alpha=0.9)
plt.plot([],[], color="orange", label="Moved")
plt.plot([],[], color="green", label="Not moved")
plt.legend(loc="best")
plt.xlabel("Frequency (MHz)")
plt.ylabel("")
plt.title("Altered array")
plt.grid()
plt.show()
if self.line.stale:
self.canvas.draw_idle()
def on_mouse_move(self, event):
"""Callback for mouse movements."""
if not self.showverts:
return
if self._ind is None:
return
if event.inaxes is None:
return
if event.button != 1:
self.moving_line.remove()
return
x, y = event.xdata, 0
self.poly.xy[self._ind] = x, y
if self._ind == 0:
self.poly.xy[-1] = x, y
elif self._ind == len(self.poly.xy) - 1:
self.poly.xy[0] = x, y
self.line.set_data(zip(*self.poly.xy))
# self.remove_series()
# f = x
# self.add_series(f, "new_lines", len(self.poly.xy[:,0])-1)
# ax.axvline(x, ymin=-1, ymax=1, linestyle="--", color="orange", alpha=0.9, animated=True)
self.canvas.restore_region(self.background)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.line)
# self.moving_line = self.ax.axvline(x, ymin=-1, ymax=1, linestyle="--", color="orange", alpha=0.9) #new line to move where mouse is
# self.ax.draw_artist(self.moving_line) # drawing the line on moving the mouse
# self.moving_line.remove()
self.draw_new_positions()
self.canvas.blit(self.ax.bbox) # blitting the canvas to render moving
if __name__ == '__main__':
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
#reading in freequency positions
data = pd.read_csv("sorted_kids.csv")
#getting color for each line
col = [] #empty list
for i in range(len(data["issue"])):
if data["issue"][i] == 1:
col.append("red")
else:
col.append("green")
# col = np.array(col)
xs = data["f0"]
ys = np.zeros_like(xs)
poly = Polygon(np.column_stack([xs, ys]), animated=True)
fig, ax = plt.subplots(dpi=150, figsize=(12,6))
ax.add_patch(poly)
p = PolygonInteractor(ax, poly, xs)
ax.set_title('Click a point to drag. spacebar=print freqs. T=toggle move. L=save and show final solution')
ax.plot([],[], color="black", alpha=0.0, label=r"TOP: ORIGINAL ARRAY")
ax.plot([],[], color="red", label="to close")
ax.plot([],[], color="green", label="spacing ok")
ax.plot([],[], color="black", alpha=0.0, label="\nBOT: ALTEERED ARRAY")
ax.plot([],[], color="red", label="orig position & to close")
ax.plot([],[], color="green", label="orig position & ok")
ax.plot([],[], color="purple", label="moved backward!")
ax.plot([],[], color="black", label="to close still")
ax.plot([],[], color="orange", label="moved & ok")
ax.legend(loc=1, fontsize=6)
plt.xlabel("Frequency (Hz)")
ax.set_xlim((min(data["f0"])-10e6, max(data["f0"])+10e6))
ax.set_ylim((-1.5, 1.5))
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
ax.axes.get_yaxis().set_visible(False)
plt.show()

Python: draw an interactive line (arc) with the mouse on an image

I'm doing a Python program to fit Bezier curves to the eye contour (for example one curve that is adapted to the bottom line of the brow and the other to the lid's contour ). In order to achieve this, I'm trying to put the image of the eye as the background of a plot, and then interactive draw a line with the mousse that fits the best. My problem is that I need an arc and not a circle as I have done but I don't know how to solve this.
My initial image and the circle I have to fit but I only want an arc. The line fits the brow after using my mouse (the pink part is what I want). It's important to say I have to convert this arc or line into a Bézier curve, I have a program that does this function but It will be better If I could directly draw a Bezier line in the image but I don't know-how so the main problem is to have an arc or line but nos a circle because I need an open curve. I have to use the Bezier lines because I have to calculate some medical parameters with them, so I need their coordinates.
import numpy as np
import matplotlib
import shapely
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from matplotlib.lines import Line2D
from matplotlib.artist import Artist
from matplotlib.bezier import find_control_points
class PolygonInteractor(object):
"""
A polygon editor.
https://matplotlib.org/gallery/event_handling/poly_editor.html
Key-bindings
't' toggle vertex markers on and off. When vertex markers are on,
you can move them, delete them
'd' delete the vertex under point
'i' insert a vertex at point. You must be within epsilon of the
line connecting two existing vertices
"""
showverts = True
epsilon = 5 # max pixel distance to count as a vertex hit
def __init__(self, ax, poly, visible=False):
if poly.figure is None:
raise RuntimeError('You must first add the polygon to a figure '
'or canvas before defining the interactor')
self.ax = ax
canvas = poly.figure.canvas
self.poly = poly
self.poly.set_visible(visible)
x, y = zip(*self.poly.xy)
self.line = Line2D(x, y, ls="",
marker='*', linewidth=1, markerfacecolor='green',
animated=True)
self.ax.add_line(self.line)
self.cid = self.poly.add_callback(self.poly_changed)
self._ind = None # the active vert
canvas.mpl_connect('draw_event', self.draw_callback)
canvas.mpl_connect('button_press_event', self.button_press_callback)
canvas.mpl_connect('key_press_event', self.key_press_callback)
canvas.mpl_connect('button_release_event', self.button_release_callback)
canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
self.canvas = canvas
x,y = self.interpolate()
self.line2 = Line2D(x, y, animated=True)
self.ax.add_line(self.line2)
def interpolate(self):
x, y = self.poly.xy[:].T
i = np.arange(len(x))
interp_i = np.linspace(0, i.max(), 100 * i.max())
xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)
return xi,yi
def draw_callback(self, event):
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.line)
self.ax.draw_artist(self.line2)
# do not need to blit here, this will fire before the screen is
# updated
def poly_changed(self, poly):
'this method is called whenever the polygon object is called'
# only copy the artist props to the line (except visibility)
vis = self.line.get_visible()
Artist.update_from(self.line, poly)
self.line.set_visible(vis) # don't use the poly visibility state
def get_ind_under_point(self, event):
'get the index of the vertex under point if within epsilon tolerance'
# display coords
xy = np.asarray(self.poly.xy)
xyt = self.poly.get_transform().transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
d = np.hypot(xt - event.x, yt - event.y)
indseq, = np.nonzero(d == d.min())
ind = indseq[0]
if d[ind] >= self.epsilon:
ind = None
return ind
def button_press_callback(self, event):
'whenever a mouse button is pressed'
if not self.showverts:
return
if event.inaxes is None:
return
if event.button != 1:
return
self._ind = self.get_ind_under_point(event)
def button_release_callback(self, event):
'whenever a mouse button is released'
if not self.showverts:
return
if event.button != 1:
return
self._ind = None
def key_press_callback(self, event):
'whenever a key is pressed'
if not event.inaxes:
return
if event.key == 't':
self.showverts = not self.showverts
self.line.set_visible(self.showverts)
if not self.showverts:
self._ind = None
elif event.key == 'd':
ind = self.get_ind_under_point(event)
if ind is not None:
self.poly.xy = np.delete(self.poly.xy,
ind, axis=0)
self.line.set_data(zip(*self.poly.xy))
elif event.key == 'i':
xys = self.poly.get_transform().transform(self.poly.xy)
p = event.x, event.y # display coords
for i in range(len(xys) - 1):
s0 = xys[i]
s1 = xys[i + 1]
d = dist_point_to_segment(p, s0, s1)
if d <= self.epsilon:
self.poly.xy = np.insert(
self.poly.xy, i+1,
[event.xdata, event.ydata],
axis=0)
self.line.set_data(zip(*self.poly.xy))
break
if self.line.stale:
self.canvas.draw_idle()
def motion_notify_callback(self, event):
'on mouse movement'
if not self.showverts:
return
if self._ind is None:
return
if event.inaxes is None:
return
if event.button != 1:
return
x, y = event.xdata, event.ydata
self.poly.xy[self._ind] = x, y
if self._ind == 0:
self.poly.xy[-1] = x, y
elif self._ind == len(self.poly.xy) - 1:
self.poly.xy[0] = x, y
self.line.set_data(zip(*self.poly.xy))
x,y = self.interpolate()
self.line2.set_data(x,y)
self.canvas.restore_region(self.background)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.line)
self.ax.draw_artist(self.line2)
self.canvas.blit(self.ax.bbox)
if __name__ == '__main__':
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
theta = np.arange(0, 2*np.pi, 0.1)
r = 1.5
xs = r*np.cos(theta)
ys = r*np.sin(theta)
xs = (921, 951, 993, 1000)
ys = (1181, 1230, 1243, 257)
poly = Polygon(list(zip(xs, ys)), animated=True)
#poly = LineString([(0, 0), (1, 1)])
img = plt.imread("/Users/raquel/Desktop/TFG/IMÁGENES/Unknown.jpeg")
fig, ax = plt.subplots()
img = ax.imshow(img, extent=[0, 1300, 0, 1300])
ax.add_patch(poly)
p = PolygonInteractor(ax, poly, visible=False)
ax.set_title('Click and drag a point to move it')
x = ax.set_xlim((0, 1300))
y = ax.set_ylim((0, 1300))
plt.show()

Draggable Matplotlib scatter with an animated plot using blitting

I recently asked a question about creating a draggable scatter and with the help of someone I was able to come up with a working example. See 'PathCollection' not iterable - creating a draggable scatter plot.
I'm now trying to use the DraggableScatter class I created with an animated plot using blitting.
I've tried to attach the DraggableScatter class in multiple places, e.g., after initializing the scatter, in the init function and in the update function. In the first cases, the DraggableScatter's scatter is empty, which makes sense but obviously doesn't work. In the other two, the clicks seem not to be captured.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
class DraggableScatter():
epsilon = 5
def __init__(self, scatter):
self.scatter = scatter
self._ind = None
self.ax = scatter.axes
self.canvas = self.ax.figure.canvas
self.canvas.mpl_connect('button_press_event', self.button_press_callback)
self.canvas.mpl_connect('button_release_event', self.button_release_callback)
self.canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
def get_ind_under_point(self, event):
xy = np.asarray(self.scatter.get_offsets())
xyt = self.ax.transData.transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
ind = d.argmin()
if d[ind] >= self.epsilon:
ind = None
return ind
def button_press_callback(self, event):
if event.inaxes is None:
return
if event.button != 1:
return
self._ind = self.get_ind_under_point(event)
def button_release_callback(self, event):
if event.button != 1:
return
self._ind = None
def motion_notify_callback(self, event):
if self._ind is None:
return
if event.inaxes is None:
return
if event.button != 1:
return
x, y = event.xdata, event.ydata
xy = np.asarray(self.scatter.get_offsets())
xy[self._ind] = np.array([x, y])
self.scatter.set_offsets(xy)
self.canvas.draw_idle()
fig, ax = plt.subplots(1, 1)
scatter = ax.scatter([],[])
def init():
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
return scatter,
def update(frame):
scatter = ax.scatter(np.random.rand(10), np.random.rand(10), marker ='o')
ds = DraggableScatter(scatter)
return scatter,
ani = FuncAnimation(fig=fig, func=update, init_func=init, blit=True, interval=5000)
plt.show()
What is the right way to do this?
This works with the GTK3Cairo backend. (It does not work using TkAgg, Qt4Agg, Qt5Agg, GTK3Agg.)
Create the DraggableScatter once, then use ds.scatter.set_offsets to change the scatter point data inside the update function:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
class DraggableScatter():
epsilon = 5
def __init__(self, scatter):
self.scatter = scatter
self._ind = None
self.ax = scatter.axes
self.canvas = self.ax.figure.canvas
self.canvas.mpl_connect('button_press_event', self.button_press_callback)
self.canvas.mpl_connect('button_release_event', self.button_release_callback)
self.canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
def get_ind_under_point(self, event):
xy = np.asarray(self.scatter.get_offsets())
xyt = self.ax.transData.transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
ind = d.argmin()
if d[ind] >= self.epsilon:
ind = None
return ind
def button_press_callback(self, event):
if event.inaxes is None:
return
if event.button != 1:
return
self._ind = self.get_ind_under_point(event)
def button_release_callback(self, event):
if event.button != 1:
return
self._ind = None
def motion_notify_callback(self, event):
if self._ind is None:
return
if event.inaxes is None:
return
if event.button != 1:
return
x, y = event.xdata, event.ydata
xy = np.asarray(self.scatter.get_offsets())
xy[self._ind] = np.array([x, y])
self.scatter.set_offsets(xy)
self.canvas.draw_idle()
fig, ax = plt.subplots(1, 1)
scatter = ax.scatter(np.random.rand(10), np.random.rand(10), marker ='o')
ds = DraggableScatter(scatter)
def init():
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
return ds.scatter,
def update(frame, ds):
x, y = np.random.rand(10), np.random.rand(10)
ds.scatter.set_offsets(np.column_stack([x, y]))
return ds.scatter,
ani = FuncAnimation(fig=fig, func=update, init_func=init, fargs=[ds], blit=True,
interval=5000)
plt.show()

Python: key pressed event ignores conditional statement

I’m a novice working on my first python project. I started with this "poly_editor.py" example from the matplotlib documentation since my program is doing similar things with an interactive graph. I’ve commented the 3 locations where I made additions and modifications. Basically, I just made the list of vertices (self.d) visible so I could check to not add more than 100.
"""
This is an example to show how to build cross-GUI applications using
matplotlib event handling to interact with objects on the canvas
"""
import numpy as np
from matplotlib.lines import Line2D
from matplotlib.artist import Artist
from matplotlib.mlab import dist_point_to_segment
class PolygonInteractor(object):
"""
An polygon editor.
Key-bindings
't' toggle vertex markers on and off. When vertex markers are on,
you can move them, delete them
'd' delete the vertex under point
'i' insert a vertex at point. You must be within epsilon of the
line connecting two existing vertices
"""
showverts = True
epsilon = 5 # max pixel distance to count as a vertex hit
def __init__(self, ax, poly):
if poly.figure is None:
raise RuntimeError('You must first add the polygon to a figure or canvas before defining the interactor')
self.ax = ax
canvas = poly.figure.canvas
self.poly = poly
######### added ########
self.d = []
######## end add #######
x, y = zip(*self.poly.xy)
self.line = Line2D(x, y, marker='o', markerfacecolor='r', animated=True)
self.ax.add_line(self.line)
#self._update_line(poly)
cid = self.poly.add_callback(self.poly_changed)
self._ind = None # the active vert
canvas.mpl_connect('draw_event', self.draw_callback)
canvas.mpl_connect('button_press_event', self.button_press_callback)
canvas.mpl_connect('key_press_event', self.key_press_callback)
canvas.mpl_connect('button_release_event', self.button_release_callback)
canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
self.canvas = canvas
def draw_callback(self, event):
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.line)
self.canvas.blit(self.ax.bbox)
def poly_changed(self, poly):
'this method is called whenever the polygon object is called'
# only copy the artist props to the line (except visibility)
vis = self.line.get_visible()
Artist.update_from(self.line, poly)
self.line.set_visible(vis) # don't use the poly visibility state
def get_ind_under_point(self, event):
'get the index of the vertex under point if within epsilon tolerance'
#################### changed code ######################
# display coords
xy = np.asarray(self.poly.xy)
xyt = self.poly.get_transform().transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
self.d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
indseq = np.nonzero(np.equal(self.d, np.amin(self.d)))[0]
ind = indseq[0]
print(len(self.d))
if self.d[ind] >= self.epsilon:
ind = None
#################### end changes #######################
return ind
def button_press_callback(self, event):
'whenever a mouse button is pressed'
if not self.showverts:
return
if event.inaxes is None:
return
if event.button != 1:
return
self._ind = self.get_ind_under_point(event)
def button_release_callback(self, event):
'whenever a mouse button is released'
if not self.showverts:
return
if event.button != 1:
return
self._ind = None
def key_press_callback(self, event):
'whenever a key is pressed'
if not event.inaxes:
return
if event.key == 't':
self.showverts = not self.showverts
self.line.set_visible(self.showverts)
if not self.showverts:
self._ind = None
elif event.key == 'd':
ind = self.get_ind_under_point(event)
if ind is not None:
self.poly.xy = [tup for i, tup in enumerate(self.poly.xy) if i != ind]
self.line.set_data(zip(*self.poly.xy))
elif event.key == 'i':
########### added ###########
if len(self.d) < 100:
########## end add ##########
xys = self.poly.get_transform().transform(self.poly.xy)
p = event.x, event.y # display coords
for i in range(len(xys) - 1):
s0 = xys[i]
s1 = xys[i + 1]
d = dist_point_to_segment(p, s0, s1)
if d <= self.epsilon:
self.poly.xy = np.array(
list(self.poly.xy[:i]) +
[(event.xdata, event.ydata)] +
list(self.poly.xy[i:]))
self.line.set_data(zip(*self.poly.xy))
break
self.canvas.draw()
def motion_notify_callback(self, event):
'on mouse movement'
if not self.showverts:
return
if self._ind is None:
return
if event.inaxes is None:
return
if event.button != 1:
return
x, y = event.xdata, event.ydata
self.poly.xy[self._ind] = x, y
if self._ind == 0:
self.poly.xy[-1] = x, y
elif self._ind == len(self.poly.xy) - 1:
self.poly.xy[0] = x, y
self.line.set_data(zip(*self.poly.xy))
self.canvas.restore_region(self.background)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.line)
self.canvas.blit(self.ax.bbox)
if __name__ == '__main__':
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
theta = np.arange(0, 2*np.pi, 0.1)
r = 1.5
xs = r*np.cos(theta)
ys = r*np.sin(theta)
poly = Polygon(list(zip(xs, ys)), animated=True)
fig, ax = plt.subplots()
ax.add_patch(poly)
p = PolygonInteractor(ax, poly)
#ax.add_line(p.line)
ax.set_title('Click and drag a point to move it')
ax.set_xlim((-2, 2))
ax.set_ylim((-2, 2))
plt.show()
The problem is: if you click on an edge and hold down the “i” key to insert vertices, it will just continue to add beyond 100, ignoring the “if len(self.d) < 100:” statement.
I found this “bug” by accidentally leaning on the keyboard… lol. Is there any way to fix this behavior? My current work around is to bind it to the release event, but rapidly pressing the key can still cause the issue. My next option is to just allow additional points by a popup menu I guess, but I’d prefer a solution to a work around.

Interactive Line in matplotlib

I'm trying to make an interactive plot using matplotlib that creates a line segment with two handles at the endpoints. You can click and drag the handles and the line will refresh to match the positions specified in this way, in a similar fashion to this matplotlib example poly_editor: (if you see the example, imagine that I want the same thing but with just one edge of the polygon).
I have tried altering the poly_editor code to work with just the Line2D element, and my program runs without any errors, except that it doesn't draw anything on the axis at all. I think it might be an error in the scope of the variables or something to do with the draw calls from matplotlib. Any guidance as to what the errors are would be greatly appreciated.
Edit: I advanced some more, simplified the code and now I can get it to draw the line and print the index of the nearest vertex within epsilon distance, but the line stays stationary and does not animate. The updated code is bellow
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.lines import Line2D
class LineBuilder(object):
epsilon = 0.5
def __init__(self, line):
canvas = line.figure.canvas
self.canvas = canvas
self.line = line
self.axes = line.axes
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.ind = None
canvas.mpl_connect('button_press_event', self.button_press_callback)
canvas.mpl_connect('button_release_event', self.button_release_callback)
canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
def get_ind(self, event):
x = np.array(self.line.get_xdata())
y = np.array(self.line.get_ydata())
d = np.sqrt((x-event.xdata)**2 + (y - event.ydata)**2)
if min(d) > self.epsilon:
return None
if d[0] < d[1]:
return 0
else:
return 1
def button_press_callback(self, event):
if event.button != 1:
return
self.ind = self.get_ind(event)
print(self.ind)
self.line.set_animated(True)
self.canvas.draw()
self.background = self.canvas.copy_from_bbox(self.line.axes.bbox)
self.axes.draw_artist(self.line)
self.canvas.blit(self.axes.bbox)
def button_release_callback(self, event):
if event.button != 1:
return
self.ind = None
self.line.set_animated(False)
self.background = None
self.line.figure.canvas.draw()
def motion_notify_callback(self, event):
if event.inaxes != self.line.axes:
return
if event.button != 1:
return
if self.ind is None:
return
self.xs[self.ind] = event.xdata
self.ys[self.ind] = event.ydata
self.line.set_data(self.xs, self.ys)
self.canvas.restore_region(self.background)
self.axes.draw_artist(self.line)
self.canvas.blit(self.axes.bbox)
if __name__ == '__main__':
fig, ax = plt.subplots()
line = Line2D([0,1], [0,1], marker='o', markerfacecolor='red')
ax.add_line(line)
linebuilder = LineBuilder(line)
ax.set_title('click to create lines')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
plt.show()
Thanks in advance, Kevin.
Okay, I solved the problem. The new code (above) actually works, there was a mistake in it. The mpl_connect call for the motion notify event had the wrong event type, now it is working as intended.
I am new here so hope I don't make to many mistakes by replying to this self.replied question. :)
First thanks for posting this, it helped me a lot by saving some time, I wanted almost exactly this code. I did some updates that I propose here, so that it's possible to manipulate more than two points, and use the key handling events to create or delete points in the line, as the PolygonInteractor does.
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import numpy as np
def dist(x, y):
"""
Return the distance between two points.
"""
d = x - y
return np.sqrt(np.dot(d, d))
def dist_point_to_segment(p, s0, s1):
"""
Get the distance of a point to a segment.
*p*, *s0*, *s1* are *xy* sequences
This algorithm from
http://geomalgorithms.com/a02-_lines.html
"""
v = s1 - s0
w = p - s0
c1 = np.dot(w, v)
if c1 <= 0:
return dist(p, s0)
c2 = np.dot(v, v)
if c2 <= c1:
return dist(p, s1)
b = c1 / c2
pb = s0 + b * v
return dist(p, pb)
class LineBuilder(object):
epsilon = 30 #in pixels
def __init__(self, line):
canvas = line.figure.canvas
self.canvas = canvas
self.line = line
self.axes = line.axes
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.ind = None
canvas.mpl_connect('button_press_event', self.button_press_callback)
canvas.mpl_connect('button_release_event', self.button_release_callback)
canvas.mpl_connect('key_press_event', self.key_press_callback)
canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
def get_ind(self, event):
xy = np.asarray(self.line._xy)
xyt = self.line.get_transform().transform(xy)
x, y = xyt[:, 0], xyt[:, 1]
d = np.sqrt((x-event.x)**2 + (y - event.y)**2)
indseq, = np.nonzero(d == d.min())
ind = indseq[0]
if d[ind] >= self.epsilon:
ind = None
return ind
def button_press_callback(self, event):
if event.button != 1:
return
if event.inaxes is None:
return
self.ind = self.get_ind(event)
print(self.ind)
self.line.set_animated(True)
self.canvas.draw()
self.background = self.canvas.copy_from_bbox(self.line.axes.bbox)
self.axes.draw_artist(self.line)
self.canvas.blit(self.axes.bbox)
def button_release_callback(self, event):
if event.button != 1:
return
self.ind = None
self.line.set_animated(False)
self.background = None
self.line.figure.canvas.draw()
def motion_notify_callback(self, event):
if event.inaxes != self.line.axes:
return
if event.button != 1:
return
if self.ind is None:
return
self.xs[self.ind] = event.xdata
self.ys[self.ind] = event.ydata
self.line.set_data(self.xs, self.ys)
self.canvas.restore_region(self.background)
self.axes.draw_artist(self.line)
self.canvas.blit(self.axes.bbox)
def key_press_callback(self, event):
"""Callback for key presses."""
if not event.inaxes:
return
elif event.key == 'd':
ind = self.get_ind(event)
if ind is not None and len(self.xs) > 2:
self.xs = np.delete(self.xs, ind)
self.ys = np.delete(self.ys, ind)
self.line.set_data(self.xs, self.ys)
self.axes.draw_artist(self.line)
self.canvas.draw_idle()
elif event.key == 'i':
p = np.array([event.x, event.y]) # display coords
xy = np.asarray(self.line._xy)
xyt = self.line.get_transform().transform(xy)
for i in range(len(xyt) - 1):
s0 = xyt[i]
s1 = xyt[i+1]
d = dist_point_to_segment(p, s0, s1)
if d <= self.epsilon:
self.xs = np.insert(self.xs, i+1, event.xdata)
self.ys = np.insert(self.ys, i+1, event.ydata)
self.line.set_data(self.xs, self.ys)
self.axes.draw_artist(self.line)
self.canvas.draw_idle()
break
if __name__ == '__main__':
fig, ax = plt.subplots()
line = Line2D([0,0.5,1], [0,0.5,1], marker = 'o', markerfacecolor = 'red')
ax.add_line(line)
linebuilder = LineBuilder(line)
ax.set_title('click to create lines')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
plt.show()
This actually leads to a question/problem i have but it will be in another message.
Kristen

Categories

Resources