show figures in a loop

I have an application where I would like to use show in a loop but as stated
in 18.1 of the manual that does not work.
# WARNING : illustrating how NOT to use show
for i in range(10):
    # make figure i
    show()
So I made a workaround in a custom wxDialog with a wxStaticBitmap and a few
buttons and put the matplotlib figure in the bitmap with something like:
        wi,hi = figure.get_size_inches()
        width,height = bitmap_plot.GetSize()
        dpi = int(min(width/wi,height/hi))
        figure.savefig('/tmp/tmp.png',dpi = dpi)
        image = wx.Image('/tmp/tmp.png',wx.BITMAP_TYPE_ANY)
        bitmap = wx.BitmapFromImage(image)
        bitmap_plot.SetBitmap(bitmap)
Thus scaling the figure using the dpi option of savefig and then loading it
into the wxStaticBitmap.

To me it seems there might be a lot of unnecessary data handling. What is
the clever solution?
mant thanks

···

--
View this message in context: http://www.nabble.com/show-figures-in-a-loop-tp24776045p24776045.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hello Janwillem,

I have an application where I would like to use show in a loop but as stated
in 18.1 of the manual that does not work.
# WARNING : illustrating how NOT to use show
for i in range(10):
# make figure i
show()

call show() outside teh loop:

for i in <list>:
    <make the figure>
plt.show()

and that would show all the figure generated up to there.

So I made a workaround in a custom wxDialog with a wxStaticBitmap and a few

so, you want to embed in a WxWidgets application?

To me it seems there might be a lot of unnecessary data handling. What is
the clever solution?

Is the solution above fine for you?

Probably if you explain us better what you want to achieve, we can
help you in a better way.

Regards,

···

On Sun, Aug 2, 2009 at 14:11, Janwillem<jwevandijk@...1908...> wrote:
--
Sandro Tosi (aka morph, morpheus, matrixhasu)
My website: http://matrixhasu.altervista.org/
Me at Debian: http://wiki.debian.org/SandroTosi

The problem:
I have files with time versus signal data of a large series of measurements.
The python application (using wxPython actually) scans the file, applies
some math (numpy/scipy) on each record of data and than must show the signal
as a plot. After clicking OK the next record of measurement data is
processed and shown. So I do need some construct that allows showing a
series of figure one after the other. My solution works ok but I would think
that there should be an elegant shortcut that directly scales the
mathplotlib figure to a bitmap that can be shown.
Does that better explain the purpose of my question?
Cheers,Janwillem

Janwillem wrote:

···

I have an application where I would like to use show in a loop but as
stated in 18.1 of the manual that does not work.
# WARNING : illustrating how NOT to use show
for i in range(10):
    # make figure i
    show()
So I made a workaround in a custom wxDialog with a wxStaticBitmap and a
few buttons and put the matplotlib figure in the bitmap with something
like:
        wi,hi = figure.get_size_inches()
        width,height = bitmap_plot.GetSize()
        dpi = int(min(width/wi,height/hi))
        figure.savefig('/tmp/tmp.png',dpi = dpi)
        image = wx.Image('/tmp/tmp.png',wx.BITMAP_TYPE_ANY)
        bitmap = wx.BitmapFromImage(image)
        bitmap_plot.SetBitmap(bitmap)
Thus scaling the figure using the dpi option of savefig and then loading
it into the wxStaticBitmap.

To me it seems there might be a lot of unnecessary data handling. What is
the clever solution?
mant thanks

--
View this message in context: http://www.nabble.com/show-figures-in-a-loop-tp24776045p24777765.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

You are probably doing the best thing using wx for the event handling
in this app, but you do not need to save to png. You can get access
to the rendered figure in code as either a python buffer or string,
using one of several *Agg backend methods, eg buffer_rgba,
tostring_rgb, tostring_argb. See for example

  http://matplotlib.sourceforge.net/examples/pylab_examples/agg_buffer_to_array.html

JDH

···

On Sun, Aug 2, 2009 at 8:14 AM, Janwillem<jwevandijk@...1908...> wrote:

The problem:
I have files with time versus signal data of a large series of measurements.
The python application (using wxPython actually) scans the file, applies
some math (numpy/scipy) on each record of data and than must show the signal
as a plot. After clicking OK the next record of measurement data is
processed and shown. So I do need some construct that allows showing a
series of figure one after the other. My solution works ok but I would think
that there should be an elegant shortcut that directly scales the
mathplotlib figure to a bitmap that can be shown.
Does that better explain the purpose of my question?

Thanks for the hint, I now have:
        #figure is a matplotlib Figure
        #bitmap is a wx.StaticBitmap
        w_figure, h_figure = figure.get_size_inches()
        w_bitmap, h_bitmap = bitmap.GetSize()
        dpi = int(min(w_bitmap/w_figure,h_bitmap/h_figure))
        figure.set_dpi(dpi)
        figure.canvas.draw()
        w_canvas, h_canvas = figure.canvas.get_width_height()
        buffer = figure.canvas.tostring_rgb()
        bmp = wx.BitmapFromBuffer(w_canvas, h_canvas, buffer)
        bitmap.SetBitmap(bmp)
The size and dpi lines are meant to get a proper sizing of the canvas before
making a bitmap. This because I assume that the draw() converts vector
graphics to raster graphics. I am not sure all this is the cleverest way
but, when called from a wx.EVT_SIZE event it resizes nicely be it somewhat
slow.

Janwillem wrote:

···

I have an application where I would like to use show in a loop but as
stated in 18.1 of the manual that does not work.
# WARNING : illustrating how NOT to use show
for i in range(10):
    # make figure i
    show()
So I made a workaround in a custom wxDialog with a wxStaticBitmap and a
few buttons and put the matplotlib figure in the bitmap with something
like:
        wi,hi = figure.get_size_inches()
        width,height = bitmap_plot.GetSize()
        dpi = int(min(width/wi,height/hi))
        figure.savefig('/tmp/tmp.png',dpi = dpi)
        image = wx.Image('/tmp/tmp.png',wx.BITMAP_TYPE_ANY)
        bitmap = wx.BitmapFromImage(image)
        bitmap_plot.SetBitmap(bitmap)
Thus scaling the figure using the dpi option of savefig and then loading
it into the wxStaticBitmap.

To me it seems there might be a lot of unnecessary data handling. What is
the clever solution?
mant thanks

--
View this message in context: http://www.nabble.com/show-figures-in-a-loop-tp24776045p24779255.html
Sent from the matplotlib - users mailing list archive at Nabble.com.