Re: Plotting to a matplotlib / pyplot window does not work
Andrea Gavana <[email protected]>
| Newsgroups | gmane.comp.python.wxpython |
|---|---|
| Message-ID | <CAEf70bzaroZySutpiYcBCZ1mu_tjdvV-srnB9eVHNR=AsVvReg@mail.gmail.com> |
On Thu, 23 Jul 2020 at 11.50, Matthias Brennwald <[email protected]> wrote: > On Mo, Jul 20, 2020 at 23:02, Andrea Gavana <[email protected]> > wrote: > > A relatively straightforward solution is to embed your plot in a wxPython > window and update the plot from a thread when new data becomes available. > > > Ok, I revised my code accordingly (see attached example), and it works a > lot better. However, if the plot is updated every time after adding a new > data value, the GUI locks up if the rate of incoming data becomes too high. > > I worked around this by setting a flag that is TRUE while the plot is > being updated, and the code calls the updating of the plot only if this > flag is FALSE (this is also in the attached example). This works ok, but my > gut feeling is that there is a better / cleaner / more elegant way of > avoiding the GUI lockup. > > Thoughts, ideas, suggestions? > I also have an application that does heavy calculations in a separate thread and updates the main plotting window at selected timesteps. Depending on the model, I may get between 1 and 100 results per second and I have to update the plot with that. One solution that I have found is to copy the background (axes, labels, ticks and so on) and update the lines/points on the fly. something like this: import numpy import matplotlib.pyplot as plt x = numpy .arange(0,2*numpy.pi,0.01) line, = plt.plot(x, numpy.sin(x), animated=True) # save the clean slate background -- everything but the animated line # is drawn and saved in the pixel buffer background background = canvas.copy_from_bbox(ax.bbox) for i in range(100): # simulate stuff coming from a separate thread with a loop canvas.restore_region(background) # update the data line.set_ydata(numpy.sin(x+i/10.0)) # just draw the animated artist ax.draw_artist(line) # just redraw the axes rectangle canvas.blit(ax.bbox) I just save references to my line(s) as a class attribute and update those when new data comes in. I get decent FPS - depending on the machine and the model, I can keep up with 100 updates per second. Andrea. -- > You received this message because you are subscribed to the Google Groups > "wxPython-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/wxpython-users/LZ1XDQ.HRWQ1OF4INFR%40gmail.com > <https://groups.google.com/d/msgid/wxpython-users/LZ1XDQ.HRWQ1OF4INFR%40gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "wxPython-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/CAEf70bzaroZySutpiYcBCZ1mu_tjdvV-srnB9eVHNR%3DAsVvReg%40mail.gmail.com.