TYP: towards matplotlib 3.8
State: closed

Type changes for matplotlib 3.8. There is more work needed:
- failing CI with 3.8 https://github.com/pandas-dev/pandas/pull/55210#issuecomment-1732096783
- refactor the use of matplotlib to not set non-existing attributes matploblib object and to avoid using private methods (see mypy errors below). This can be done in a separate PR (or we add type ignores for now).

Typing errors left with 3.8:
```
pandas/plotting/_matplotlib/timeseries.py:127: error: "Axes" has no attribute "_plot_data"; maybe "plot_date"?  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:139: error: "Axes" has no attribute "_plot_data"; maybe "plot_date"?  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:156: error: "Axes" has no attribute "_plot_data"; maybe "plot_date"?  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:158: error: "Axes" has no attribute "freq"  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:160: error: "XAxis" has no attribute "freq"  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:162: error: "Axes" has no attribute "legendlabels"  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:165: error: "Axes" has no attribute "view_interval"  [attr-defined]
pandas/plotting/_matplotlib/timeseries.py:166: error: "Axes" has no attribute "date_axis_info"  [attr-defined]
pandas/plotting/_matplotlib/core.py:468: error: "Axes" has no attribute "containers"  [attr-defined]
pandas/plotting/_matplotlib/core.py:485: error: "_AxesBase" has no attribute "_get_lines"; maybe "get_lines"?  [attr-defined]
pandas/plotting/_matplotlib/core.py:485: error: "Axes" has no attribute "_get_lines"  [attr-defined]
pandas/plotting/_matplotlib/core.py:486: error: "_AxesBase" has no attribute "_get_patches_for_fill"  [attr-defined]
pandas/plotting/_matplotlib/core.py:486: error: "Axes" has no attribute "_get_patches_for_fill"  [attr-defined]
pandas/plotting/_matplotlib/core.py:487: error: "Axes" has no attribute "right_ax"  [attr-defined]
pandas/plotting/_matplotlib/core.py:487: error: "_AxesBase" has no attribute "left_ax"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1440: error: "Axes" has no attribute "_plot_data"; maybe "plot_date"?  [attr-defined]
pandas/plotting/_matplotlib/core.py:1444: error: "Axes" has no attribute "freq"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1458: error: "Axes" has no attribute "_stacker_pos_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1460: error: "Axes" has no attribute "_stacker_neg_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1461: error: "Axes" has no attribute "_stacker_pos_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1462: error: "Axes" has no attribute "_stacker_neg_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1473: error: "Axes" has no attribute "_stacker_pos_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1475: error: "Axes" has no attribute "_stacker_neg_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1488: error: "Axes" has no attribute "_stacker_pos_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1490: error: "Axes" has no attribute "_stacker_neg_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1590: error: "Axes" has no attribute "_stacker_pos_prior"  [attr-defined]
pandas/plotting/_matplotlib/core.py:1592: error: "Axes" has no attribute "_stacker_neg_prior"  [attr-defined]
```

This PR will obviously fail as it doesn't update matplotlib itself.

Labels:
name: Visualization
description: plotting
name: Typing
description: type annotations, mypy/pyright type checking

Comments:
> refactor the use of matplotlib to not set non-existing attributes matploblib objec

xref https://github.com/pandas-dev/pandas/issues/54485
I have also opened matplotlib/matplotlib#26883, FYI, which fixes up some of them that we could do better on (not _all_, particularly I may do a followup regarding the `None | Axis | _DummyAxis | _AxisWrapper` stuff, though I at least made `self.set_axis(self.axis)` typecheck as okay.


Not handling the `None` case is something we do ourselves, for the record.
For some reason this line

https://github.com/pandas-dev/pandas/blame/9e1096e8373bc99675fd1b3490cfb7cf26041395/doc/source/user_guide/visualization.rst#L747

breaks the doc-test with matplotlib 3.8

> Sphinx parallel build error:
> RuntimeError: Unexpected exception in `/home/runner/work/pandas/pandas/doc/source/user_guide/visualization.rst` line 747
> Error: Process completed with exit code 2.


edit:

Seems like pyright has a few additional errors they are mostly because pyright doesn't special case hasattr.
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
Thank you @mroeschke for rebasing! I'm very busy until at least 11/29 - if you or someone else wants to take over this PR, feel free to!

I think this PR might not need all changes since matplotlib 3.8.1 released (see failing mypy/pyright tests).
Thanks @twoertwein and @ksunden