Skip to content

TST: Clean tests/plotting #45992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions pandas/plotting/_matplotlib/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ def inner():
import matplotlib as mpl
except ImportError:
return False
return (
op(Version(mpl.__version__), Version(version))
and str(mpl.__version__)[0] != "0"
)
return op(Version(mpl.__version__), Version(version))

return inner


mpl_ge_2_2_3 = _mpl_version("2.2.3", operator.ge)
mpl_ge_3_0_0 = _mpl_version("3.0.0", operator.ge)
mpl_ge_3_1_0 = _mpl_version("3.1.0", operator.ge)
mpl_ge_3_2_0 = _mpl_version("3.2.0", operator.ge)
mpl_ge_3_3_0 = _mpl_version("3.3.0", operator.ge)
mpl_ge_3_4_0 = _mpl_version("3.4.0", operator.ge)
mpl_ge_3_5_0 = _mpl_version("3.5.0", operator.ge)
25 changes: 1 addition & 24 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from pandas.core.frame import DataFrame

from pandas.io.formats.printing import pprint_thing
from pandas.plotting._matplotlib.compat import mpl_ge_3_0_0
from pandas.plotting._matplotlib.converter import register_pandas_matplotlib_converters
from pandas.plotting._matplotlib.groupby import reconstruct_data_with_by
from pandas.plotting._matplotlib.style import get_standard_colors
Expand Down Expand Up @@ -1032,29 +1031,7 @@ def _plot_colorbar(self, ax: Axes, **kwds):
# use the last one which contains the latest information
# about the ax
img = ax.collections[-1]
cbar = self.fig.colorbar(img, ax=ax, **kwds)

if mpl_ge_3_0_0():
# The workaround below is no longer necessary.
return cbar

points = ax.get_position().get_points()
cbar_points = cbar.ax.get_position().get_points()

cbar.ax.set_position(
[
cbar_points[0, 0],
points[0, 1],
cbar_points[1, 0] - cbar_points[0, 0],
points[1, 1] - points[0, 1],
]
)
# To see the discrepancy in axis heights uncomment
# the following two lines:
# print(points[1, 1] - points[0, 1])
# print(cbar_points[1, 1] - cbar_points[0, 1])

return cbar
return self.fig.colorbar(img, ax=ax, **kwds)


class ScatterPlot(PlanePlot):
Expand Down
8 changes: 2 additions & 6 deletions pandas/plotting/_matplotlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,8 @@ def handle_shared_axes(
sharey: bool,
):
if nplots > 1:
if compat.mpl_ge_3_2_0():
row_num = lambda x: x.get_subplotspec().rowspan.start
col_num = lambda x: x.get_subplotspec().colspan.start
else:
row_num = lambda x: x.rowNum
col_num = lambda x: x.colNum
row_num = lambda x: x.get_subplotspec().rowspan.start
col_num = lambda x: x.get_subplotspec().colspan.start

if compat.mpl_ge_3_4_0():
is_first_col = lambda x: x.get_subplotspec().is_first_col()
Expand Down
96 changes: 18 additions & 78 deletions pandas/tests/plotting/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from __future__ import annotations

