Discussion:
[Matplotlib-users] Autoscale AxesImage after using set_data()
Christoph Bersch
2011-02-08 09:05:30 UTC
Permalink
Hi,

I'm trying to autoscale an AxesImage after having set new data with
set_data(). I thought, the way to do it is to use Axes.relim() followed
by Axes.autoscale_view(). Unfortunately, this does not work properly
both with version 0.99.3 and 1.0.1.
Consider the following example (adapted from the example
<http://matplotlib.sourceforge.net/examples/animation/simple_anim_gtk.html>):

import time
import numpy as np
import matplotlib
matplotlib.use('GTKAgg')

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
x, y = np.mgrid[0:100, 0:100]
img = ax.imshow(np.sin(0.05 * y))

def animate():
time.sleep(3)
x, y = np.mgrid[0:200, 0:200]
img.set_data(np.sin(0.05 * x))
# set_*lim works with v 0.99.3
ax.set_xlim(0, 200)
ax.set_ylim(0, 200)
# ax.relim()
# ax.autoscale_view()
fig.canvas.draw()
return False

import gobject
gobject.idle_add(animate)
plt.show()


I want the plot to show the 200x200 image after the update, with the
correct ticks showing. But what I get is the following:

Using set_xlim() and set_ylim() works, but only for version 0.99.3.
With version 1.0.1 the axes show a range of 200x200, and the new image
data is used, but the new 200x200 image is shrunk to a 100x100 region.

Using relim() and autoscale_view(), which is what I thought the correct
way to do it, also does not work:
With version 0.99.3 it does nothing, it does no autoscaling at all. With
version 1.0.1 the new image is shown completely, but the axes ticks
still show 0..100 instead of 0..200

Thanks for you help,
Christoph
Christoph Bersch
2011-02-08 11:56:21 UTC
Permalink
On 08.02.2011 10:23, Thomas Lecocq wrote:
> I would suggest calling requet_redraw() ...

I could not find such a method, nor any similar one: request_redraw() or
*_redraw(). I grepped through the version 1.0.1 source code.

Christoph
Thomas Lecocq
2011-02-08 12:01:00 UTC
Permalink
Christoph,

this could work ;

img.set_extent([0,200,0,200])

HTH


Thom

**********************
Thomas Lecocq

Geologist
Ph.D.Student (Seismology)
Royal Observatory of Belgium
**********************



> Date: Tue, 8 Feb 2011 12:56:21 +0100
> From: ***@bersch.net
> To: ***@msn.com
> CC: matplotlib-***@lists.sourceforge.net
> Subject: Re: [Matplotlib-users] Autoscale AxesImage after using set_data()
>
> On 08.02.2011 10:23, Thomas Lecocq wrote:
> > I would suggest calling requet_redraw() ...
>
> I could not find such a method, nor any similar one: request_redraw() or
> *_redraw(). I grepped through the version 1.0.1 source code.
>
> Christoph
Christoph Bersch
2011-02-08 12:46:47 UTC
Permalink
On 08.02.2011 13:01, Thomas Lecocq wrote:
>
> this could work ;
>
> img.set_extent([0,200,0,200])

Yes, this works. Thank you.

So now I use
ax.relim()
ax.autoscale_view()
img.set_extent([0, 200, 0, 200])
Loading...