Pregunta

In my code, I have a draggablepoint class which defines a point that can be dragged interactively. So the interface basically selects one draggablepoint, clears the existing points and draws it on the figure. I expect the point to be shown on the figure right after I click the button, however, it takes time to show the point. Are there any faster ways to write the matplotlib part code? Thank you!

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.widgets import Button
from random import randint
import math
class Interface:

    def __init__(self, points):
        self.points = points
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111)
        self.run_button = Button(plt.axes([0.75, 0.6, 0.1, 0.04]), 'Draw', color='g', hovercolor='0.75')
        self.circles = []
        self.has_circle = False
        self.draggables = []

    def go(self):
        self.run_button.on_clicked(self.run)


    def run(self, event):
        '''remove point from the figure first'''
        if self.has_circle:
            self.circle.remove() 
        did = randint(0, len(self.points)-1)
        self.has_circle = True
        self.circle = patches.Circle(self.points[did], 0.02, fc='b', alpha=0.5, picker=True)
        self.ax.add_patch(self.circle)
        dr = DraggablePoint(self.circle)
        dr.connect()
        self.draggables.append(dr)

class DraggablePoint:
    lock = None #only one can be animated at a time
    def __init__(self, point):
        self.point = point
        self.press = None
        self.background = None
        self.ax = self.point.figure.add_subplot(111)

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.cidrelease = self.point.figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.cidmotion = self.point.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

    def on_press(self, event):
        if event.inaxes != self.point.axes: return
        if DraggablePoint.lock is not None: return
        contains = self.point.contains(event)[0]
        if not contains: return
        self.press = (self.point.center), event.xdata, event.ydata
        DraggablePoint.lock = self    
        # draw everything but the selected rectangle and store the pixel buffer
        canvas = self.point.figure.canvas
        axes = self.point.axes
        self.point.set_animated(True)
        canvas.draw()
        self.background = canvas.copy_from_bbox(self.point.axes.bbox)
        # now redraw just the rectangle
        axes.draw_artist(self.point)
        # and blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_motion(self, event):
        if DraggablePoint.lock is not self: return
        if event.inaxes != self.point.axes: return
        if event.xdata < event.ydata / math.sqrt(3): return
        if event.xdata > (event.ydata - math.sqrt(3))/(-math.sqrt(3)): return
        self.point.center, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.point.center = (self.point.center[0]+dx, self.point.center[1]+dy)
        canvas = self.point.figure.canvas
        axes = self.point.axes
        # restore the background region
        canvas.restore_region(self.background)
        # redraw just the current rectangle
        axes.draw_artist(self.point)
        # blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_release(self, event):
        'on release we reset the press data'
        if DraggablePoint.lock is not self:return
        self.press = None
        DraggablePoint.lock = None
        # turn off the rect animation property and reset the background
        self.point.set_animated(False)
        self.background = None
        # redraw the full figure
        self.point.figure.canvas.draw()

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.point.figure.canvas.mpl_disconnect(self.cidpress)
        self.point.figure.canvas.mpl_disconnect(self.cidrelease)
        self.point.figure.canvas.mpl_disconnect(self.cidmotion)

if __name__ == '__main__':
    points = [(0.5,0.4), (0.2, 0.3), (0.4, 0.8), (0.8, 0.9)]
    interface = Interface(points)
    interface.go()
    plt.show()
¿Fue útil?

Solución

add self.fig.canvas.draw() at the end of the run function.

The point is slow to appear because an update isn't forced before you leave the button.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top