Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: check if grid is on?

Matplotlib has a very easy method for toggling gridlines on a figure:

from matplotlib.figure import Figure
fig = Figure()
ax = fig.add_subplot(111)
ax.grid(True)

But it does not seem to have any method to determine the state of the grid (on/True or off/False)?

A look at the source code reveals that buried in the Axis class, there are the private variables: self._gridOnMinor and self._gridOnMajor

Accessing these could be done by:

ax.xaxis._gridOnMinor
ax.yaxis._gridOnMinor

and so on... but as these are designated as private, I'm a bit wary about doing so.

Is this really the only way to check whether the grid is on or off?

like image 417
jramm Avatar asked Apr 23 '14 13:04

jramm


People also ask

How do I turn on the grid in Matplotlib?

By default, Matplotlib does not display gridlines on plots. However, you can use the matplotlib. pyplot. grid() function to easily display and customize gridlines on a plot.

What does PLT grid () do in Python?

pyplot. grid() Function. The grid() function in pyplot module of matplotlib library is used to configure the grid lines.


1 Answers

I had the same problems and had to implement a special variable in my class to be aware of grid state. So I guess you either stay with your "dangerous private method" or with my "ugly method" :(

like image 176
arbulgazar Avatar answered Oct 14 '22 08:10

arbulgazar