showing an image on log axes?

Hi John, The image is correct when plotted using

    > i=imread('plot.png') then imshow(i), but I want to add
    > axes. I generated the image directly using GTK commands,
    > then saved the pixbuf as png. The pixels in the image
    > correspond to sample points in both x- and y-directions
    > generated using exp(linspace(log(low),log(high),num). Why is
    > there no logspace in matplotlib, btw?

I'll be happy to add it -- how about sending a version?

    > All I basically need is a way to say what the range and
    > distribution of the pixels is: I don't want the axes to
    > default to integer-numbered linear-spaced values as they
    > currently do.

    > I tried to see if I could use the set_xscale command but it
    > seems to be internal and/or only applicable to polar plots?

setting the xscale and yscale to 'log' should work fine, as long as
you make sure the xaxis and yaxis do not contain nonpositive limits.
For an MxN image, the default limits are 0..N-1 and 0..M-1 and the 0
will break the log transform. You can work around this by setting the
image "extent"

  from pylab import figure, show, nx
  fig = figure()
  ax = fig.add_subplot(111)
  im = nx.mlab.rand(500,500)
  ax.imshow(im, extent=(1,501,1,501))
  ax.set_xscale('log')
  ax.set_yscale('log')
  show()

Hope this helps,
JDH

Hi John

Your suggestions worked perfectly. Thanks!

As an aside, I had been composing my image as a GTK Pixbuf object (via
the gtk.gdk.pixbuf_new_from_data method: hacking about, poking values
into a big array of char) and I did notice that the
gtk.gdk.Pixbuf.get_pixels_array method didn't give me a correct plot. So
im=gtk.Image() then im=set_from_pixbuf(p) and im.show() doesn't give the
same thing as im=p.get_pixels.array() and imshow(im). Evidently the GTK
pixels array isn't laid out the same way as the Matplotlib pixels array.
Is that right? In any case, the answer was to write out to a png file,
then read back in using 'imread'.

wrt 'logspace', I think that a logspace implementation should be pretty
simple (the challenge would be more to know that it's doing all the
right things with namespaces and exceptions and ufuncs etc).

def logspace(low,high,num):
    return pylab.exp(pylab.linspace(pylab.log(low),pylab.log(high),num))

Perhaps someone else could clarify whether or not this implementation is
100% compatible with the Matlab one.

Cheers
JP

John Hunter wrote:

···

            
    > Hi John, The image is correct when plotted using
    > i=imread('plot.png') then imshow(i), but I want to add
    > axes. I generated the image directly using GTK commands,
    > then saved the pixbuf as png. The pixels in the image
    > correspond to sample points in both x- and y-directions
    > generated using exp(linspace(log(low),log(high),num). Why is
    > there no logspace in matplotlib, btw?

I'll be happy to add it -- how about sending a version?

    > All I basically need is a way to say what the range and
    > distribution of the pixels is: I don't want the axes to
    > default to integer-numbered linear-spaced values as they
    > currently do.

    > I tried to see if I could use the set_xscale command but it
    > seems to be internal and/or only applicable to polar plots?

setting the xscale and yscale to 'log' should work fine, as long as
you make sure the xaxis and yaxis do not contain nonpositive limits.
For an MxN image, the default limits are 0..N-1 and 0..M-1 and the 0
will break the log transform. You can work around this by setting the
image "extent"

  from pylab import figure, show, nx
  fig = figure()
  ax = fig.add_subplot(111)
  im = nx.mlab.rand(500,500)
  ax.imshow(im, extent=(1,501,1,501))
  ax.set_xscale('log')
  ax.set_yscale('log')
  show()

Hope this helps,
JDH

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
John Pye
School of Mechanical and Manufacturing Engineering
The University of New South Wales
Sydney NSW 2052 Australia
t +61 2 9385 5127
f +61 2 9663 1222
mailto:john.pye_AT_student_DOT_unsw.edu.au

I often want to plot matrices, with the axes labeled according to the
matrix index. I.e. the top-lefthand element should be (0,0) and the
bottom-righthand element (rows,columns). Setting the extent does
work, i.e.

ax.imshow(im,extent=(1,columns,rows,1))

If others also use this frequently, it may be useful to have a quick
way of doing it (or maybe, there already is, and I've missed it).

Regards
Stéfan

···

On Tue, Jun 13, 2006 at 06:21:22AM -0500, John Hunter wrote:

setting the xscale and yscale to 'log' should work fine, as long as
you make sure the xaxis and yaxis do not contain nonpositive limits.
For an MxN image, the default limits are 0..N-1 and 0..M-1 and the 0
will break the log transform. You can work around this by setting the
image "extent"

  from pylab import figure, show, nx
  fig = figure()
  ax = fig.add_subplot(111)
  im = nx.mlab.rand(500,500)
  ax.imshow(im, extent=(1,501,1,501))

Hi,
Numpy has a logspace function, so it may be a bad idea to create a function in pylab with the same name that does something slightly different.

def logspace(start,stop,num=50,endpoint=True,base=10.0):
“”"Evenly spaced numbers on a logarithmic scale.

Computes int(num) evenly spaced exponents from start to stop.
If endpoint=True, then last exponent is stop.
Returns base**exponents.
"""
y = linspace(start,stop,num=num,endpoint=endpoint)

return _nx.power(base,y)

David

2006/6/15, Stefan van der Walt <stefan@…841…>:

···

On Tue, Jun 13, 2006 at 06:21:22AM -0500, John Hunter wrote:

setting the xscale and yscale to ‘log’ should work fine, as long as
you make sure the xaxis and yaxis do not contain nonpositive limits.
For an MxN image, the default limits are 0…N-1 and 0…M-1 and the 0

will break the log transform. You can work around this by setting the
image “extent”

from pylab import figure, show, nx
fig = figure()
ax = fig.add_subplot
(111)
im = nx.mlab.rand(500,500)
ax.imshow(im, extent=(1,501,1,501))

I often want to plot matrices, with the axes labeled according to the
matrix index. I.e. the top-lefthand element should be (0,0) and the

bottom-righthand element (rows,columns). Setting the extent does
work, i.e.

ax.imshow(im,extent=(1,columns,rows,1))

If others also use this frequently, it may be useful to have a quick
way of doing it (or maybe, there already is, and I’ve missed it).

Regards
Stéfan


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

See matshow(), by yours truly :slight_smile: One of my few direct contributions
to mpl. There may be a better way to do what it does with today's
mpl, but it works for me (all the figures from the SANUM talk were
done with it).

Cheers,

f

···

On 6/15/06, Stefan van der Walt <stefan@...841...> wrote:

I often want to plot matrices, with the axes labeled according to the
matrix index. I.e. the top-lefthand element should be (0,0) and the
bottom-righthand element (rows,columns). Setting the extent does
work, i.e.

ax.imshow(im,extent=(1,columns,rows,1))

If others also use this frequently, it may be useful to have a quick
way of doing it (or maybe, there already is, and I've missed it).

That's exactly what I need -- except that it forces the creation of a
new figure, which doesn't play well with subplot. Specifying a figure
number is, like it says in the docstring, rather unpredictable. Is
there an easy way to work around that limitation?

An example plot of what I would like to see is at

http://mentat.za.net/results/lp.jpg

Cheers
Stéfan

···

On Thu, Jun 15, 2006 at 11:02:47AM -0600, Fernando Perez wrote:

On 6/15/06, Stefan van der Walt <stefan@...841...> wrote:
> I often want to plot matrices, with the axes labeled according to the
> matrix index. I.e. the top-lefthand element should be (0,0) and the
> bottom-righthand element (rows,columns). Setting the extent does
> work, i.e.
>
> ax.imshow(im,extent=(1,columns,rows,1))
>
> If others also use this frequently, it may be useful to have a quick
> way of doing it (or maybe, there already is, and I've missed it).

See matshow(), by yours truly :slight_smile: One of my few direct contributions
to mpl. There may be a better way to do what it does with today's
mpl, but it works for me (all the figures from the SANUM talk were
done with it).

Well, the reason behind creating a new figure was to have tight
control of the aspect ratio: if you draw into an existing figure, you
have no idea what its aspect ratio will be. There might have been a
cleaner solution, but I just punted and went for the obvious: make a
new figure you /know/ will have the right aspect ratio always. Feel
free to submit an improved version :slight_smile:

Cheers,

f

···

On 6/15/06, Stefan van der Walt <stefan@...841...> wrote:

That's exactly what I need -- except that it forces the creation of a
new figure, which doesn't play well with subplot. Specifying a figure
number is, like it says in the docstring, rather unpredictable. Is
there an easy way to work around that limitation?

Fernando Perez wrote:

···

On 6/15/06, Stefan van der Walt <stefan@...841...> wrote:

That's exactly what I need -- except that it forces the creation of a
new figure, which doesn't play well with subplot. Specifying a figure
number is, like it says in the docstring, rather unpredictable. Is
there an easy way to work around that limitation?

Well, the reason behind creating a new figure was to have tight
control of the aspect ratio: if you draw into an existing figure, you
have no idea what its aspect ratio will be. There might have been a
cleaner solution, but I just punted and went for the obvious: make a
new figure you /know/ will have the right aspect ratio always. Feel
free to submit an improved version :slight_smile:

One can control the aspect ratio with Axes.set_aspect(), so making a new figure is not really necessary now.

Eric