Matplotlib: check if grid is on?

百般思念 提交于 2019-12-08 21:43:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!