问题
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?
回答1:
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" :(
来源:https://stackoverflow.com/questions/23246021/matplotlib-check-if-grid-is-on