[Matplotlib-users] Making an interactive plot faster
Hi everyone, The following shows an example of a simple data viewer which includes a slider, a bitmap, and a scatter plot: """ import numpy as np from matplotlib import pyplot as plt from matplotlib.widgets import Slider fig = plt.figure() ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.7]) image = ax1.imshow(np.random.random((512, 512)), vmin=0., vmax=1.) ax2 = fig.add_axes([0.1, 0.9, 0.85, 0.05]) ax2.set_xticklabels("") ax2.set_yticklabels("") slider = Slider(ax2, "", 0., 1.) def update_vmax(value): image.set_clim(0., value) fig.canvas.draw() slider.on_changed(update_vmax) ax3 = fig.add_axes([0.55, 0.1, 0.4, 0.7]) x = np.random.random(1) y = np.random.random(1) ax3.scatter(x, y) plt.show() """ When moving the slider, the vmax of the image changes, but I get very slow frame rates (~2/sec) on my computer with the MacOS X backend. I was wondering whether people here have any tips on speeding things up? As far as I can figure out, this is slow because both the slider and the callback function call ``fig.canvas.draw``. Slider calls it before the callback function, so I definitely need to draw things in that function, but I can get a factor of 2x speedup by doing slider.drawon = False which prevents ``canvas.draw()`` being called twice. However, this is still much too slow, so I started to look into using ``draw_artist`` to only update elements that need updating, but this requires partially re-implementing the Slider class. As a side note, using ``draw_artist`` also does not work on the MacOS X backend: https://github.com/matplotlib/matplotlib/issues/166 but I'm willing to have a solution that wouldn't work on this backend if it was the only way. Does anyone have a clean solution to increasing the frame rate of this example? Thanks! Tom -- This SF.net email is sponsored by Windows: Build for Windows Store. http://p.sf.net/sfu/windows-dev2dev ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Making an interactive plot faster
Have you tried using "draw_idle"? That will schedule the draw for the next time the event loop is idle. Mike On 06/24/2013 07:39 AM, Thomas Robitaille wrote: > Hi everyone, > > The following shows an example of a simple data viewer which includes > a slider, a bitmap, and a scatter plot: > > """ > import numpy as np > from matplotlib import pyplot as plt > from matplotlib.widgets import Slider > > fig = plt.figure() > > ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.7]) > image = ax1.imshow(np.random.random((512, 512)), vmin=0., vmax=1.) > > ax2 = fig.add_axes([0.1, 0.9, 0.85, 0.05]) > ax2.set_xticklabels("") > ax2.set_yticklabels("") > slider = Slider(ax2, "", 0., 1.) > > def update_vmax(value): > image.set_clim(0., value) > fig.canvas.draw() > > slider.on_changed(update_vmax) > > ax3 = fig.add_axes([0.55, 0.1, 0.4, 0.7]) > x = np.random.random(1) > y = np.random.random(1) > ax3.scatter(x, y) > > plt.show() > """ > > When moving the slider, the vmax of the image changes, but I get very > slow frame rates (~2/sec) on my computer with the MacOS X backend. I > was wondering whether people here have any tips on speeding things up? > > As far as I can figure out, this is slow because both the slider and > the callback function call ``fig.canvas.draw``. Slider calls it before > the callback function, so I definitely need to draw things in that > function, but I can get a factor of 2x speedup by doing > > slider.drawon = False > > which prevents ``canvas.draw()`` being called twice. However, this is > still much too slow, so I started to look into using ``draw_artist`` > to only update elements that need updating, but this requires > partially re-implementing the Slider class. > > As a side note, using ``draw_artist`` also does not work on the MacOS X > backend: > > https://github.com/matplotlib/matplotlib/issues/166 > > but I'm willing to have a solution that wouldn't work on this backend > if it was the only way. > > Does anyone have a clean solution to increasing the frame rate of this > example? > > Thanks! > Tom > > -- > This SF.net email is sponsored by Windows: > > Build for Windows Store. > > http://p.sf.net/sfu/windows-dev2dev > ___ > Matplotlib-users mailing list > Matplotlib-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- This SF.net email is sponsored by Windows: Build for Windows Store. http://p.sf.net/sfu/windows-dev2dev ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Strange behavior with basemap.interp() and masked arrays
Goodman, Alexander (398J-Affiliate) June 24, 2013 4:30 PMJust something I thought I should add to this, but would the following code seem like a reasonable workaround? Basically, since map_coordinates() cannot handle mask arrays, I thought that perhaps passing in the actual mask itself to interp() with order=3 may produce close to reasonable results, eg: dataout = interp(datain, xin, yin, xout, yout, masked=True, order=3)maskin = datain.mask.astype(int)maskout = interp(datain.mask, xin, yin, xout, yout, order=3)dataout.mask = dataout.mask | maskout Thanks,Alex Alex: The basemap.interp docstring includes a note describing a trick I often use with masked arrays: Note If datain is a masked array and order=1 (bilinear interpolation) is used, elements of dataout will be masked if any of the four surrounding points in datain are masked. To avoid this, do the interpolation in two passes, first with order=1 (producing dataout1), then with order=0 (producing dataout2). Then replace all the masked values in dataout1 with the corresponding elements in dataout2 (using numpy.where). This effectively uses nearest neighbor interpolation if any of the four surrounding points in datain are masked, and bilinear interpolation otherwise. I suppose the same trick might work with order=3, but I have never tried it.-Jeff -- This SF.net email is sponsored by Windows: Build for Windows Store. http://p.sf.net/sfu/windows-dev2dev___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users