ENH: Add which parameter to Axis.get_gridlines#31674
Conversation
`Axis.get_gridlines` previously returned only major gridlines, with no public way to get the minor gridlines. Downstream libraries (seaborn, gwpy, mpld3, ...) had to reach into the private `_minor_tick_kw` mapping to inspect minor grid state. Extend the signature to `get_gridlines(which='major')`, accepting `'major'`, `'minor'`, or `'both'` (matching the vocabulary of `Axis.grid` and `get_ticklabels`). The default of `'major'` keeps the existing behavior byte-for-byte. Closes matplotlib#19021
55bc9a1 to
676d602
Compare
The Axes.get_xgridlines and Axes.get_ygridlines methods are auto-generated wrappers around Axis.get_gridlines (via _axis_method_wrapper), so they inherit the new which parameter at runtime. Update the .pyi stubs to match, fixing the mypy stubtest failure.
|
Please fill out the AI disclosure. |
This comment was marked as outdated.
This comment was marked as outdated.
|
@rcomer Sorry for disturbing but , are there any changes that i have to make with the PR or is the review still pending ? |
|
Thanks for the PR. The original request of #19021 to get the grid on status is already handed through I'm a bit torn on the addtion for getting minor grid lines. From a technical and consistency perspective this is correct. However, we discourage accessing individual ticks and their constituent Artists (ticklines, labels, grid lines). These should be considered ephemal and may be changed or recreated whenever the plot changes. |
|
Thanks @timhoffm — you're right on both counts. I went back and checked the current state of things:
I should have re-validated the private-API claim in my PR body before writing it — apologies for the stale framing. The remaining motivation is the styling case in the what's-new example. That's narrower than I implied to. It's the same pattern (#19021 (comment)) — but I take your point that ephemeral is a stronger principle than API symmetry. Happy to close this if you'd prefer not to extend the gridline-access surface, or scope it down if there's a middle ground you'd like to see. Your call. |
|
Thanks @rcomer , that settles it.Both the status-check and the styling use cases are already covered by existing public API, so this PR isn't adding anything.Closing it now.Appreciate both of you taking the time to walk through this. I am really sorry for wasting your time. |
|
Thanks for your understanding @SharadhNaidu. It is not your fault that the issue was out of date. |

PR summary
Closes #19021.
Axis.get_gridlines()currently returns only major gridlines, with no public way to obtain the minor gridlines. As called out in the issue thread, downstream libraries have been forced to reach into private API as a workaround:seabornreadsAxis._minor_tick_kw['gridOn'](source)gwpy,mpld3,mplexporterand others were referenced in the same threadThis PR implements the API @jklymak proposed in the issue:
The vocabulary
'major' | 'minor' | 'both'matchesAxis.grid()andAxis.get_ticklabels(). Default'major'preserves the previous behavior byte-for-byte, so every existing zero-argument caller continues to work unchanged.Files changed
lib/matplotlib/axis.pyget_gridlinesto acceptwhich='major'/'minor'/'both'lib/matplotlib/axis.pyilib/matplotlib/axes/_base.pyiAxes.get_x/ygridlinesstubs (auto-delegated wrappers)lib/matplotlib/tests/test_axes.pydoc/release/next_whats_new/get_gridlines_which.rstTesting
I monkey-patched the new implementation onto an installed
matplotlib==3.10.9and ran the same assertions the new pytest cases exercise:which='major',which='minor',which='both'returns the expected lengths and ordering'both'ordering ismajor + minor(matchingget_ticklabels)Line2Dobjects tracks the active grid state throughAxis.grid(visible=..., which=...)callsget_gridlines()andget_gridlines('major')are equivalent on bothxaxisandyaxiswhich='invalid'raisesValueErrorwith the standard_api.check_in_listmessageAxes.get_xgridlines(which=...)/Axes.get_ygridlines(which=...)auto-delegate via_axis_method_wrapperAll checks pass against the installed matplotlib with the new method patched in.
Notes for reviewers
get_majorgridlines/get_minorgridlinesmethods (briefly considered in the thread) I am happy to refactor.which='both', major gridlines come before minor gridlines, matchingget_ticklabels(which='both').Line2D.get_visible()to address @anntzer's concern about per-gridline visibility.AI Disclosure
AI was used to help proofread the grammar of this PR description and to understand some parts of the existing codebase during development. The code changes and design decisions were developed independently.
PR checklist
.. plot::directive showing minor-gridline coloring)versionchanged:: 3.11in the docstring +doc/release/next_whats_new/get_gridlines_which.rst)