Is there a matplotlib event that is similar the the Tkinter <B1-Motion> key? Such that a function is called only when the user is holding B1 down while moving the mouse?
I'm current looking into this documentation but don't see such an option, if not is there some simple way I can recreate this?
def notify_motion(event):
"""
Only called when button 1 is clicked and motion detected
"""
...
I have not idea whether you are still care about this issue, anyway there is a very simple solution using the button attribute of matplotlib.backend_bases.MouseEvent class, since event.button will be 1 if the cursor is being pressed and None if it is being released.
Following is my code to draw a line by holding and moving the cursor:
import matplotlib.pyplot as plt
import numpy as np
def moved_and_pressed(event):
if event.button==1:
x = np.append(line.get_xdata(), event.xdata)
y = np.append(line.get_ydata(), event.ydata)
line.set_data(x, y)
fig.canvas.draw()
fig, ax = plt.subplots(1,1, figsize=(5,3), dpi=100)
line, = ax.plot([], [], 'k')
ax.set_xlim(0,10); ax.set_ylim(0,10)
cid = fig.canvas.mpl_connect('motion_notify_event', moved_and_pressed)
plt.show()
Hope it helps.
canvas.mpl_connect doesn't provide a key_down+motion event. A possible hack is to store a property of whether the mouse button (or key) is hold down and use the motion_notify_event.
In this example, tho in wx, we can hold mouse down and draw a red rectangle over a plot:
import wx
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle
import matplotlib.cm as cm
import matplotlib.pyplot as plt
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent)
self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)
self.SetSizer(self.boxSizer1)
self.figure = Figure()
self.canvas = FigureCanvas(self, -1, self.figure)
self.boxSizer1.Add(self.canvas, 1, wx.EXPAND)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.axes = [self.figure.add_subplot(111), ]
self.axes[0].plot(range(100), range(100))
self.canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)
self.canvas.mpl_connect('button_press_event', self.on_press)
self.canvas.mpl_connect('button_release_event', self.on_release)
self.canvas.mpl_connect('motion_notify_event', self.on_motion)
self.rect = Rectangle((0,0), 1, 1, fill=False, ec='r')
self.axes[0].add_patch(self.rect)
#store a property of whether the mouse key is pressed
self.pressed = False
def ChangeCursor(self, event):
'''Change cursor into crosshair type when enter the plot area'''
self.canvas.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
def on_press(self, event):
'''Draw ROI rectangle'''
self.pressed = True
self.x0 = int(event.xdata)
self.y0 = int(event.ydata)
def on_release(self, event):
'''When mouse is on plot and button is released, redraw ROI rectangle, update ROI values'''
self.pressed = False
self.redraw_rect(event)
def redraw_rect(self, event):
'''Draw the ROI rectangle overlay'''
try:
self.x1 = int(event.xdata)
self.y1 = int(event.ydata)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
except:
pass
self.canvas.draw()
def on_motion(self, event):
'''If the mouse is on plot and if the mouse button is pressed, redraw ROI rectangle'''
if self.pressed:
# redraw the rect
self.redraw_rect(event)
class App(wx.App):
def OnInit(self):
frame = Frame(None)
self.SetTopWindow(frame)
frame.Show(True)
return True
if __name__=='__main__':
app=App(0)
app.MainLoop()
Related
I am currently working on a program that I am creating with Tkinter. Thereby large matrices (signals) are read in, which I represent as an image. In addition, I would like to display the signal at the point X (red vertical line) in the adjacent plot (interactive).
# Imports
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import numpy as np
from tkinter import *
# Global: Selected points with cursor
points = []
# Cursor
class Cursor:
def __init__(self, ax):
self.ax = ax
self.background = None
self.horizontal_line = ax.axhline(color='r', lw=0.8, ls='--')
self.vertical_line = ax.axvline(color='r', lw=0.8, ls='--')
self._creating_background = False
ax.figure.canvas.mpl_connect('draw_event', self.on_draw)
def on_draw(self, event):
self.create_new_background()
def set_cross_hair_visible(self, visible):
need_redraw = self.horizontal_line.get_visible() != visible
self.horizontal_line.set_visible(visible)
self.vertical_line.set_visible(visible)
return need_redraw
def create_new_background(self):
if self._creating_background:
return
self._creating_background = True
self.set_cross_hair_visible(False)
self.ax.figure.canvas.draw_idle()
self.background = self.ax.figure.canvas.copy_from_bbox(self.ax.bbox)
self.set_cross_hair_visible(True)
self._creating_background = False
def on_mouse_move(self, event, mode: str, matrix=None):
if self.background is None:
self.create_new_background()
if not event.inaxes:
need_redraw = self.set_cross_hair_visible(False)
if need_redraw:
self.ax.figure.canvas.restore_region(self.background)
self.ax.figure.canvas.blit(self.ax.bbox)
else:
self.set_cross_hair_visible(True)
x, y = event.xdata, event.ydata
if mode == "both":
self.horizontal_line.set_ydata(y)
self.vertical_line.set_xdata(x)
self.ax.figure.canvas.restore_region(self.background)
self.ax.draw_artist(self.horizontal_line)
self.ax.draw_artist(self.vertical_line)
elif mode == "horizontal":
self.ax.cla()
self.ax.plot(matrix[:, int(x)], range(0, matrix.shape[0], 1))
self.ax.figure.canvas.draw_idle()
self.horizontal_line.set_ydata(y)
self.ax.figure.canvas.restore_region(self.background)
self.ax.draw_artist(self.horizontal_line)
self.ax.figure.canvas.blit(self.ax.bbox)
# Graphical User Interface
class ToolGUI:
def __init__(self, master):
self.master = master
# Matrix (Example)
self.matrix = np.random.rand(3000, 5000)
# Subplots
self.fig = plt.figure(constrained_layout=True)
self.spec = self.fig.add_gridspec(5, 6)
self.ax_main = self.fig.add_subplot(self.spec[:, :-1])
self.ax_main.imshow(self.matrix, cmap='gray', aspect='auto')
self.ax_right = self.fig.add_subplot(self.spec[:, -1:], sharey=self.ax_main)
self.ax_right.get_yaxis().set_visible(False)
# Canvas - Drawing Area
self.canvas = FigureCanvasTkAgg(self.fig, master=master)
self.canvas.get_tk_widget().grid(column=0, row=0, sticky=NSEW)
# Cursor with crosshair
self.cursor_main = Cursor(self.ax_main)
self.fig.canvas.mpl_connect('motion_notify_event', lambda event: self.cursor_main.on_mouse_move(event, mode="both"))
self.cursor_right = Cursor(self.ax_right)
self.fig.canvas.mpl_connect('motion_notify_event', lambda event: self.cursor_right.on_mouse_move(event, mode="horizontal", matrix=self.matrix))
# Update Canvas
self.canvas.draw() # Update canvas
# Create root window
root = Tk()
# Root window title
root.title("Tool")
# Create GUI
my_gui = ToolGUI(root)
# Execute Tkinter
root.mainloop()
This example is only a small part of my program. In my full program, for example, certain points are picked out manually. With the help of the interactive plot a more exact selection of such points is possible.
Unfortunately, the program runs very slowly due to the use of this interactive plot. Since I haven't been working with Python for too long, I would appreciate any suggestions for improvement!
Thanks in advance! - Stefan
I have created a tkinter GUI that when a button is pushed a tk.toplevel is created with an interactive animated chart inside. When I run it for the first time and push the button to create the tk.toplevel with the chart, it works well, but when I close the tk.toplevel and push the button to open a new tk.toplevel, another extra chart appears. Apart from that, If I close the program and I want to run it again I have to close the terminal because it freezes.
I have tried removing plt.ion() and doing plt.show() but none of these options solve the problem. Also, I have seen the use of multiprocessing, but I don't really understand it well and I don't know why is it needed.
Why does it happen? How can I solve this problem?
Here is the code that I have used:
import tkinter as tk
import matplotlib
import matplotlib.figure as mf
import matplotlib.pyplot as plt
import matplotlib.animation as ani
from matplotlib.animation import FuncAnimation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
container = tk.Frame(self)
container.pack()
frame = MAININTERFACE(parent=container, controller=self)
frame.grid(row=0, column=0, sticky="nsew")
def graph(self):
grafica1=GRAPHICATION(controller=self)
grafica1.Graph()
class MAININTERFACE(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.button=tk.Button(self, text='GRAPH', command=lambda: self.controller.graph())
self.button.pack(pady=20)
class GRAPHICATION(tk.Frame):
def __init__(self,controller):
tk.Frame.__init__(self)
self.controller=controller
self.x=[]
self.y=[]
def animation_frame(self, i):
if i==0.0:
self.time=0
self.energy=0
else:
self.time=self.time+1
self.energy=self.energy+1
self.x.append(self.time)
self.y.append(self.energy)
self.line.set_data(self.x,self.y)
self.ax.axis([0,10,0,10])
def Graph(self):
self.graphtoplevel=tk.Toplevel(self.controller)
self.graphtoplevel.title('Toplevel')
self.fig, self.ax = plt.subplots()
self.graph=FigureCanvasTkAgg(self.fig, self.graphtoplevel)
self.image=self.graph.get_tk_widget()
plt.ion()
self.line, = self.ax.plot(self.x,self.y)
self.image.grid(row=0, column=0, sticky='nsew')
self.animation=FuncAnimation(self.fig,func=self.animation_frame,frames=np.arange(0,11,1),interval=500, repeat=False)
self.snap_cursor = SnaptoCursor(self.ax, self.x, self.y)
self.fig.canvas.mpl_connect('button_press_event', self.snap_cursor.mouse_click)
class SnaptoCursor(object):
"""
Like Cursor but the crosshair snaps to the nearest x, y point.
For simplicity, this assumes that *x* is sorted.
"""
def __init__(self, ax, x, y):
self.ax = ax
self.lx = ax.axhline(color='k', linewidth=0.5) # the horiz line
self.ly = ax.axvline(color='k', linewidth=0.5) # the vert line
self.x = x
self.y = y
# text location in axes coords
self.txt = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def mouse_click(self, event):
if not event.inaxes:
return
x, y = event.xdata, event.ydata
indx = min(np.searchsorted(self.x, x), len(self.x) - 1)
x = self.x[indx]
y = self.y[indx]
# update the line positions
self.lx.set_ydata(y)
self.ly.set_xdata(x)
self.txt.set_text('x=%1.2f, y=%1.2f' % (x, y))
self.ax.figure.canvas.draw()
if __name__ == "__main__":
app = SampleApp()
app.geometry('500x200')
app.title('MAIN GUI')
app.mainloop()
You need to make changes to your def Graph(self) function, as there are lines that are creating a window with tk.Toplevel and then the plt is creating another window.
Make your changes as follows, the capitalized comments you must do, and the others are optional for setting of titles:
def Graph(self):
#REMOVE THESE LINES
#self.graphtoplevel=tk.Toplevel(self.controller)
#self.graphtoplevel.title('Toplevel')
#modify the window title to your liking by assigning num
self.fig, self.ax = plt.subplots(num="TopLevel")
#REMOVE THESE LINES
#self.graph=FigureCanvasTkAgg(self.fig, self.graphtoplevel)
#self.image=self.graph.get_tk_widget()
plt.ion()
self.line, = self.ax.plot(self.x,self.y)
#REMOVE THIS LINE
#self.image.grid(row=0, column=0, sticky='nsew')
self.animation=FuncAnimation(self.fig,func=self.animation_frame,frames=np.arange(0,11,1),interval=500, repeat=False)
self.snap_cursor = SnaptoCursor(self.ax, self.x, self.y)
self.fig.canvas.mpl_connect('button_press_event', self.snap_cursor.mouse_click)
# displaying a title on top of figure (or do not add if not needed)
plt.title("Specific Fig Desc Title")
#ADD THIS LINE TO SHOW WINDOW
plt.show()
Please see this for more on figure window title:
Set matplotlib default figure window title
When you run the code the error should be pretty obvious, just click your mouse in the black space and move it around. I'm not sure how the line segments are being created, I pulled it from the library and its kind of confusing. How can I get the line segments to plot along my scatter plot? When I looks at the segments array being generated I believe it’s creating pairs of x,x and y,y instead of x,y and x,y
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap
import numpy as np
import random
class Mouse_Game:
'''Creates a matplotlib canvas object that plot mouse coordinates when animated.'''
def __init__(self, root, width=400, height=400):
self.frame = tk.Frame(master=root)
self.status = False
self.fig = plt.Figure(dpi=100, facecolor='black')
self.ax = self.fig.add_axes([0,0,1,1], fc='black')
self.width = width
self.height = height
self.ax.axis('off')
#set up the canvas object and bind commands
self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame, resize_callback=self.configure) # A tk.DrawingArea.
self.canvas.draw()
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=True)
self.fig.canvas.mpl_connect('button_press_event', self.animate)
self.fig.canvas.mpl_connect('motion_notify_event', self.motion)
def animate(self, event):
#Check if currently running
if not self.status:
self.status = True
self.capture = np.array([(event.xdata, event.ydata)]*40)
else:
self.status = False
#plot a line that follows the mouse around
while self.status:
self.ax.clear()
self.ax.set_xlim(0, self.width)
self.ax.set_ylim(0, self.height)
x = self.capture[:,0]
y = self.capture[:,1]
############################################################
points = self.capture.T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
###########################################################
lc = LineCollection(segments, cmap='magma')
lc.set_array(x)
lc.set_linewidth(2)
self.ax.add_collection(lc)
self.ax.scatter(x, y, marker='o')
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def motion(self, event):
if self.status:
#Append mouse coordinates to array when the mouse moves
self.capture = self.capture[1:40]
self.capture = np.append(self.capture, [(event.xdata, event.ydata)], axis=0)
def configure(self, event):
#Used to adjust coordinate setting when the screen size changes
self.width, self.height = self.canvas.get_width_height()
def _quit():
#destroy window
root.quit()
root.destroy()
root = tk.Tk()
root.wm_title("Mouse Plot")
MG = Mouse_Game(root=root)
MG.frame.pack(expand=True, fill='both')
button = tk.Button(root, text="Quit", command=_quit)
button.pack(side='bottom', pady=10)
tk.mainloop()
I'm not sure why you transpose the array. If you leave the transposition out, it'll work just fine
points = self.capture.reshape(-1, 1, 2)
I am trying to write my own crop function so I can select an area from a larger image and then perform some other operations on it. I use matplotlib to import and display images: self.img=plt.imread("someimage.png")
If I stay in the canvas area this works fine:
def __onclick__(self, event):
self.point=[]
if event.xdata is not None:
xholder=event.xdata
else:
xholder = 0
if event.ydata is not None:
yholder = event.ydata
else:
yholder = 0
self.point.append([int(xholder),int(yholder)])
def __onrelease__(self,event):
if event.xdata is not None:
xholder=event.xdata
else:
xholder = self.img.shape[0]
if event.ydata is not None:
yholder = event.ydata
else:
yholder = self.img.shape[1]
self.point.append([int(xholder),int(yholder)])
self.plotcanvas.axes.imshow(self.img[self.point[0][1]:self.point[1][1],self.point[0][0]:self.point[1][0]])
self.plotcanvas.draw()
However as soon as I go outside the canvas bounds event.xdata and event.ydata return (None,None). Is there anyway to constrict the mouse movements to only the area of the canvas once a mouse click is registered in the area of the canvas?
EDIT with code that allows to import a .png file and then crop it
import sys
from PyQt4 import QtGui, QtCore
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import numpy as np
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.statusBar()
self.form_widget=FormWidget(self)
self.setCentralWidget(self.form_widget)
openFile = QtGui.QAction('Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new File')
openFile.triggered.connect(self.showDialog)
toolbar = self.addToolBar('Exit')
toolbar.addAction(openFile)
self.show()
def showDialog(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home')
self.form_widget.redraw(fname)
class FormWidget(QtGui.QWidget):
def __init__(self,parent):
super(FormWidget,self).__init__()
self.layout=QtGui.QVBoxLayout(self)
self.mpcanvas=MyMplCanvas(self)
self.plotcanvas=MyMplCanvas(self)
self.layout.addWidget(self.plotcanvas)
self.layout.addWidget(self.mpcanvas)
self.setLayout(self.layout)
self.cidpresscsv=self.mpcanvas.fig.canvas.mpl_connect('button_press_event', self.__onclick__)
self.cidreleasecsv=self.mpcanvas.fig.canvas.mpl_connect('button_release_event', self.__onrelease__)
def redraw(self,fname):
self.mpcanvas.axes=self.mpcanvas.fig.add_subplot(111)
self.img=plt.imread(fname)
self.mpcanvas.axes.imshow(self.img)
self.mpcanvas.draw()
def __onclick__(self, event):
self.point=[]
if event.xdata is not None:
xholder=event.xdata
else:
xholder = 0
if event.ydata is not None:
yholder = event.ydata
else:
yholder = 0
self.point.append([int(xholder),int(yholder)])
def __onrelease__(self,event):
if event.xdata is not None:
xholder=event.xdata
else:
xholder = self.img.shape[0]
if event.ydata is not None:
yholder = event.ydata
else:
yholder = self.img.shape[1]
self.point.append([int(xholder),int(yholder)])
self.plotcanvas.axes.imshow(self.img[self.point[0][1]:self.point[1][1],self.point[0][0]:self.point[1][0]])
self.plotcanvas.draw()
class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
self.fig = plt.figure(figsize=(width, height), dpi=dpi)
self.axes = self.fig.add_subplot(111)
self.fig.tight_layout()
self.axes.hold(False)
self.compute_initial_figure()
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This is what came out after cutting out all the unnecessary stuff and still make a functional program that lets you import a .png file and then croup out an area out of it. I am sure that Rectangle selector proposed by Ed Smith might work, but I had trouble getting the example code to work and was confused about where the values were stored and how eclick and erelease worked in context with other event handlers. I'm afraid I am not that well versed in matplotlib and python in general.
The box selector matplotlib widget may be the easiest way to go. You can use the code with the image you have:
from matplotlib.widgets import RectangleSelector
from pylab import *
def onselect(eclick, erelease):
'eclick and erelease are matplotlib events at press and release'
print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
print ' endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)
print ' used button : ', eclick.button
def toggle_selector(event):
print ' Key pressed.'
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print ' RectangleSelector deactivated.'
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print ' RectangleSelector activated.'
toggle_selector.RS.set_active(True)
fig = figure
ax = subplot(111)
A = np.random.random((30,30))
ax.imshow(A)
toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='box')
connect('key_press_event', toggle_selector)
show()
Although the mouse isn't limited to the box, the default behaviour when minspanx and minspany are None is to limit the returned coordinates to the box. I don't think you could easily limit mouse movements to a canvas.
Inspired by this example I'm trying to write a little matplotlib program that allows the user to drag and drop datapoints in a scatter plot dynamically. In contrast to the example which uses a bar plot (and thus allows dragging of rectangles) my goal was to achieve the same with other patches, like for instance a circle (any patch that is more scatter-plot-compatible than a rectangle would do). However I'm stuck at the point of updating the position of my patch. While a Rectangle provides a function set_xy I cannot find a direct analog for Cirlce or Ellipse. Obtaining the position of a circle is also less straightforward that for a rectangle, but is possible via obtaining the bounding box. The missing piece now is to find a way to update the position of my patch. Any hint on how to achieve this would be great! The current minimal working example would look like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
class DraggablePatch:
def __init__(self, patch):
self.patch = patch
self.storedPosition = None
self.connect()
def getPosOfPatch(self, marker):
ext = marker.get_extents().get_points()
x0 = ext[0,0]
y0 = ext[0,1]
x1 = ext[1,0]
y1 = ext[1,1]
return 0.5*(x0+x1), 0.5*(y0+y1)
def connect(self):
'connect to all the events we need'
self.cidpress = self.patch.figure.canvas.mpl_connect('button_press_event', self.onPress)
self.cidmotion = self.patch.figure.canvas.mpl_connect('motion_notify_event', self.onMove)
def onPress(self, event):
'on button press we will see if the mouse is over us and store some data'
contains, attrd = self.patch.contains(event)
if contains:
self.storedPosition = self.getPosOfPatch(self.patch), event.xdata, event.ydata
def onMove(self, event):
'how to update an circle?!'
contains, attrd = self.patch.contains(event)
if contains and self.storedPosition is not None:
oldPos, oldEventXData, oldEventYData = self.storedPosition
dx = event.xdata - oldEventXData
dy = event.ydata - oldEventYData
newX = oldPos[0] + dx
newY = oldPos[1] + dy
print "now I would like to move my patch to", newX, newY
def myPatch(x,y):
return patches.Circle((x,y), radius=.05, alpha=0.5)
N = 10
x = np.random.random(N)
y = np.random.random(N)
patches = [myPatch(x[i], y[i]) for i in range(N)]
fig = plt.figure()
ax = fig.add_subplot(111)
drs = []
for patch in patches:
ax.add_patch(patch)
dr = DraggablePatch(patch)
drs.append(dr)
plt.show()
It's a bit annoying that it's inconsistent, but to update the position of a circle, set circ.center = new_x, new_y.
As a simple (non-draggable) example:
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
class InteractiveCircle(object):
def __init__(self):
self.fig, self.ax = plt.subplots()
self.ax.axis('equal')
self.circ = Circle((0.5, 0.5), 0.1)
self.ax.add_artist(self.circ)
self.ax.set_title('Click to move the circle')
self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def on_click(self, event):
if event.inaxes is None:
return
self.circ.center = event.xdata, event.ydata
self.fig.canvas.draw()
def show(self):
plt.show()
InteractiveCircle().show()