1

I am trying to plot intermediary plots (or subplots) on which we add new data in a loop (just dots, no legend or anything). Is there a way to reuse the previously saved plots and draw the new data over it so that the computation time of savefig doesn't grow at every iteration?

A solution that could work would be to save the inside of the window, and then load it and plot over it. However I have no idea if that's possible or how to implement it.

One simple example of a problematic code where the savefig call duration increases:

import time
import matplotlib.pyplot as plt
import numpy

fig = plt.figure()
for i in range(10):
    x = numpy.random.rand(100000)
    y = numpy.random.rand(100000)

    plt.plot(x, y, '.')

    t = time.time()
    fig.savefig('ZZZZ%d.jpg' % i)
    elapsed = time.time() - t
    print("%.2fs" % elapsed)
user3548298
  • 186
  • 1
  • 1
  • 13
  • `savefig` needs to draw the figure, this cannot be circumvented. But your problem here is that you create one `Line2D` object per loop run. Instead, update a single line with new points. – ImportanceOfBeingErnest Jul 07 '19 at 02:34
  • 2
    It looks like an answer to this question has a method to do this https://stackoverflow.com/questions/10944621/dynamically-updating-plot-in-matplotlib –  Jul 07 '19 at 02:37
  • I have no idea what you are talking about (regarding Line2D and single line with new points). Can you develop a bit? – user3548298 Jul 07 '19 at 02:38
  • 1
    Possible duplicate of [Dynamically updating plot in matplotlib](https://stackoverflow.com/questions/10944621/dynamically-updating-plot-in-matplotlib) –  Jul 07 '19 at 02:38
  • It seems like he had the same problem that I'm facing, however, the solution that he accepted as the best answer doesn't even solve that problem. The draw() and savefig() duration still grow linearly. – user3548298 Jul 07 '19 at 02:49
  • Right, I originally missed the fact that you are plotting `100000` points each time. So there isn't really any difference drawing 10 times 100000 points or 1 time 10*100000 points. Since the area in the picture where the new points appear is always the same, that area needs to be redrawn. And that is what takes the time. There is no way you can reuse the previously drawn image in such case. Maybe think about reducing the number of points to draw in the first place, if timing is an issue. – ImportanceOfBeingErnest Jul 07 '19 at 11:16
  • I NEED to plot those points. I found a solution which is using imshow() with a saved image file from the previous iteration. However, while I can successfully plot an image and align it correctly, I have trouble actually saving and loading the inner subwindow (the rectangle where we draw the dots) and adding new data to it. Basically atm I have an image that is saved every iteration containing only that window (with plt.axis('off')), but when I load it, it is out of alignment. Then I save that image and load it and draw it on the useful plot. I'm still trying to figure out what I did wrong. – user3548298 Jul 07 '19 at 12:45

0 Answers0