gridlines at 0,0 only

===== Original message from John Hunter | Mon, 04 Jul 2005:

    >> One could add a kwarg to the various plot commands like
    >> noupdate to suppress updating the datalim,

    > example?

This was just a suggestion of an interface that could be implemented,
not one that exists. The interface would be

  # do not update the data limits which control autoscaling
  axvline(x=0, noupdate=True)

    >> Basically, you would like to add string symbol markers to
    >> scatter, and have them colored with colormaps and support
    >> variable sizeing as well, right?

    > yes. i think it would be a useful addition to pylab.

I agree -- could you add a feature request to the sourceforge site
with a description of the problem and a link to this thread?

    > making the plot window smaller, each marker are constant in
    > size, taking up a larger area when reducing the plot area.
    > (this is not a big issue though.)

I see. Yes there are times when you want the size of your markers to
be in data coordinates rather than in physical coordinates. This can
introduce other headaches, though.

    >> for gline in ax.get_xgridlines(): xmin, xmax =
    >> gline.get_xdata() # both should be the same
    >> gline.set_visible(xmin==0.0) # visible only at x==0

    > it didn't work at all. i started out with ax = gca(),
    > started with grids turned on and off.

There are two problems here. One is that you may need to control for
floating point inaccuracies and so want to test for "close to" zero.
This is trivial. The more subtle one has to do with how mpl manages
ticks and gridlines. These are created dynamically as needed, often
at draw time. So if interactive mode is off, the actual tick
locations may not be updated until draw time, which is why the call to

  gline.set_visible(closeto(ymin,0.0)) # visible only at y==0

isn't behaving properly. I consider this to be a bug, but I'll have
to think a little bit about the way to fix it. Feel free to file a
bug report on the sf site to help remind me :slight_smile: In the meantime, here
is a workaround which manipulates the interactive and draw state
directly, as described at
http://matplotlib.sourceforge.net/installing.html

from pylab import arange, ion, draw, rand, figure, show

# turn interactive mode on
ion()

fig = figure()
ax = fig.add_subplot(111)
ax.plot(rand(100)-0.5, rand(100)-0.5, 'go')
ax.set_xlim((-0.5, 0.5))
ax.set_xticks(arange(-0.5, 0.51, 0.1))

ax.grid(True)

def closeto(x, y, eps=1e-10):
    'return true if x close to y with some tolerance eps'
    return abs(x-y)<eps

# force a draw to create the ticks and gridlines
draw()

# now turn off all the grids except at zero
for gline in ax.get_xgridlines():
    xmin, xmax = gline.get_xdata() # both should be the same
    gline.set_visible(closeto(xmin,0.0)) # visible only at x==0

for gline in ax.get_ygridlines():
    ymin, ymax = gline.get_ydata() # both should be the same
    gline.set_visible(closeto(ymin,0.0)) # visible only at y==0

draw() # force a redraw
show()