0

For a long time, I have been struggling with the live portability of Matplotlib. In between, I moved to PyQt and found several good solutions there. However, now I'd like to write some very simple simulations and would like to keep track of the process. The data that has to be tracked is a 2D point, so I would use Matplotlib's scatter method. The updating works quite nicely with fig.canvas.draw. However the more data points I have plotted the slower the plotting becomes.

def __init__(self):
    self.fig, (self.ax1, self.ax2) = plt.subplots(2)
    self.fig.show()
    self.fig.canvas.draw()
    self.main()

def main(self):
    c = 1
    for i in range(1000):
        self.ax1.scatter(i, c)
        self.ax2.scatter(c, i)
        c += 1
        self.fig.canvas.draw()
        self.fig.canvas.flush_events()

The relevant code lines are shown. It really revolves around the ax.scatter(). To my understanding, the underlying plot object becomes larger and larger as continuously add points to a point where the plotting takes more time than actual data simulation. Is there a way to only plot the new points? I've read of options for line plots where people use line.set_ydata and manual axis updates. Is there something similar to scatterplots or is there may be an even more elegant way?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Pa Po
  • 1
  • 2
    capture the artists returned by Axes.scatter and update their x- and y-data as you iterate – Paul H Jan 25 '18 at 17:55
  • Turn interactive mode on. This will only plot the latest data. I suspect that with interactive off, `draw` may be redrawing the entire plot from scratch. – Mad Physicist Jan 25 '18 at 19:42
  • Also, for your example, `c = i + 1`. If the real relationship is not that simple, it is still probably better to do `for i, c in zip(range(1000), range(1, 1001)):` or whatever. – Mad Physicist Jan 25 '18 at 19:44

0 Answers0