updating colorbar to match new image data...

Hi All:

I’m using the following code to create and display an
image with a colorbar. Later on, I read data from a file and update the
image.

How do I refresh the colorbar to match this new data?
Uncommenting the line in my “ReadFromFile” call below generates a
new colorbar. I’d just like to replace the existing one to match
the new data.

I’m using Python 2.5.2, wx 2.8.7.1, and Matplotlib
0.98.5.2 on win32.

THANXS

amb

···

#################################################################################################

def SetupPlot(self):

    self.figure =

Figure()

    self.axes =

self.figure.add_subplot(111)

    self.canvas =

FigureCanvas(self, -1, self.figure)

    self.data =

np.zeros((self.numChans,self.pageSizeSamps))

    self.myImage =

self.axes.imshow(self.data,aspect=‘auto’)

    self.cbar =

self.figure.colorbar(self.myImage,ticks=[0,100],orientation=‘horizontal’)

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

    self.add_toolbar()

#################################################################################################

def ReadFromFile(self):

    if

self.filename=="":

print “pick a file first!”

    else:

bunch of code to read from file via numpy fromfile lives here…

self.image_data = abs(fftshift(data,axes=[0]))

self.myImage = self.axes.imshow(self.image_data,aspect=‘auto’,extent=[self.pageOffset,self.pageOffset+self.pageSizeSamps,0,self.numChans])

#self.cbar = self.figure.colorbar(self.myImage,orientation=‘horizontal’)

self.canvas.draw()

Lewis, Ambrose J. wrote:

Hi All:

I�m using the following code to create and display an image with a colorbar. Later on, I read data from a file and update the image.

How do I refresh the colorbar to match this new data?

For a colorbar cb I use:

cb.set_clim(vmin=min_value,vmax=max_value)
cb.draw_all()

With this method you have to calculate the minimum and maximum values first.

JLS

Lewis, Ambrose J. wrote:

Hi All:

I’m using the following code to create and display an image with a colorbar. Later on, I read data from a file and update the image.

How do I refresh the colorbar to match this new data? Uncommenting the line in my “ReadFromFile” call below generates a new colorbar. I’d just like to replace the existing one to match the new data.

I’m using Python 2.5.2, wx 2.8.7.1, and Matplotlib 0.98.5.2 on win32.

THANXS

amb

    #################################################################################################

    def SetupPlot(self):

        self.figure = Figure()

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

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

        self.data = np.zeros((self.numChans,self.pageSizeSamps))

        self.myImage = self.axes.imshow(self.data,aspect='auto')

        self.cbar = self.figure.colorbar(self.myImage,ticks=[0,100],orientation='horizontal')

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

        self.add_toolbar()

    #################################################################################################

    def ReadFromFile(self):

        if self.filename=="":

            print "pick a file first!"

        else:

            # bunch of code to read from file via numpy fromfile lives here…

            self.image_data = abs(fftshift(data,axes=[0]))

            self.myImage = self.axes.imshow(self.image_data,aspect='auto',extent=[self.pageOffset,self.pageOffset+self.pageSizeSamps,0,self.numChans])

            #self.cbar = self.figure.colorbar(self.myImage,orientation='horizontal')

            self.canvas.draw()

The problem is that you are generating a new mappable, so the old colorbar is disconnected from the new image. I haven't tested with an example, but it looks like you should be able to replace the calls in ReadFromFile to imshow with method calls on the original image object. Try using self.myImage.set_data, self.myImage.set_extent, and self.myImage.changed. The latter will tell the colorbar to update itself.

Eric

Thanks Eric...This approach also fixed my zooming issue in my other
post!!!
amb

···

-----Original Message-----
From: Eric Firing [mailto:efiring@…202…]
Sent: Monday, February 16, 2009 4:12 PM
To: Lewis, Ambrose J.
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] updating colorbar to match new image
data…

Lewis, Ambrose J. wrote:

Hi All:

I'm using the following code to create and display an image with a
colorbar. Later on, I read data from a file and update the image.

How do I refresh the colorbar to match this new data? Uncommenting

the

line in my "ReadFromFile" call below generates a new colorbar. I'd

just

like to replace the existing one to match the new data.

I'm using Python 2.5.2, wx 2.8.7.1, and Matplotlib 0.98.5.2 on win32.

THANXS

amb

########################################################################
#########################

    def SetupPlot(self):

        self.figure = Figure()

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

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

        self.data = np.zeros((self.numChans,self.pageSizeSamps))

        self.myImage = self.axes.imshow(self.data,aspect='auto')

        self.cbar =

self.figure.colorbar(self.myImage,ticks=[0,100],orientation='horizontal'
)

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

        self.add_toolbar()

########################################################################
#########################

    def ReadFromFile(self):

        if self.filename=="":

            print "pick a file first!"

        else:

            # bunch of code to read from file via numpy fromfile lives

here...

            self.image_data = abs(fftshift(data,axes=[0]))

            self.myImage =

self.axes.imshow(self.image_data,aspect='auto',extent=[self.pageOffset,s
elf.pageOffset+self.pageSizeSamps,0,self.numChans])

            #self.cbar =
self.figure.colorbar(self.myImage,orientation='horizontal')

            self.canvas.draw()

The problem is that you are generating a new mappable, so the old
colorbar is disconnected from the new image. I haven't tested with an
example, but it looks like you should be able to replace the calls in
ReadFromFile to imshow with method calls on the original image object.
Try using self.myImage.set_data, self.myImage.set_extent, and
self.myImage.changed. The latter will tell the colorbar to update
itself.

Eric