Skip to content

Commit 2c23332

Browse files
authored
CLN: plotting (#56091)
* CLN: plotting * troubleshoot doctests
1 parent 639bd66 commit 2c23332

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

pandas/plotting/_matplotlib/boxplot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas.util._exceptions import find_stack_level
1616

1717
from pandas.core.dtypes.common import is_dict_like
18+
from pandas.core.dtypes.generic import ABCSeries
1819
from pandas.core.dtypes.missing import remove_na_arraylike
1920

2021
import pandas as pd
@@ -362,7 +363,7 @@ def boxplot(
362363
if return_type not in BoxPlot._valid_return_types:
363364
raise ValueError("return_type must be {'axes', 'dict', 'both'}")
364365

365-
if isinstance(data, pd.Series):
366+
if isinstance(data, ABCSeries):
366367
data = data.to_frame("x")
367368
column = "x"
368369

pandas/plotting/_matplotlib/core.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def generate(self) -> None:
504504
self._adorn_subplots(fig)
505505

506506
for ax in self.axes:
507-
self._post_plot_logic_common(ax, self.data)
507+
self._post_plot_logic_common(ax)
508508
self._post_plot_logic(ax, self.data)
509509

510510
@final
@@ -697,7 +697,7 @@ def _compute_plot_data(self):
697697
if is_empty:
698698
raise TypeError("no numeric data to plot")
699699

700-
self.data = numeric_data.apply(self._convert_to_ndarray)
700+
self.data = numeric_data.apply(type(self)._convert_to_ndarray)
701701

702702
def _make_plot(self, fig: Figure):
703703
raise AbstractMethodError(self)
@@ -714,21 +714,29 @@ def _add_table(self) -> None:
714714
tools.table(ax, data)
715715

716716
@final
717-
def _post_plot_logic_common(self, ax: Axes, data) -> None:
717+
def _post_plot_logic_common(self, ax: Axes) -> None:
718718
"""Common post process for each axes"""
719719
if self.orientation == "vertical" or self.orientation is None:
720-
self._apply_axis_properties(ax.xaxis, rot=self.rot, fontsize=self.fontsize)
721-
self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
720+
type(self)._apply_axis_properties(
721+
ax.xaxis, rot=self.rot, fontsize=self.fontsize
722+
)
723+
type(self)._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
722724

723725
if hasattr(ax, "right_ax"):
724-
self._apply_axis_properties(ax.right_ax.yaxis, fontsize=self.fontsize)
726+
type(self)._apply_axis_properties(
727+
ax.right_ax.yaxis, fontsize=self.fontsize
728+
)
725729

726730
elif self.orientation == "horizontal":
727-
self._apply_axis_properties(ax.yaxis, rot=self.rot, fontsize=self.fontsize)
728-
self._apply_axis_properties(ax.xaxis, fontsize=self.fontsize)
731+
type(self)._apply_axis_properties(
732+
ax.yaxis, rot=self.rot, fontsize=self.fontsize
733+
)
734+
type(self)._apply_axis_properties(ax.xaxis, fontsize=self.fontsize)
729735

730736
if hasattr(ax, "right_ax"):
731-
self._apply_axis_properties(ax.right_ax.yaxis, fontsize=self.fontsize)
737+
type(self)._apply_axis_properties(
738+
ax.right_ax.yaxis, fontsize=self.fontsize
739+
)
732740
else: # pragma no cover
733741
raise ValueError
734742

@@ -799,8 +807,9 @@ def _adorn_subplots(self, fig: Figure):
799807
self.axes[0].set_title(self.title)
800808

801809
@final
810+
@staticmethod
802811
def _apply_axis_properties(
803-
self, axis: Axis, rot=None, fontsize: int | None = None
812+
axis: Axis, rot=None, fontsize: int | None = None
804813
) -> None:
805814
"""
806815
Tick creation within matplotlib is reasonably expensive and is
@@ -1028,16 +1037,6 @@ def _get_ax(self, i: int):
10281037
ax.get_yaxis().set_visible(True)
10291038
return ax
10301039

1031-
@final
1032-
@classmethod
1033-
def get_default_ax(cls, ax) -> None:
1034-
import matplotlib.pyplot as plt
1035-
1036-
if ax is None and len(plt.get_fignums()) > 0:
1037-
with plt.rc_context():
1038-
ax = plt.gca()
1039-
ax = cls._get_ax_layer(ax)
1040-
10411040
@final
10421041
def on_right(self, i: int):
10431042
if isinstance(self.secondary_y, bool):
@@ -1192,7 +1191,7 @@ def match_labels(data, e):
11921191
@final
11931192
def _get_errorbars(
11941193
self, label=None, index=None, xerr: bool = True, yerr: bool = True
1195-
):
1194+
) -> dict[str, Any]:
11961195
errors = {}
11971196

11981197
for kw, flag in zip(["xerr", "yerr"], [xerr, yerr]):

pandas/plotting/_matplotlib/groupby.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
from pandas.core.dtypes.missing import remove_na_arraylike
88

99
from pandas import (
10-
DataFrame,
1110
MultiIndex,
12-
Series,
1311
concat,
1412
)
1513

@@ -20,6 +18,11 @@
2018

2119
from pandas._typing import IndexLabel
2220

21+
from pandas import (
22+
DataFrame,
23+
Series,
24+
)
25+
2326

2427
def create_iter_data_given_by(
2528
data: DataFrame, kind: str = "hist"
@@ -48,10 +51,10 @@ def create_iter_data_given_by(
4851
4952
>>> import numpy as np
5053
>>> tuples = [('h1', 'a'), ('h1', 'b'), ('h2', 'a'), ('h2', 'b')]
51-
>>> mi = MultiIndex.from_tuples(tuples)
54+
>>> mi = pd.MultiIndex.from_tuples(tuples)
5255
>>> value = [[1, 3, np.nan, np.nan],
5356
... [3, 4, np.nan, np.nan], [np.nan, np.nan, 5, 6]]
54-
>>> data = DataFrame(value, columns=mi)
57+
>>> data = pd.DataFrame(value, columns=mi)
5558
>>> create_iter_data_given_by(data)
5659
{'h1': h1
5760
a b
@@ -104,7 +107,7 @@ def reconstruct_data_with_by(
104107
Examples
105108
--------
106109
>>> d = {'h': ['h1', 'h1', 'h2'], 'a': [1, 3, 5], 'b': [3, 4, 6]}
107-
>>> df = DataFrame(d)
110+
>>> df = pd.DataFrame(d)
108111
>>> reconstruct_data_with_by(df, by='h', cols=['a', 'b'])
109112
h1 h2
110113
a b a b

pandas/plotting/_matplotlib/hist.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _make_plot(self, fig: Figure) -> None:
161161
kwds.pop("color")
162162

163163
if self.weights is not None:
164-
kwds["weights"] = self._get_column_weights(self.weights, i, y)
164+
kwds["weights"] = type(self)._get_column_weights(self.weights, i, y)
165165

166166
y = reformat_hist_y_given_by(y, self.by)
167167

@@ -284,15 +284,15 @@ def _plot( # type: ignore[override]
284284

285285
def _make_plot_keywords(self, kwds: dict[str, Any], y: np.ndarray) -> None:
286286
kwds["bw_method"] = self.bw_method
287-
kwds["ind"] = self._get_ind(y, ind=self.ind)
287+
kwds["ind"] = type(self)._get_ind(y, ind=self.ind)
288288

289289
def _post_plot_logic(self, ax: Axes, data) -> None:
290290
ax.set_ylabel("Density")
291291

292292

293293
def _grouped_plot(
294294
plotf,
295-
data,
295+
data: Series | DataFrame,
296296
column=None,
297297
by=None,
298298
numeric_only: bool = True,
@@ -335,7 +335,7 @@ def _grouped_plot(
335335

336336

337337
def _grouped_hist(
338-
data,
338+
data: Series | DataFrame,
339339
column=None,
340340
by=None,
341341
ax=None,
@@ -417,7 +417,7 @@ def plot_group(group, ax) -> None:
417417

418418

419419
def hist_series(
420-
self,
420+
self: Series,
421421
by=None,
422422
ax=None,
423423
grid: bool = True,
@@ -495,7 +495,7 @@ def hist_series(
495495

496496

497497
def hist_frame(
498-
data,
498+
data: DataFrame,
499499
column=None,
500500
by=None,
501501
grid: bool = True,

0 commit comments

Comments
 (0)