From 51c0403fea0dd41b42df20f903c0adad083c8ac5 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 7 Nov 2023 18:41:40 -0800 Subject: [PATCH 1/3] REF: make plotting less stateful --- pandas/plotting/_matplotlib/core.py | 30 ++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index b67a8186c8c2b..a4232da25bf37 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -126,8 +126,6 @@ def _kind(self) -> str: def orientation(self) -> str | None: return None - axes: np.ndarray # of Axes objects - def __init__( self, data, @@ -236,10 +234,11 @@ def __init__( self.mark_right = kwds.pop("mark_right", True) self.stacked = kwds.pop("stacked", False) + # ax may be an Axes object or (if self.subplots) an ndarray of + # Axes objects self.ax = ax # TODO: deprecate fig keyword as it is ignored, not passed in tests # as of 2023-11-05 - self.axes = np.array([], dtype=object) # "real" version get set in `generate` # parse errorbar input if given xerr = kwds.pop("xerr", None) @@ -463,7 +462,7 @@ def draw(self) -> None: @final def generate(self) -> None: self._compute_plot_data() - fig = self._setup_subplots() + fig = self.fig self._make_plot(fig) self._add_table() self._make_legend() @@ -509,7 +508,19 @@ def _maybe_right_yaxis(self, ax: Axes, axes_num: int): return new_ax @final - def _setup_subplots(self) -> Figure: + @cache_readonly + def fig(self) -> Figure: + return self._axes_and_fig[1] + + @final + @cache_readonly + # TODO: can we annotate this as both a Sequence[Axes] and ndarray[object]? + def axes(self) -> Sequence[Axes]: + return self._axes_and_fig[0] + + @final + @cache_readonly + def _axes_and_fig(self) -> tuple[Sequence[Axes], Figure]: if self.subplots: naxes = ( self.nseries if isinstance(self.subplots, bool) else len(self.subplots) @@ -552,8 +563,7 @@ def _setup_subplots(self) -> Figure: elif self.logy == "sym" or self.loglog == "sym": [a.set_yscale("symlog") for a in axes] - self.axes = axes - return fig + return axes, fig @property def result(self): @@ -562,7 +572,8 @@ def result(self): """ if self.subplots: if self.layout is not None and not is_list_like(self.ax): - return self.axes.reshape(*self.layout) + # error: "Sequence[Any]" has no attribute "reshape" + return self.axes.reshape(*self.layout) # type: ignore[attr-defined] else: return self.axes else: @@ -972,7 +983,8 @@ def _get_ax(self, i: int): i = self._col_idx_to_axis_idx(i) ax = self.axes[i] ax = self._maybe_right_yaxis(ax, i) - self.axes[i] = ax + # error: Unsupported target for indexed assignment ("Sequence[Any]") + self.axes[i] = ax # type: ignore[index] else: ax = self.axes[0] ax = self._maybe_right_yaxis(ax, i) From f449fb2320a462aca3125366c8af396da629f674 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 8 Nov 2023 15:50:19 -0800 Subject: [PATCH 2/3] mypy fixup --- pandas/plotting/_matplotlib/core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index a4232da25bf37..4f12a2def2931 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -13,11 +13,13 @@ TYPE_CHECKING, Any, Literal, + cast, final, ) import warnings import matplotlib as mpl +from matplotlib.axes import Axes import numpy as np from pandas.errors import AbstractMethodError @@ -81,7 +83,6 @@ if TYPE_CHECKING: from matplotlib.artist import Artist - from matplotlib.axes import Axes from matplotlib.axis import Axis from matplotlib.figure import Figure @@ -563,7 +564,8 @@ def _axes_and_fig(self) -> tuple[Sequence[Axes], Figure]: elif self.logy == "sym" or self.loglog == "sym": [a.set_yscale("symlog") for a in axes] - return axes, fig + axes_seq = cast(Sequence[Axes], axes) + return axes_seq, fig @property def result(self): From b0c1d0873ed89f72f5efbf40fdf97b653069430d Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 8 Nov 2023 19:17:19 -0800 Subject: [PATCH 3/3] Fix registry test --- pandas/plotting/_matplotlib/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 4f12a2def2931..848c6d45c494d 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -19,7 +19,6 @@ import warnings import matplotlib as mpl -from matplotlib.axes import Axes import numpy as np from pandas.errors import AbstractMethodError @@ -83,6 +82,7 @@ if TYPE_CHECKING: from matplotlib.artist import Artist + from matplotlib.axes import Axes from matplotlib.axis import Axis from matplotlib.figure import Figure @@ -564,7 +564,7 @@ def _axes_and_fig(self) -> tuple[Sequence[Axes], Figure]: elif self.logy == "sym" or self.loglog == "sym": [a.set_yscale("symlog") for a in axes] - axes_seq = cast(Sequence[Axes], axes) + axes_seq = cast(Sequence["Axes"], axes) return axes_seq, fig @property