From: Alan G I. <ai...@am...> - 2004-09-07 01:01:59
|
I'm a newbie to Matplotlib but a longtime gnuplot user. Using the matlab module, what is the right way to set xzeroaxis (using gnuplot terminology). Using axis and hline seems cumbersome, so I assume there is a better way to implement this common need. Thank you, Alan Isaac |
From: Stephen W. <ste...@cs...> - 2004-09-07 02:26:15
|
On Mon, 2004-09-06 at 06:28, Alan G Isaac wrote: > I'm a newbie to Matplotlib but a longtime gnuplot user. > Using the matlab module, what is the right way to > set xzeroaxis MATLAB doesn't have anything similar, AFAIK, but the following will work: def xzeroaxis(form=3D'k'): # draw a line at y=3D0 on the current plot v=3Daxis() plot(v[0:2],[0,0],form) def yzeroaxis(form=3D'k'): v=3Daxis() plot([0,0],v[2:4],form) Is this worth adding to the distribution? I doubt it is general enough to warrant. John: when I do the following: v=3Darange(-5,5) plot(v,v**3) xzeroaxis() yzeroaxis() the vertical line at x=3D0 doesn't quite go to top and bottom of the plot. This is with 0.62.4. --=20 Stephen Walton <ste...@cs...> Dept. of Physics & Astronomy, CSU Northridge |
From: John H. <jdh...@ac...> - 2004-09-07 03:10:05
|
>>>>> "Stephen" == Stephen Walton <ste...@cs...> writes: Stephen> On Mon, 2004-09-06 at 06:28, Alan G Isaac wrote: >> I'm a newbie to Matplotlib but a longtime gnuplot user. Using >> the matlab module, what is the right way to set xzeroaxis Stephen> MATLAB doesn't have anything similar, AFAIK, but the Stephen> following will work: Stephen> def xzeroaxis(form='k'): # draw a line at y=0 on the Stephen> current plot v=axis() plot(v[0:2],[0,0],form) Stephen> def yzeroaxis(form='k'): v=axis() plot([0,0],v[2:4],form) Stephen> Is this worth adding to the distribution? I doubt it is Stephen> general enough to warrant. Stephen> John: when I do the following: v=arange(-5,5) Stephen> plot(v,v**3) xzeroaxis() yzeroaxis() Stephen> the vertical line at x=0 doesn't quite go to top and Stephen> bottom of the plot. This is with 0.62.4. Without testing, my guess is that the autoscale magic is getting in your way. Every plot command calls autoscale, which in the absence of bugs will include the data but may exceed it. Ie, if you plot data with a x range from 0.1 to 9.9, it may set the xlim from 0--10. Thus, although your idea of using the axis return values to get the min, max is nice, it fails when the autoscale chooses a wider range for nice ticking. The solution I posted a few minutes ago in which the span is plotted in *axes* coordinates rather than data coordinates circumvents this problem. Cheers! JDH |
From: John H. <jdh...@ac...> - 2004-09-07 02:47:48
|
>>>>> "Alan" == Alan G Isaac <ai...@am...> writes: Alan> I'm a newbie to Matplotlib but a longtime gnuplot user. Alan> Using the matlab module, what is the right way to set Alan> xzeroaxis (using gnuplot terminology). Using axis and hline Alan> seems cumbersome, so I assume there is a better way to Alan> implement this common need. Alan> Thank you, Alan Isaac I've not used set xzeroaxis. I googled around a bit and gathered that this draws a horizontal line from xmin to xmax at y=0. There is not equivalent currently in matplotlib so there is no clean and easy way to do this, but Fernando Perez and I talked a bit at scipy about adding some gnuplot compatibility functions and this would fit under that umbrella. I'm interested in getting some feedback about whether folks think this is a good idea. The worry is that we dump too much into the axes to support matlab, IDL and gnuplot users. Here's how you should do this under the current matplotlib. What you want to do is plot a line where the y coord is in data units and equal to zero, and the x coord is in axes units and spans the xrange. The line stretches from left to right regardless of the xlimits. Each axes provides a data transform and an axes transform, so that you can specify lines/text/etc in either coord system; axes coords are 0,0 for lower left and 1,1 for upper right. But what you want to do is mix the data and axes transforms for x and y. The transforms module provides a helper function for this. The following code is freestyle (untested) but should work from matplotlib.transforms import blend_xy_sep_transform ax = gca() trans = blend_xy_sep_transform( ax.transAxes, ax.transData) plot([0,1], [0,0], transform=trans) Try this out and see if it behaves as you like. I can add a methods for this for xzero and yzero if you and others feel this would be useful. Rather than reusing the gnuplot names, it might be better to provide a method that always draws a line from xmin to xmax at a given height regardless of the xlim, and do the same for a vertical line at some x. JDH |
From: Alan G I. <ai...@am...> - 2004-09-07 06:46:56
|
On Mon, 06 Sep 2004, John Hunter apparently wrote: > I've not used set xzeroaxis. I googled around a bit and gathered that > this draws a horizontal line from xmin to xmax at y=0. Right. > Fernando Perez and I talked a bit at scipy about > adding some gnuplot compatibility functions and this would fit under > that umbrella. I'm interested in getting some feedback about whether > folks think this is a good idea. I care about functionality much more than syntax. This is a common need, so it shd be easy to do. > from matplotlib.transforms import blend_xy_sep_transform > ax = gca() > trans = blend_xy_sep_transform( ax.transAxes, ax.transData) > plot([0,1], [0,0], transform=trans) > Try this out and see if it behaves as you like. So far so good. > Rather than reusing the gnuplot names, it might be better to > provide a method that always draws a line from xmin to xmax at a given > height regardless of the xlim, and do the same for a vertical line at > some x. I agree. I actually need the additional functionality. So while we're generalizing let me dream. Focus on the case of vertical lines. The ideal functionality would allow me to use 'vlines' with a special value to indicate ymin and ymax. Ideally this same 'special values' convention would extend to 'fill' (or maybe 'bar'), since I often need event shading. Perhaps the obvious special value is None? fwiw, Alan |
From: Gary <pa...@in...> - 2004-09-07 13:10:57
|
Stephen Walton wrote: >On Mon, 2004-09-06 at 06:28, Alan G Isaac wrote: > > >>I'm a newbie to Matplotlib but a longtime gnuplot user. >>Using the matlab module, what is the right way to >>set xzeroaxis >> >> > >MATLAB doesn't have anything similar, AFAIK, but the following will >work: > >def xzeroaxis(form='k'): # draw a line at y=0 on the current plot > v=axis() > plot(v[0:2],[0,0],form) > > [...] I've accomplished this feature via: ax = axis() set(gca(). hlines([0.], ax[0], ax[1])[0], linewidth=1) I don't remember why I did it this way instead of just plotting. Maybe I thought is was more clever. Does this method avoid auto-scaling? -g |
From: John H. <jdh...@ac...> - 2004-09-07 14:41:17
|
>>>>> "Gary" == Gary <pa...@in...> writes: Gary> I've accomplished this feature via: ax = axis() set(gca(). Gary> hlines([0.], ax[0], ax[1])[0], linewidth=1) Gary> I don't remember why I did it this way instead of just Gary> plotting. Maybe I thought is was more clever. Does this Gary> method avoid auto-scaling? -g Yes it does, only because hlines doesn't call autoscale (I just looked it up). This is probably a bug, since I don't see why the plot and hlines method should have different behavior. It looks like an oversight. I think plotting the horizontal extent in axes coords is the proper way to go. JDH |