Skip to content

Commit 6755b81

Browse files
authored
REF: make plotting less stateful (6) (#55886)
* REF: make plotting less stateful * mypy fixup * Fix registry test
1 parent 14cc1fe commit 6755b81

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

pandas/plotting/_matplotlib/core.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TYPE_CHECKING,
1414
Any,
1515
Literal,
16+
cast,
1617
final,
1718
)
1819
import warnings
@@ -126,8 +127,6 @@ def _kind(self) -> str:
126127
def orientation(self) -> str | None:
127128
return None
128129

129-
axes: np.ndarray # of Axes objects
130-
131130
def __init__(
132131
self,
133132
data,
@@ -236,10 +235,11 @@ def __init__(
236235
self.mark_right = kwds.pop("mark_right", True)
237236
self.stacked = kwds.pop("stacked", False)
238237

238+
# ax may be an Axes object or (if self.subplots) an ndarray of
239+
# Axes objects
239240
self.ax = ax
240241
# TODO: deprecate fig keyword as it is ignored, not passed in tests
241242
# as of 2023-11-05
242-
self.axes = np.array([], dtype=object) # "real" version get set in `generate`
243243

244244
# parse errorbar input if given
245245
xerr = kwds.pop("xerr", None)
@@ -463,7 +463,7 @@ def draw(self) -> None:
463463
@final
464464
def generate(self) -> None:
465465
self._compute_plot_data()
466-
fig = self._setup_subplots()
466+
fig = self.fig
467467
self._make_plot(fig)
468468
self._add_table()
469469
self._make_legend()
@@ -509,7 +509,19 @@ def _maybe_right_yaxis(self, ax: Axes, axes_num: int):
509509
return new_ax
510510

511511
@final
512-
def _setup_subplots(self) -> Figure:
512+
@cache_readonly
513+
def fig(self) -> Figure:
514+
return self._axes_and_fig[1]
515+
516+
@final
517+
@cache_readonly
518+
# TODO: can we annotate this as both a Sequence[Axes] and ndarray[object]?
519+
def axes(self) -> Sequence[Axes]:
520+
return self._axes_and_fig[0]
521+
522+
@final
523+
@cache_readonly
524+
def _axes_and_fig(self) -> tuple[Sequence[Axes], Figure]:
513525
if self.subplots:
514526
naxes = (
515527
self.nseries if isinstance(self.subplots, bool) else len(self.subplots)
@@ -552,8 +564,8 @@ def _setup_subplots(self) -> Figure:
552564
elif self.logy == "sym" or self.loglog == "sym":
553565
[a.set_yscale("symlog") for a in axes]
554566

555-
self.axes = axes
556-
return fig
567+
axes_seq = cast(Sequence["Axes"], axes)
568+
return axes_seq, fig
557569

558570
@property
559571
def result(self):
@@ -562,7 +574,8 @@ def result(self):
562574
"""
563575
if self.subplots:
564576
if self.layout is not None and not is_list_like(self.ax):
565-
return self.axes.reshape(*self.layout)
577+
# error: "Sequence[Any]" has no attribute "reshape"
578+
return self.axes.reshape(*self.layout) # type: ignore[attr-defined]
566579
else:
567580
return self.axes
568581
else:
@@ -972,7 +985,8 @@ def _get_ax(self, i: int):
972985
i = self._col_idx_to_axis_idx(i)
973986
ax = self.axes[i]
974987
ax = self._maybe_right_yaxis(ax, i)
975-
self.axes[i] = ax
988+
# error: Unsupported target for indexed assignment ("Sequence[Any]")
989+
self.axes[i] = ax # type: ignore[index]
976990
else:
977991
ax = self.axes[0]
978992
ax = self._maybe_right_yaxis(ax, i)

0 commit comments

Comments
 (0)