From: John P. <joh...@st...> - 2006-06-13 01:05:42
|
Hi all, I have a PNG image that I would like to mount on log-log axes. The points in the image correspond to computed values on a log-log scale, so no scaling of the image is required: I just want to stick it on top of suitably-marked axes. It would be great if I could then overlay some dot points as well. Is this possible with matplotlib? Can anyone give me some pointers on how to do it? Or a better tool for this? Cheers JP -- 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 http://pye.dyndns.org/ |
From: John H. <jdh...@ac...> - 2006-06-13 02:57:31
|
>>>>> "John" == John Pye <joh...@st...> writes: John> Hi all, I have a PNG image that I would like to mount on John> log-log axes. The points in the image correspond to computed John> values on a log-log scale, so no scaling of the image is John> required: I just want to stick it on top of suitably-marked John> axes. It would be great if I could then overlay some dot John> points as well. John> Is this possible with matplotlib? Can anyone give me some John> pointers on how to do it? Or a better tool for this? I'm not sure from your post if the log scale applies to the implicit xy coords of the pixels, or to the intensity of the pixels. I'm assuming the former below (if it's the latter you probably want custom normalize and colormap objects). logarithmic xy pixel locations may be possible with a NonuniformImage. Take a look at the following for example code http://article.gmane.org/gmane.comp.python.matplotlib.general/4050 I'm not sure that this will work since I haven't tried it, but it's the best bet as far as I can see. See how far you can get with it and if you get stuck, post a code example and CC Nicholas and we'll see if we can progress. JDH |
From: John P. <joh...@st...> - 2006-06-13 04:23:55
|
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? 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? There's an ASCII mockup of what I'm wanting below. As I said, the image doesn't need to be stretched, just stuck straight on the right axes. Cheers JP 10 +-----|-----|-----+ | | | | 1 + + + + | | | my image here | 0.1+ + + + | | | | e-3+-----+-----+-----+ 0.1 1 10 100 John Hunter wrote: >>>>>> "John" == John Pye <joh...@st...> writes: >>>>>> > > John> Hi all, I have a PNG image that I would like to mount on > John> log-log axes. The points in the image correspond to computed > John> values on a log-log scale, so no scaling of the image is > John> required: I just want to stick it on top of suitably-marked > John> axes. It would be great if I could then overlay some dot > John> points as well. > > John> Is this possible with matplotlib? Can anyone give me some > John> pointers on how to do it? Or a better tool for this? > > I'm not sure from your post if the log scale applies to the implicit > xy coords of the pixels, or to the intensity of the pixels. I'm > assuming the former below (if it's the latter you probably want custom > normalize and colormap objects). > > logarithmic xy pixel locations may be possible with a NonuniformImage. > Take a look at the following for example code > > http://article.gmane.org/gmane.comp.python.matplotlib.general/4050 > > I'm not sure that this will work since I haven't tried it, but it's > the best bet as far as I can see. > > See how far you can get with it and if you get stuck, post a code > example and CC Nicholas and we'll see if we can progress. > > JDH > > > -- 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 http://pye.dyndns.org/ |
From: John H. <jdh...@ac...> - 2006-06-13 11:29:01
|
>>>>> "John" == John Pye <joh...@st...> writes: John> Hi John, The image is correct when plotted using John> i=imread('plot.png') then imshow(i), but I want to add John> axes. I generated the image directly using GTK commands, John> then saved the pixbuf as png. The pixels in the image John> correspond to sample points in both x- and y-directions John> generated using exp(linspace(log(low),log(high),num). Why is John> there no logspace in matplotlib, btw? I'll be happy to add it -- how about sending a version? John> All I basically need is a way to say what the range and John> distribution of the pixels is: I don't want the axes to John> default to integer-numbered linear-spaced values as they John> currently do. John> I tried to see if I could use the set_xscale command but it John> 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 |
From: John P. <joh...@st...> - 2006-06-15 07:46:26
|
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: >>>>>> "John" == John Pye <joh...@st...> writes: >>>>>> > > John> Hi John, The image is correct when plotted using > John> i=imread('plot.png') then imshow(i), but I want to add > John> axes. I generated the image directly using GTK commands, > John> then saved the pixbuf as png. The pixels in the image > John> correspond to sample points in both x- and y-directions > John> generated using exp(linspace(log(low),log(high),num). Why is > John> there no logspace in matplotlib, btw? > > I'll be happy to add it -- how about sending a version? > > John> All I basically need is a way to say what the range and > John> distribution of the pixels is: I don't want the axes to > John> default to integer-numbered linear-spaced values as they > John> currently do. > > John> I tried to see if I could use the set_xscale command but it > John> 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 > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- 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 http://pye.dyndns.org/ |
From: Stefan v. d. W. <st...@su...> - 2006-06-15 09:26:38
|
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" >=20 > from pylab import figure, show, nx > fig =3D figure() > ax =3D fig.add_subplot(111) > im =3D nx.mlab.rand(500,500) > ax.imshow(im, extent=3D(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=3D(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=E9fan |
From: David H. <dav...@gm...> - 2006-06-16 04:03:54
|
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=3D50,endpoint=3DTrue,base=3D10.0): """Evenly spaced numbers on a logarithmic scale. Computes int(num) evenly spaced exponents from start to stop. If endpoint=3DTrue, then last exponent is stop. Returns base**exponents. """ y =3D linspace(start,stop,num=3Dnum,endpoint=3Dendpoint) return _nx.power(base,y) David 2006/6/15, Stefan van der Walt <st...@su...>: > > 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 =3D figure() > > ax =3D fig.add_subplot(111) > > im =3D nx.mlab.rand(500,500) > > ax.imshow(im, extent=3D(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=3D(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=E9fan > > > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
From: Fernando P. <fpe...@gm...> - 2006-06-15 17:02:55
|
On 6/15/06, Stefan van der Walt <st...@su...> 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 :) 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 |
From: Stefan v. d. W. <st...@su...> - 2006-06-15 22:20:24
|
On Thu, Jun 15, 2006 at 11:02:47AM -0600, Fernando Perez wrote: > On 6/15/06, Stefan van der Walt <st...@su...> 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=3D(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). >=20 > See matshow(), by yours truly :) 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). 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=E9fan |
From: Fernando P. <fpe...@gm...> - 2006-06-15 22:26:21
|
On 6/15/06, Stefan van der Walt <st...@su...> 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 :) Cheers, f |
From: Eric F. <ef...@ha...> - 2006-06-15 23:19:05
|
Fernando Perez wrote: > On 6/15/06, Stefan van der Walt <st...@su...> 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 :) One can control the aspect ratio with Axes.set_aspect(), so making a new figure is not really necessary now. Eric |
From: John H. <jdh...@ac...> - 2006-06-16 02:16:45
|
>>>>> "Eric" == Eric Firing <ef...@ha...> writes: Eric> One can control the aspect ratio with Axes.set_aspect(), so Eric> making a new figure is not really necessary now. Fernando, if you haven't played with the new aspect handling courtesy of Mark Bakkar and Eric, you should give it a whirl in your freetime :-) As Eric indicates, it "just works" now, under the presence of figure resizes, etc. Thus some changes in matshow are warranted. JDH |