Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Matplotlib and PySide

Matplotlib and PySide

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pytobias
    wrote on last edited by
    #1

    Hello,

    I'm trying to embed a matplotlib figure within QMainWindow using pyside. In the following example a rectangle is plotted and now I want to have it draggable by using mouse-commands. Why is the following code not working? If I use the DraggableRectangle-Class without Qt it works quite well.

    @import numpy as np
    import matplotlib.pyplot as plt
    import sys

    from PySide import QtCore, QtGui
    import matplotlib
    matplotlib.use('Qt4Agg')
    matplotlib.rcParams['backend.qt4']='PySide'
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

    class DraggableRectangle:
    def init(self, rect):
    self.rect = rect
    self.press = None

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.rect.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.rect.axes: return
    
        contains, attrd = self.rect.contains(event)
        if not contains: return
        print 'event contains', self.rect.xy
        x0, y0 = self.rect.xy
        self.press = x0, y0, event.xdata, event.ydata
    
    def on_motion(self, event):
        'on motion we will move the rect if the mouse is over us'
        if self.press is None: return
        if event.inaxes != self.rect.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        #print 'x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f'%(x0, xpress, event.xdata, dx, x0+dx)
        self.rect.set_x(x0+dx)
        self.rect.set_y(y0+dy)
    
        self.rect.figure.canvas.draw()
    
    
    def on_release(self, event):
        'on release we reset the press data'
        self.press = None
        self.rect.figure.canvas.draw()
    
    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
    

    class MainWindow(QtGui.QMainWindow):
    def init(self, parent=None):
    super(MainWindow, self).init(parent)

        self.setGeometry(100, 100, 1000, 600)
    
        self.fig = plt.Figure((16, 4))
        self.fig.subplots_adjust(0.01, 0.01, 0.99, 0.99)
        self.canvas = FigureCanvas(self.fig)
        self.subplot = self.fig.add_subplot(111)
        self.subplot.set_axis_off()
        self.press = None
    
        self.rect = plt.Rectangle((0.5,0.5), 0.3, 0.3, edgecolor='r', facecolor='green', picker=10)
        self.subplot.add_patch(self.rect)
    
        self.setCentralWidget(self.canvas)
    
        dr = DraggableRectangle(self.rect)
        dr.connect()
    

    def main():
    app = QtGui.QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()

    main()@

    Thank you for your help in advance!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved