get_*gridlines alwys returns the same things?

This is weird:

When plotting something very simple, e.g.,

        t = arange( 0.0, 2.0, 0.01 )
        s = sin( 2*pi*t )
        plot( t, s, ":" )

I thought I can check weather the grid is on or off by

   gca().get_xgridlines()

-- but this *always* returns

<a list of 5 Line2D xgridline objects>

with *always* the same lines

Line2D((0,0),(0,1))
Line2D((0,0),(0,1))
Line2D((0,0),(0,1))
Line2D((0,0),(0,1))
Line2D((0,0),(0,1))

That's really independent of whether the grid is on or off.

Is there any explanation for it that does not have to do with Harry
Potter or the Jedi? :wink:

--Nico

I got curious and looked for the grid command in matplotlib/axes.py. Looks like an inherited-from-Matlab thing. In the cla (clear axis) function of the Axes class:

         self._gridOn = rcParams['axes.grid']
         #...
         self.grid(self._gridOn)

and grid() passes its argument on to the xaxis.grid and yaxis.grid.

I haven't found the code that checks any of those settings to decide whether the gridline objects are to be drawn or not (??) but I think we can rule out Harry Potter. Not magic: adaptation. (Or, if you will, not mystification: legacy code.)

&C

···

On May 4, 2010, at 3:27 PM, Nico Schlömer wrote:

This is weird:

When plotting something very simple, e.g.,

       t = arange( 0.0, 2.0, 0.01 )
       s = sin( 2*pi*t )
       plot( t, s, ":" )

I thought I can check weather the grid is on or off by

  gca().get_xgridlines()

-- but this *always* returns

<a list of 5 Line2D xgridline objects>

with *always* the same lines

Line2D((0,0),(0,1))

That's really independent of whether the grid is on or off.

Is there any explanation for it that does not have to do with Harry
Potter or the Jedi? :wink:

--Nico

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

positions of gridlines (and ticks, ticklabels, etc) are updated during
the drawing time (when the figure is drawn). Thus, the coordinates of
lines returned by gca().get_xgridlines() are sometimes meaningless and
you have to be careful about using this (it is best not to pay
attention). However, other artist-related attributes, e.g., line
colors etc, are respected.

Whether gridlines are on or off is actually controlled by each tick.
For example,

for t in gca().xaxis.majorTicks:
  print t.gridOn

A status of the gridlines is also kept in the axis-level.

print gca().xaxis._gridOnMajor

Regards,

-JJ

···

On Tue, May 4, 2010 at 6:27 PM, Nico Schlömer <nico.schloemer@...1896....> wrote:

That's really independent of whether the grid is on or off.

Is there any explanation for it that does not have to do with Harry
Potter or the Jedi? :wink:

I got curious and looked for the grid command in matplotlib/axes.py.
Looks like an inherited-from-Matlab thing. In the cla (clear axis)
function of the Axes class:

          self._gridOn = rcParams['axes.grid']
          #...
          self.grid(self._gridOn)

and grid() passes its argument on to the xaxis.grid and yaxis.grid.

I haven't found the code that checks any of those settings to decide
whether the gridline objects are to be drawn or not (??) but I think
we can rule out Harry Potter. Not magic: adaptation. (Or, if you will,
not mystification: legacy code.)

Exactly.

The decision on whether to draw the gridlines is made in the draw() method of each Tick object; even if the gridlines exist, they may not be drawn.
There is no API for retrieving the grid state, and the grid() API, both at the Axes and the Axis level, is a bit complicated. Although you can't retrieve the grid state (except by reading private attributes), you can set the grid to a known state, and you can toggle the state.

If there is a substantial need to read the grid state, we could expose it via a suitable API at the Axis level. But is this important?

Eric

···

On 05/05/2010 08:46 AM, Chloe Lewis wrote:

&C

On May 4, 2010, at 3:27 PM, Nico Schlömer wrote:

This is weird:

When plotting something very simple, e.g.,

        t = arange( 0.0, 2.0, 0.01 )
        s = sin( 2*pi*t )
        plot( t, s, ":" )

I thought I can check weather the grid is on or off by

   gca().get_xgridlines()

-- but this *always* returns

<a list of 5 Line2D xgridline objects>

with *always* the same lines

Line2D((0,0),(0,1))

That's really independent of whether the grid is on or off.

Is there any explanation for it that does not have to do with Harry
Potter or the Jedi? :wink:

--Nico

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

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

If there is a substantial need to read the grid state, we could expose
it via a suitable API at the Axis level. But is this important?

Well, I'm using this for the matplotlib2tikz converter
<http://github.com/nicki/matplotlib2tikz&gt; which takes a matplotlib
figure and spits out TikZ code. TikZ knows
"{x,y}{major,minor}grid={on,off}", so I'd like to read this from the
plot.

For now, I can live with

    gca().xaxis._gridOnMajor

as JJ proposed, but I guess I can't rely on this forever.

--Nico

···

On Wed, May 5, 2010 at 9:36 PM, Eric Firing <efiring@...202...> wrote:

On 05/05/2010 08:46 AM, Chloe Lewis wrote:

I got curious and looked for the grid command in matplotlib/axes.py.
Looks like an inherited-from-Matlab thing. In the cla (clear axis)
function of the Axes class:

      self\.\_gridOn = rcParams\[&#39;axes\.grid&#39;\]
      \#\.\.\.
      self\.grid\(self\.\_gridOn\)

and grid() passes its argument on to the xaxis.grid and yaxis.grid.

I haven't found the code that checks any of those settings to decide
whether the gridline objects are to be drawn or not (??) but I think
we can rule out Harry Potter. Not magic: adaptation. (Or, if you will,
not mystification: legacy code.)

Exactly.

The decision on whether to draw the gridlines is made in the draw()
method of each Tick object; even if the gridlines exist, they may not be
drawn.
There is no API for retrieving the grid state, and the grid() API, both
at the Axes and the Axis level, is a bit complicated. Although you
can't retrieve the grid state (except by reading private attributes),
you can set the grid to a known state, and you can toggle the state.

If there is a substantial need to read the grid state, we could expose
it via a suitable API at the Axis level. But is this important?

Eric

&C

On May 4, 2010, at 3:27 PM, Nico Schlömer wrote:

This is weird:

When plotting something very simple, e.g.,

    t = arange\( 0\.0, 2\.0, 0\.01 \)
    s = sin\( 2\*pi\*t \)
    plot\( t, s, &quot;:&quot; \)

I thought I can check weather the grid is on or off by

gca().get_xgridlines()

-- but this *always* returns

<a list of 5 Line2D xgridline objects>

with *always* the same lines

Line2D((0,0),(0,1))
Line2D((0,0),(0,1))
Line2D((0,0),(0,1))
Line2D((0,0),(0,1))
Line2D((0,0),(0,1))

That's really independent of whether the grid is on or off.

Is there any explanation for it that does not have to do with Harry
Potter or the Jedi? :wink:

--Nico

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

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

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