Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow Line2D rendering in matplotlib

I am trying to use Line2D to render a line between the last point clicked on a polygon and the user's mouse. My current code currently works, but it lags FOREVER between renders, so that the line lags way behind the mouse. Current relevant code is:

#in constructor 
cid2 = self.ui.canvas2.mpl_connect('motion_notify_event', self.renderMeasureLine)

#This gets called and is laggy
def renderMeasureLine(self, mouseEvent):            
    if self.measuring and mouseEvent.inaxes == self.ui.canvas2.axes and len(self.pointListX) > 0:

        if self.dotted_line in self.ui.canvas2.axes.lines:
            self.ui.canvas2.axes.lines.remove(self.dotted_line)
        self.dotted_line = Line2D([self.pointListX[-1],mouseEvent.xdata],[self.pointListY[-1],mouseEvent.ydata],
                                   color=color, axes=self.ui.canvas2.axes, ls = '--')
        self.ui.canvas2.axes.add_line(self.dotted_line)
        self.ui.canvas2.draw()

================================================================================

#mpl canvas class
class MyDynamicMplCanvas(MyMplCanvas):
    """A canvas that updates itself every second with a new plot."""
    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        self.colorMap = cmap.bone

    def compute_initial_figure(self):
        self.axes.imshow(self.data, cmap = cmap.bone)

    def update_figure(self, dataOverride = None):
        if self.data is not None or dataOverride is not none:
            FigureCanvas.updateGeometry(self)
            self.axes.clear()
            if dataOverride is not None:
                self.axes.imshow(dataOverride, cmap = self.getColorMap())
            else:
                self.axes.imshow(self.data, cmap = self.getColorMap())
        self.draw()

So essentially, every time the mouse moves, I want to update a dotted line between the last polygon point clicked and the current new mouse position. This works, but it is SUPER laggy. Any ideas??

Edit: Picture of what's going on. The dotted line is what is slow.

enter image description here

like image 508
tylerthemiler Avatar asked Oct 17 '25 17:10

tylerthemiler


1 Answers

Do not create a new Line2D for every mouse move. A Line2D can have more than 2 points. So instead, use Line2D.set_data() to alter the data in a single Line2D.

Drawing one Line2D with thousands of points will surely be faster than instantiating and drawing thousands of Line2Ds.

def renderMeasureLine(self, mouseEvent):            
    if (self.measuring and mouseEvent.inaxes == self.ui.canvas2.axes
            and len(self.pointListX) > 0):
        if self.dotted_line in self.ui.canvas2.axes.lines:
            x, y = self.dotted_line.get_data()
            x.append(mouseEvent.xdata)
            y.append(mouseEvent.ydata)
            self.dotted_line.set_data(x, y)
        else:
            self.dotted_line = Line2D(
                [self.pointListX[-1],mouseEvent.xdata],
                [self.pointListY[-1],mouseEvent.ydata],
                color=color, axes=self.ui.canvas2.axes, ls = '--')
            self.ui.canvas2.axes.add_line(self.dotted_line)

        self.ui.canvas2.blit(self.ui.canvas2.axes.bbox)
        self.ui.canvas2.draw_idle()
like image 130
unutbu Avatar answered Oct 20 '25 06:10

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!