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?
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.
pyplot. grid() Function. The grid() function in pyplot module of matplotlib library is used to configure the grid lines.
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" :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With