import os
from typing import (
TYPE_CHECKING,
Sequence,
Expand All @@ -22,11 +21,7 @@
from pandas.core.dtypes.api import is_list_like

import pandas as pd
from pandas import (
DataFrame,
Series,
to_datetime,
)
from pandas import Series
import pandas._testing as tm

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

def setup_method(self, method):

def setup_method(self):
import matplotlib as mpl

from pandas.plotting._matplotlib import compat

self.compat = compat

mpl.rcdefaults()

self.start_date_to_int64 = 812419200000000000
self.end_date_to_int64 = 819331200000000000

self.mpl_ge_2_2_3 = compat.mpl_ge_2_2_3()
self.mpl_ge_3_0_0 = compat.mpl_ge_3_0_0()
self.mpl_ge_3_1_0 = compat.mpl_ge_3_1_0()
self.mpl_ge_3_2_0 = compat.mpl_ge_3_2_0()

self.bp_n_objects = 7
self.polycollection_factor = 2
self.default_figsize = (6.4, 4.8)
self.default_tick_position = "left"

n = 100
with tm.RNGContext(42):
gender = np.random.choice(["Male", "Female"], size=n)
classroom = np.random.choice(["A", "B", "C"], size=n)

self.hist_df = DataFrame(
{
"gender": gender,
"classroom": classroom,
"height": np.random.normal(66, 4, size=n),
"weight": np.random.normal(161, 32, size=n),
"category": np.random.randint(4, size=n),
"datetime": to_datetime(
np.random.randint(
self.start_date_to_int64,
self.end_date_to_int64,
size=n,
dtype=np.int64,
)
),
}
)

self.tdf = tm.makeTimeDataFrame()
self.hexbin_df = DataFrame(
{
"A": np.random.uniform(size=20),
"B": np.random.uniform(size=20),
"C": np.arange(20) + np.random.uniform(size=20),
}
)

def teardown_method(self, method):
def teardown_method(self):
tm.close()

@cache_readonly
Expand Down Expand Up @@ -166,13 +111,12 @@ def _check_data(self, xp, rs):
xp_lines = xp.get_lines()
rs_lines = rs.get_lines()

def check_line(xpl, rsl):
assert len(xp_lines) == len(rs_lines)
for xpl, rsl in zip(xp_lines, rs_lines):
xpdata = xpl.get_xydata()
rsdata = rsl.get_xydata()
tm.assert_almost_equal(xpdata, rsdata)

assert len(xp_lines) == len(rs_lines)
[check_line(xpl, rsl) for xpl, rsl in zip(xp_lines, rs_lines)]
tm.close()

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

if figsize is None:
figsize = self.default_figsize
figsize = (6.4, 4.8)
visible_axes = self._flatten_visible(axes)

if axes_num is not None:
Expand Down Expand Up @@ -525,15 +469,8 @@ def _check_grid_settings(self, obj, kinds, kws={}):
def is_grid_on():
xticks = self.plt.gca().xaxis.get_major_ticks()
yticks = self.plt.gca().yaxis.get_major_ticks()
# for mpl 2.2.2, gridOn and gridline.get_visible disagree.
# for new MPL, they are the same.

if self.mpl_ge_3_1_0:
xoff = all(not g.gridline.get_visible() for g in xticks)
yoff = all(not g.gridline.get_visible() for g in yticks)
else:
xoff = all(not g.gridOn for g in xticks)
yoff = all(not g.gridOn for g in yticks)
xoff = all(not g.gridline.get_visible() for g in xticks)
yoff = all(not g.gridline.get_visible() for g in yticks)

return not (xoff and yoff)

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

def get_x_axis(self, ax):
return ax._shared_axes["x"] if self.compat.mpl_ge_3_5_0() else ax._shared_x_axes
from pandas.plotting._matplotlib.compat import mpl_ge_3_5_0

if mpl_ge_3_5_0():
return ax._shared_axes["x"]
return ax._shared_x_axes

def get_y_axis(self, ax):
return ax._shared_axes["y"] if self.compat.mpl_ge_3_5_0() else ax._shared_y_axes
from pandas.plotting._matplotlib.compat import mpl_ge_3_5_0

if mpl_ge_3_5_0():
return ax._shared_axes["y"]
return ax._shared_y_axes


def _check_plot_works(f, filterwarnings="always", default_axes=False, **kwargs):
Expand Down Expand Up @@ -656,8 +601,3 @@ def _gen_two_subplots(f, fig, **kwargs):
else:
kwargs["ax"] = fig.add_subplot(212)
yield f(**kwargs)


def curpath():
pth, _ = os.path.split(os.path.abspath(__file__))
return pth
35 changes: 35 additions & 0 deletions pandas/tests/plotting/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import pytest

from pandas import (
DataFrame,
to_datetime,
)
import pandas._testing as tm


@pytest.fixture
def hist_df():
n = 100
with tm.RNGContext(42):
gender = np.random.choice(["Male", "Female"], size=n)
classroom = np.random.choice(["A", "B", "C"], size=n)

hist_df = DataFrame(
{
"gender": gender,
"classroom": classroom,
"height": np.random.normal(66, 4, size=n),
"weight": np.random.normal(161, 32, size=n),
"category": np.random.randint(4, size=n),
"datetime": to_datetime(
np.random.randint(
812419200000000000,
819331200000000000,
size=n,
dtype=np.int64,
)
),
}
)
return hist_df
Loading