Skip to content

Commit 03fef5f

Browse files
authored
TST: Clean tests/plotting (#45992)
1 parent 79ffc96 commit 03fef5f

16 files changed

+448
-580
lines changed

pandas/plotting/_matplotlib/compat.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,10 @@ def inner():
1010
import matplotlib as mpl
1111
except ImportError:
1212
return False
13-
return (
14-
op(Version(mpl.__version__), Version(version))
15-
and str(mpl.__version__)[0] != "0"
16-
)
13+
return op(Version(mpl.__version__), Version(version))
1714

1815
return inner
1916

2017

21-
mpl_ge_2_2_3 = _mpl_version("2.2.3", operator.ge)
22-
mpl_ge_3_0_0 = _mpl_version("3.0.0", operator.ge)
23-
mpl_ge_3_1_0 = _mpl_version("3.1.0", operator.ge)
24-
mpl_ge_3_2_0 = _mpl_version("3.2.0", operator.ge)
25-
mpl_ge_3_3_0 = _mpl_version("3.3.0", operator.ge)
2618
mpl_ge_3_4_0 = _mpl_version("3.4.0", operator.ge)
2719
mpl_ge_3_5_0 = _mpl_version("3.5.0", operator.ge)

pandas/plotting/_matplotlib/core.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from pandas.core.frame import DataFrame
4343

4444
from pandas.io.formats.printing import pprint_thing
45-
from pandas.plotting._matplotlib.compat import mpl_ge_3_0_0
4645
from pandas.plotting._matplotlib.converter import register_pandas_matplotlib_converters
4746
from pandas.plotting._matplotlib.groupby import reconstruct_data_with_by
4847
from pandas.plotting._matplotlib.style import get_standard_colors
@@ -1032,29 +1031,7 @@ def _plot_colorbar(self, ax: Axes, **kwds):
10321031
# use the last one which contains the latest information
10331032
# about the ax
10341033
img = ax.collections[-1]
1035-
cbar = self.fig.colorbar(img, ax=ax, **kwds)
1036-
1037-
if mpl_ge_3_0_0():
1038-
# The workaround below is no longer necessary.
1039-
return cbar
1040-
1041-
points = ax.get_position().get_points()
1042-
cbar_points = cbar.ax.get_position().get_points()
1043-
1044-
cbar.ax.set_position(
1045-
[
1046-
cbar_points[0, 0],
1047-
points[0, 1],
1048-
cbar_points[1, 0] - cbar_points[0, 0],
1049-
points[1, 1] - points[0, 1],
1050-
]
1051-
)
1052-
# To see the discrepancy in axis heights uncomment
1053-
# the following two lines:
1054-
# print(points[1, 1] - points[0, 1])
1055-
# print(cbar_points[1, 1] - cbar_points[0, 1])
1056-
1057-
return cbar
1034+
return self.fig.colorbar(img, ax=ax, **kwds)
10581035

10591036

10601037
class ScatterPlot(PlanePlot):

pandas/plotting/_matplotlib/tools.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,8 @@ def handle_shared_axes(
388388
sharey: bool,
389389
):
390390
if nplots > 1:
391-
if compat.mpl_ge_3_2_0():
392-
row_num = lambda x: x.get_subplotspec().rowspan.start
393-
col_num = lambda x: x.get_subplotspec().colspan.start
394-
else:
395-
row_num = lambda x: x.rowNum
396-
col_num = lambda x: x.colNum
391+
row_num = lambda x: x.get_subplotspec().rowspan.start
392+
col_num = lambda x: x.get_subplotspec().colspan.start
397393

398394
if compat.mpl_ge_3_4_0():
399395
is_first_col = lambda x: x.get_subplotspec().is_first_col()

pandas/tests/plotting/common.py

+18-78
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from __future__ import annotations
99

10-
import os
1110
from typing import (
1211
TYPE_CHECKING,
1312
Sequence,
@@ -22,11 +21,7 @@
2221
from pandas.core.dtypes.api import is_list_like
2322

2423
import pandas as pd
25-
from pandas import (
26-
DataFrame,
27-
Series,
28-
to_datetime,
29-
)
24+
from pandas import Series
3025
import pandas._testing as tm
3126

3227
if TYPE_CHECKING:
@@ -39,62 +34,12 @@ class TestPlotBase:
3934
This is a common base class used for various plotting tests
4035
"""
4136

42-
def setup_method(self, method):
43-
37+
def setup_method(self):
4438
import matplotlib as mpl
4539

46-
from pandas.plotting._matplotlib import compat
47-
48-
self.compat = compat
49-
5040
mpl.rcdefaults()
5141

52-
self.start_date_to_int64 = 812419200000000000
53-
self.end_date_to_int64 = 819331200000000000
54-
55-
self.mpl_ge_2_2_3 = compat.mpl_ge_2_2_3()
56-
self.mpl_ge_3_0_0 = compat.mpl_ge_3_0_0()
57-
self.mpl_ge_3_1_0 = compat.mpl_ge_3_1_0()
58-
self.mpl_ge_3_2_0 = compat.mpl_ge_3_2_0()
59-
60-
self.bp_n_objects = 7
61-
self.polycollection_factor = 2
62-
self.default_figsize = (6.4, 4.8)
63-
self.default_tick_position = "left"
64-
65-
n = 100
66-
with tm.RNGContext(42):
67-
gender = np.random.choice(["Male", "Female"], size=n)
68-
classroom = np.random.choice(["A", "B", "C"], size=n)
69-
70-
self.hist_df = DataFrame(
71-
{
72-
"gender": gender,
73-
"classroom": classroom,
74-
"height": np.random.normal(66, 4, size=n),
75-
"weight": np.random.normal(161, 32, size=n),
76-
"category": np.random.randint(4, size=n),
77-
"datetime": to_datetime(
78-
np.random.randint(
79-
self.start_date_to_int64,
80-
self.end_date_to_int64,
81-
size=n,
82-
dtype=np.int64,
83-
)
84-
),
85-
}
86-
)
87-
88-
self.tdf = tm.makeTimeDataFrame()
89-
self.hexbin_df = DataFrame(
90-
{
91-
"A": np.random.uniform(size=20),
92-
"B": np.random.uniform(size=20),
93-
"C": np.arange(20) + np.random.uniform(size=20),
94-
}
95-
)
96-
97-
def teardown_method(self, method):
42+
def teardown_method(self):
9843
tm.close()
9944

10045
@cache_readonly
@@ -166,13 +111,12 @@ def _check_data(self, xp, rs):
166111
xp_lines = xp.get_lines()
167112
rs_lines = rs.get_lines()
168113

169-
def check_line(xpl, rsl):
114+
assert len(xp_lines) == len(rs_lines)
115+
for xpl, rsl in zip(xp_lines, rs_lines):
170116
xpdata = xpl.get_xydata()
171117
rsdata = rsl.get_xydata()
172118
tm.assert_almost_equal(xpdata, rsdata)
173119

174-
assert len(xp_lines) == len(rs_lines)
175-
[check_line(xpl, rsl) for xpl, rsl in zip(xp_lines, rs_lines)]
176120
tm.close()
177121

178122
def _check_visible(self, collections, visible=True):
@@ -387,7 +331,7 @@ def _check_axes_shape(self, axes, axes_num=None, layout=None, figsize=None):
387331
from pandas.plotting._matplotlib.tools import flatten_axes
388332

389333
if figsize is None:
390-
figsize = self.default_figsize
334+
figsize = (6.4, 4.8)
391335
visible_axes = self._flatten_visible(axes)
392336

393337
if axes_num is not None:
@@ -525,15 +469,8 @@ def _check_grid_settings(self, obj, kinds, kws={}):
525469
def is_grid_on():
526470
xticks = self.plt.gca().xaxis.get_major_ticks()
527471
yticks = self.plt.gca().yaxis.get_major_ticks()
528-
# for mpl 2.2.2, gridOn and gridline.get_visible disagree.
529-
# for new MPL, they are the same.
530-
531-
if self.mpl_ge_3_1_0:
532-
xoff = all(not g.gridline.get_visible() for g in xticks)
533-
yoff = all(not g.gridline.get_visible() for g in yticks)
534-
else:
535-
xoff = all(not g.gridOn for g in xticks)
536-
yoff = all(not g.gridOn for g in yticks)
472+
xoff = all(not g.gridline.get_visible() for g in xticks)
473+
yoff = all(not g.gridline.get_visible() for g in yticks)
537474

538475
return not (xoff and yoff)
539476

@@ -572,10 +509,18 @@ def _unpack_cycler(self, rcParams, field="color"):
572509
return [v[field] for v in rcParams["axes.prop_cycle"]]
573510

574511
def get_x_axis(self, ax):
575-
return ax._shared_axes["x"] if self.compat.mpl_ge_3_5_0() else ax._shared_x_axes
512+
from pandas.plotting._matplotlib.compat import mpl_ge_3_5_0
513+
514+
if mpl_ge_3_5_0():
515+
return ax._shared_axes["x"]
516+
return ax._shared_x_axes
576517

577518
def get_y_axis(self, ax):
578-
return ax._shared_axes["y"] if self.compat.mpl_ge_3_5_0() else ax._shared_y_axes
519+
from pandas.plotting._matplotlib.compat import mpl_ge_3_5_0
520+
521+
if mpl_ge_3_5_0():
522+
return ax._shared_axes["y"]
523+
return ax._shared_y_axes
579524

580525

581526
def _check_plot_works(f, filterwarnings="always", default_axes=False, **kwargs):
@@ -656,8 +601,3 @@ def _gen_two_subplots(f, fig, **kwargs):
656601
else:
657602
kwargs["ax"] = fig.add_subplot(212)
658603
yield f(**kwargs)
659-
660-
661-
def curpath():
662-
pth, _ = os.path.split(os.path.abspath(__file__))
663-
return pth

pandas/tests/plotting/conftest.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import (
5+
DataFrame,
6+
to_datetime,
7+
)
8+
import pandas._testing as tm
9+
10+
11+
@pytest.fixture
12+
def hist_df():
13+
n = 100
14+
with tm.RNGContext(42):
15+
gender = np.random.choice(["Male", "Female"], size=n)
16+
classroom = np.random.choice(["A", "B", "C"], size=n)
17+
18+
hist_df = DataFrame(
19+
{
20+
"gender": gender,
21+
"classroom": classroom,
22+
"height": np.random.normal(66, 4, size=n),
23+
"weight": np.random.normal(161, 32, size=n),
24+
"category": np.random.randint(4, size=n),
25+
"datetime": to_datetime(
26+
np.random.randint(
27+
812419200000000000,
28+
819331200000000000,
29+
size=n,
30+
dtype=np.int64,
31+
)
32+
),
33+
}
34+
)
35+
return hist_df

0 commit comments

Comments
 (0)