updating image in wxpython gui...

Hi All:

I’m very new to matplotlib, but it seems like a very
powerful tool!

I’m trying to write a wxpython GUI that displays an
image from a data file that the user selected. I’ve looked at a bunch of
examples, but can’t seem to get it quite right. I’ve initialized
the image to a buffer of zeros, but can never get it to display the new data!

Any/all help & suggestions are greatly appreciated!!!

I wrote a “setupPlot” method that is called in
the init method of a wx.ScrolledWindow:

def SetupPlot(self):

self.figure =
Figure()

self.axes =
self.figure.add_subplot(111)

self.data
= np.zeros((64,8800),float)

self.myImage =
self.axes.imshow(self.data,aspect=100,animated=True)

self.canvas =
FigureCanvas(self, -1, self.figure)

self.sizer.Add(self.canvas,pos=(1,1))

self.add_toolbar()

Here is my “onPaint” event:

def OnPaint(self, event):

self.canvas.draw()

Finally, here is my “openFile” event:

def OnOpenFileEvent(self,event):

                    dlg = wx.FileDialog(self,"Open

Channel data file…",os.getcwd(), style=wx.OPEN,wildcard=self.wildcard)

                    if dlg.ShowModal() == wx.ID_OK:

                                self.filename =

dlg.GetPath()

                                datatype = np.complex64

                                shape = (64,8800)

                                fd = open(self.filename,

‘rb’)

                                read_data =

np.fromfile(file=fd, dtype=datatype).reshape(shape)

                                x =

abs(fftshift(read_data))

                                self.myImage.set_array(x)

                                self.axes.draw_artist(self.myImage)

                                self.canvas.blit()

THANXS

amb

You might try simplifying the problem to see if it is matplotlib, your
image data, your use of animation or wx. Ie, with *your* image data,
can you get the image to update in the pylab shell

  In [1]: ax = subplot(111)

  In [2]: data = np.random.rand(10,5)

  In [3]: im = ax.imshow(data)

  In [4]: ax.figure.canvas.draw()

  In [5]: data2 = np.random.rand(10,5)

  In [6]: im.set_array(data2)

  In [7]: ax.figure.canvas.draw()

If not, perhaps there is some massaging of the raw data that is needed.

If that works, try taking the animation calls out of the loop (don't
set the animated property, don't use draw_artist, don't use blit) but
just set the image data and call canvas.draw()

I did a quick read through of the code and did not find anything
glaringly wrong, so you need to start simplifying the problem and
reducing it to hone in on where the problem is occurring. If you post
a free standing script that we can run , with the data, in all
likelihood we can find the problem. You might even find it yourself
in the process of developing a minimal script <wink>

JDH

···

On Wed, Jan 7, 2009 at 7:57 AM, Lewis, Ambrose J. <AMBROSE.J.LEWIS@...2441...> wrote:

Hi All:

I'm very new to matplotlib, but it seems like a very powerful tool!

I'm trying to write a wxpython GUI that displays an image from a data file
that the user selected. I've looked at a bunch of examples, but can't seem
to get it quite right. I've initialized the image to a buffer of zeros, but
can never get it to display the new data!

Any/all help & suggestions are greatly appreciated!!!

Thanks for the quick reply John...I got it working.
To test the data file, I wrote a small script to display it outside of a
wxPython GUI. I slightly modified the "image_demo.py" example to read
complex values and displays them as an image:

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from scipy.fftpack import fftshift
datatype = np.complex64
shape = (64,8800)
fd = open('C:\\asp\\aspGui\\testData\\chan_output.bin', 'rb')
read_data = np.fromfile(file=fd, dtype=datatype).reshape(shape)
x = abs(fftshift(read_data))
im = plt.imshow(x,aspect='auto')
plt.show()

Next, as suggested, I wrote a small wxPython GUI that updates an image
based on user selections. In this example, the image is updated when
the user clicks on a button. The image will alternate between random
values and zeros:

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from scipy.fftpack import fftshift
from wx import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as
FigureCanvas
from numpy.random import randn

class PlotFigure(Frame):

    def __init__(self):
        Frame.__init__(self, None, -1, "Test embedded wxFigure")

  # make some random data...
  x = np.clip(randn(64,8800), -1, 1)

  # make the image...
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
  self.myImage = self.axes.imshow(x,aspect='auto')

  # add button to change the image...
  self.myButton = Button(self,-1,'change data')
  self.myButton.Bind(wx.EVT_BUTTON, self.OnButtonClick)

  # add the image to the display...
        sizer = BoxSizer(VERTICAL)
        sizer.Add(self.canvas, 1, LEFT|TOP|GROW)
  sizer.Add(self.myButton)
        self.SetSizer(sizer)
        self.Fit()
  self.myFlag = 0

    def OnButtonClick(self,event):
  if self.myFlag == 1:
    x = np.clip(randn(64,8800), -1, 1)
    self.myFlag = 0
  else:
    x = np.zeros((64,8800),float)
    self.myFlag = 1
  self.myImage.set_array(x)
  self.canvas.draw()

if __name__ == '__main__':
    app = PySimpleApp()
    frame = PlotFigure()

    frame.Show()
    app.MainLoop()

···

from my data file and convert the data. This example reads in a file of

-----Original Message-----
From: John Hunter [mailto:jdh2358@…287…]
Sent: Wednesday, January 07, 2009 9:23 AM
To: Lewis, Ambrose J.
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] updating image in wxpython gui…

On Wed, Jan 7, 2009 at 7:57 AM, Lewis, Ambrose J. <AMBROSE.J.LEWIS@...2441...> wrote:

Hi All:

I'm very new to matplotlib, but it seems like a very powerful tool!

I'm trying to write a wxpython GUI that displays an image from a data

file

that the user selected. I've looked at a bunch of examples, but can't

seem

to get it quite right. I've initialized the image to a buffer of

zeros, but

can never get it to display the new data!

Any/all help & suggestions are greatly appreciated!!!

You might try simplifying the problem to see if it is matplotlib, your
image data, your use of animation or wx. Ie, with *your* image data,
can you get the image to update in the pylab shell

  In [1]: ax = subplot(111)

  In [2]: data = np.random.rand(10,5)

  In [3]: im = ax.imshow(data)

  In [4]: ax.figure.canvas.draw()

  In [5]: data2 = np.random.rand(10,5)

  In [6]: im.set_array(data2)

  In [7]: ax.figure.canvas.draw()

If not, perhaps there is some massaging of the raw data that is needed.

If that works, try taking the animation calls out of the loop (don't
set the animated property, don't use draw_artist, don't use blit) but
just set the image data and call canvas.draw()

I did a quick read through of the code and did not find anything
glaringly wrong, so you need to start simplifying the problem and
reducing it to hone in on where the problem is occurring. If you post
a free standing script that we can run , with the data, in all
likelihood we can find the problem. You might even find it yourself
in the process of developing a minimal script <wink>

JDH