13
13
TYPE_CHECKING ,
14
14
Any ,
15
15
Literal ,
16
+ cast ,
16
17
final ,
17
18
)
18
19
import warnings
@@ -126,8 +127,6 @@ def _kind(self) -> str:
126
127
def orientation (self ) -> str | None :
127
128
return None
128
129
129
- axes : np .ndarray # of Axes objects
130
-
131
130
def __init__ (
132
131
self ,
133
132
data ,
@@ -236,10 +235,11 @@ def __init__(
236
235
self .mark_right = kwds .pop ("mark_right" , True )
237
236
self .stacked = kwds .pop ("stacked" , False )
238
237
238
+ # ax may be an Axes object or (if self.subplots) an ndarray of
239
+ # Axes objects
239
240
self .ax = ax
240
241
# TODO: deprecate fig keyword as it is ignored, not passed in tests
241
242
# as of 2023-11-05
242
- self .axes = np .array ([], dtype = object ) # "real" version get set in `generate`
243
243
244
244
# parse errorbar input if given
245
245
xerr = kwds .pop ("xerr" , None )
@@ -463,7 +463,7 @@ def draw(self) -> None:
463
463
@final
464
464
def generate (self ) -> None :
465
465
self ._compute_plot_data ()
466
- fig = self ._setup_subplots ()
466
+ fig = self .fig
467
467
self ._make_plot (fig )
468
468
self ._add_table ()
469
469
self ._make_legend ()
@@ -509,7 +509,19 @@ def _maybe_right_yaxis(self, ax: Axes, axes_num: int):
509
509
return new_ax
510
510
511
511
@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 ]:
513
525
if self .subplots :
514
526
naxes = (
515
527
self .nseries if isinstance (self .subplots , bool ) else len (self .subplots )
@@ -552,8 +564,8 @@ def _setup_subplots(self) -> Figure:
552
564
elif self .logy == "sym" or self .loglog == "sym" :
553
565
[a .set_yscale ("symlog" ) for a in axes ]
554
566
555
- self . axes = axes
556
- return fig
567
+ axes_seq = cast ( Sequence [ "Axes" ], axes )
568
+ return axes_seq , fig
557
569
558
570
@property
559
571
def result (self ):
@@ -562,7 +574,8 @@ def result(self):
562
574
"""
563
575
if self .subplots :
564
576
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]
566
579
else :
567
580
return self .axes
568
581
else :
@@ -972,7 +985,8 @@ def _get_ax(self, i: int):
972
985
i = self ._col_idx_to_axis_idx (i )
973
986
ax = self .axes [i ]
974
987
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]
976
990
else :
977
991
ax = self .axes [0 ]
978
992
ax = self ._maybe_right_yaxis (ax , i )
0 commit comments