Skip to content

Commit 785708f

Browse files
authored
Cleanup compat code for obsolete versions of dependencies (#3607)
1 parent 45a098f commit 785708f

File tree

12 files changed

+44
-115
lines changed

12 files changed

+44
-115
lines changed

seaborn/_base.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,11 +1160,7 @@ def _attach(
11601160
# For categorical y, we want the "first" level to be at the top of the axis
11611161
if self.var_types.get("y", None) == "categorical":
11621162
for ax in ax_list:
1163-
try:
1164-
ax.yaxis.set_inverted(True)
1165-
except AttributeError: # mpl < 3.1
1166-
if not ax.yaxis_inverted():
1167-
ax.invert_yaxis()
1163+
ax.yaxis.set_inverted(True)
11681164

11691165
# TODO -- Add axes labels
11701166

seaborn/_compat.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@
77
from seaborn.utils import _version_predates
88

99

10-
def MarkerStyle(marker=None, fillstyle=None):
11-
"""
12-
Allow MarkerStyle to accept a MarkerStyle object as parameter.
13-
14-
Supports matplotlib < 3.3.0
15-
https://github.com/matplotlib/matplotlib/pull/16692
16-
17-
"""
18-
if isinstance(marker, mpl.markers.MarkerStyle):
19-
if fillstyle is None:
20-
return marker
21-
else:
22-
marker = marker.get_marker()
23-
return mpl.markers.MarkerStyle(marker, fillstyle)
24-
25-
2610
def norm_from_scale(scale, norm):
2711
"""Produce a Normalize object given a Scale and min/max domain limits."""
2812
# This is an internal maplotlib function that simplifies things to access

seaborn/_core/plot.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ def layout(
858858

859859
# TODO def legend (ugh)
860860

861-
def theme(self, *args: dict[str, Any]) -> Plot:
861+
def theme(self, config: dict[str, Any], /) -> Plot:
862862
"""
863863
Control the appearance of elements in the plot.
864864
@@ -880,13 +880,7 @@ def theme(self, *args: dict[str, Any]) -> Plot:
880880
"""
881881
new = self._clone()
882882

883-
# We can skip this whole block on Python 3.8+ with positional-only syntax
884-
nargs = len(args)
885-
if nargs != 1:
886-
err = f"theme() takes 1 positional argument, but {nargs} were given"
887-
raise TypeError(err)
888-
889-
rc = mpl.RcParams(args[0])
883+
rc = mpl.RcParams(config)
890884
new._theme.update(rc)
891885

892886
return new

seaborn/_core/properties.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@
33
import warnings
44

55
import numpy as np
6+
from numpy.typing import ArrayLike
67
from pandas import Series
78
import matplotlib as mpl
89
from matplotlib.colors import to_rgb, to_rgba, to_rgba_array
10+
from matplotlib.markers import MarkerStyle
911
from matplotlib.path import Path
1012

1113
from seaborn._core.scales import Scale, Boolean, Continuous, Nominal, Temporal
1214
from seaborn._core.rules import categorical_order, variable_type
13-
from seaborn._compat import MarkerStyle
1415
from seaborn.palettes import QUAL_PALETTES, color_palette, blend_palette
1516
from seaborn.utils import get_color_cycle
1617

1718
from typing import Any, Callable, Tuple, List, Union, Optional
1819

19-
try:
20-
from numpy.typing import ArrayLike
21-
except ImportError:
22-
# numpy<1.20.0 (Jan 2021)
23-
ArrayLike = Any
24-
2520
RGBTuple = Tuple[float, float, float]
2621
RGBATuple = Tuple[float, float, float, float]
2722
ColorSpec = Union[RGBTuple, RGBATuple, str]

seaborn/_statistics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class instantiation.
2525
2626
"""
2727
from numbers import Number
28+
from statistics import NormalDist
2829
import numpy as np
2930
import pandas as pd
3031
try:
@@ -35,7 +36,7 @@ class instantiation.
3536
_no_scipy = True
3637

3738
from .algorithms import bootstrap
38-
from .utils import _check_argument, _normal_quantile_func
39+
from .utils import _check_argument
3940

4041

4142
class KDE:
@@ -627,7 +628,8 @@ def _compute_k(self, n):
627628
elif self.k_depth == "proportion":
628629
k = int(np.log2(n)) - int(np.log2(n * self.outlier_prop)) + 1
629630
elif self.k_depth == "trustworthy":
630-
point_conf = 2 * _normal_quantile_func(1 - self.trust_alpha / 2) ** 2
631+
normal_quantile_func = np.vectorize(NormalDist().inv_cdf)
632+
point_conf = 2 * normal_quantile_func(1 - self.trust_alpha / 2) ** 2
631633
k = int(np.log2(n / point_conf)) + 1
632634
else:
633635
# Allow having k directly specified as input

seaborn/categorical.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import pandas as pd
99

1010
import matplotlib as mpl
11+
from matplotlib.cbook import normalize_kwargs
1112
from matplotlib.collections import PatchCollection
13+
from matplotlib.markers import MarkerStyle
1214
from matplotlib.patches import Rectangle
1315
import matplotlib.pyplot as plt
1416

@@ -23,11 +25,9 @@
2325
_default_color,
2426
_get_patch_legend_artist,
2527
_get_transform_functions,
26-
_normalize_kwargs,
2728
_scatter_legend_artist,
2829
_version_predates,
2930
)
30-
from seaborn._compat import MarkerStyle
3131
from seaborn._statistics import (
3232
EstimateAggregator,
3333
LetterValues,
@@ -605,7 +605,7 @@ def plot_boxes(
605605
value_var = {"x": "y", "y": "x"}[self.orient]
606606

607607
def get_props(element, artist=mpl.lines.Line2D):
608-
return _normalize_kwargs(plot_kws.pop(f"{element}props", {}), artist)
608+
return normalize_kwargs(plot_kws.pop(f"{element}props", {}), artist)
609609

610610
if not fill and linewidth is None:
611611
linewidth = mpl.rcParams["lines.linewidth"]
@@ -1175,7 +1175,7 @@ def plot_points(
11751175
agg_var = {"x": "y", "y": "x"}[self.orient]
11761176
iter_vars = ["hue"]
11771177

1178-
plot_kws = _normalize_kwargs(plot_kws, mpl.lines.Line2D)
1178+
plot_kws = normalize_kwargs(plot_kws, mpl.lines.Line2D)
11791179
plot_kws.setdefault("linewidth", mpl.rcParams["lines.linewidth"] * 1.8)
11801180
plot_kws.setdefault("markeredgewidth", plot_kws["linewidth"] * 0.75)
11811181
plot_kws.setdefault("markersize", plot_kws["linewidth"] * np.sqrt(2 * np.pi))
@@ -2371,7 +2371,7 @@ def barplot(
23712371

23722372
agg_cls = WeightedAggregator if "weight" in p.plot_data else EstimateAggregator
23732373
aggregator = agg_cls(estimator, errorbar, n_boot=n_boot, seed=seed)
2374-
err_kws = {} if err_kws is None else _normalize_kwargs(err_kws, mpl.lines.Line2D)
2374+
err_kws = {} if err_kws is None else normalize_kwargs(err_kws, mpl.lines.Line2D)
23752375

23762376
# Deprecations to remove in v0.15.0.
23772377
err_kws, capsize = p._err_kws_backcompat(err_kws, errcolor, errwidth, capsize)
@@ -2506,7 +2506,7 @@ def pointplot(
25062506

25072507
agg_cls = WeightedAggregator if "weight" in p.plot_data else EstimateAggregator
25082508
aggregator = agg_cls(estimator, errorbar, n_boot=n_boot, seed=seed)
2509-
err_kws = {} if err_kws is None else _normalize_kwargs(err_kws, mpl.lines.Line2D)
2509+
err_kws = {} if err_kws is None else normalize_kwargs(err_kws, mpl.lines.Line2D)
25102510

25112511
# Deprecations to remove in v0.15.0.
25122512
p._point_kwargs_backcompat(scale, join, kwargs)
@@ -2848,7 +2848,7 @@ def catplot(
28482848
color = desaturate(color, saturation)
28492849

28502850
if kind in ["strip", "swarm"]:
2851-
kwargs = _normalize_kwargs(kwargs, mpl.collections.PathCollection)
2851+
kwargs = normalize_kwargs(kwargs, mpl.collections.PathCollection)
28522852
kwargs["edgecolor"] = p._complement_color(
28532853
kwargs.pop("edgecolor", default), color, p._hue_map
28542854
)
@@ -3023,14 +3023,14 @@ def catplot(
30233023
# Deprecations to remove in v0.15.0.
30243024
# TODO Uncomment when removing deprecation backcompat
30253025
# capsize = kwargs.pop("capsize", 0)
3026-
# err_kws = _normalize_kwargs(kwargs.pop("err_kws", {}), mpl.lines.Line2D)
3026+
# err_kws = normalize_kwargs(kwargs.pop("err_kws", {}), mpl.lines.Line2D)
30273027
p._point_kwargs_backcompat(
30283028
kwargs.pop("scale", deprecated),
30293029
kwargs.pop("join", deprecated),
30303030
kwargs
30313031
)
30323032
err_kws, capsize = p._err_kws_backcompat(
3033-
_normalize_kwargs(kwargs.pop("err_kws", {}), mpl.lines.Line2D),
3033+
normalize_kwargs(kwargs.pop("err_kws", {}), mpl.lines.Line2D),
30343034
None,
30353035
errwidth=kwargs.pop("errwidth", deprecated),
30363036
capsize=kwargs.pop("capsize", 0),
@@ -3052,7 +3052,7 @@ def catplot(
30523052
aggregator = agg_cls(estimator, errorbar, n_boot=n_boot, seed=seed)
30533053

30543054
err_kws, capsize = p._err_kws_backcompat(
3055-
_normalize_kwargs(kwargs.pop("err_kws", {}), mpl.lines.Line2D),
3055+
normalize_kwargs(kwargs.pop("err_kws", {}), mpl.lines.Line2D),
30563056
errcolor=kwargs.pop("errcolor", deprecated),
30573057
errwidth=kwargs.pop("errwidth", deprecated),
30583058
capsize=kwargs.pop("capsize", 0),

seaborn/distributions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import matplotlib as mpl
1111
import matplotlib.pyplot as plt
1212
import matplotlib.transforms as tx
13+
from matplotlib.cbook import normalize_kwargs
1314
from matplotlib.colors import to_rgba
1415
from matplotlib.collections import LineCollection
1516

@@ -28,7 +29,6 @@
2829
remove_na,
2930
_get_transform_functions,
3031
_kde_support,
31-
_normalize_kwargs,
3232
_check_argument,
3333
_assign_default_kwargs,
3434
_default_color,
@@ -171,7 +171,7 @@ def _artist_kws(self, kws, fill, element, multiple, color, alpha):
171171
"""Handle differences between artists in filled/unfilled plots."""
172172
kws = kws.copy()
173173
if fill:
174-
kws = _normalize_kwargs(kws, mpl.collections.PolyCollection)
174+
kws = normalize_kwargs(kws, mpl.collections.PolyCollection)
175175
kws.setdefault("facecolor", to_rgba(color, alpha))
176176

177177
if element == "bars":
@@ -916,7 +916,7 @@ def plot_univariate_density(
916916
artist = mpl.collections.PolyCollection
917917
else:
918918
artist = mpl.lines.Line2D
919-
plot_kws = _normalize_kwargs(plot_kws, artist)
919+
plot_kws = normalize_kwargs(plot_kws, artist)
920920

921921
# Input checking
922922
_check_argument("multiple", ["layer", "stack", "fill"], multiple)

seaborn/relational.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66
import matplotlib as mpl
77
import matplotlib.pyplot as plt
8+
from matplotlib.cbook import normalize_kwargs
89

910
from ._base import (
1011
VectorPlotter,
@@ -14,7 +15,6 @@
1415
_default_color,
1516
_deprecate_ci,
1617
_get_transform_functions,
17-
_normalize_kwargs,
1818
_scatter_legend_artist,
1919
)
2020
from ._statistics import EstimateAggregator, WeightedAggregator
@@ -237,7 +237,7 @@ def plot(self, ax, kws):
237237
# gotten from the corresponding matplotlib function, and calling the
238238
# function will advance the axes property cycle.
239239

240-
kws = _normalize_kwargs(kws, mpl.lines.Line2D)
240+
kws = normalize_kwargs(kws, mpl.lines.Line2D)
241241
kws.setdefault("markeredgewidth", 0.75)
242242
kws.setdefault("markeredgecolor", "w")
243243

@@ -400,7 +400,7 @@ def plot(self, ax, kws):
400400
if data.empty:
401401
return
402402

403-
kws = _normalize_kwargs(kws, mpl.collections.PathCollection)
403+
kws = normalize_kwargs(kws, mpl.collections.PathCollection)
404404

405405
# Define the vectors of x and y positions
406406
empty = np.full(len(data), np.nan)

seaborn/utils.py

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,6 @@ def ci_to_errsize(cis, heights):
5555
return errsize
5656

5757

58-
def _normal_quantile_func(q):
59-
"""
60-
Compute the quantile function of the standard normal distribution.
61-
62-
This wrapper exists because we are dropping scipy as a mandatory dependency
63-
but statistics.NormalDist was added to the standard library in 3.8.
64-
65-
"""
66-
try:
67-
from statistics import NormalDist
68-
qf = np.vectorize(NormalDist().inv_cdf)
69-
except ImportError:
70-
try:
71-
from scipy.stats import norm
72-
qf = norm.ppf
73-
except ImportError:
74-
msg = (
75-
"Standard normal quantile functions require either Python>=3.8 or scipy"
76-
)
77-
raise RuntimeError(msg)
78-
return qf(q)
79-
80-
8158
def _draw_figure(fig):
8259
"""Force draw of a matplotlib figure, accounting for back-compat."""
8360
# See https://github.com/matplotlib/matplotlib/issues/19197 for context
@@ -110,7 +87,7 @@ def _default_color(method, hue, color, kws, saturation=1):
11087

11188
elif method.__name__ == "plot":
11289

113-
color = _normalize_kwargs(kws, mpl.lines.Line2D).get("color")
90+
color = normalize_kwargs(kws, mpl.lines.Line2D).get("color")
11491
scout, = method([], [], scalex=False, scaley=False, color=color)
11592
color = scout.get_color()
11693
scout.remove()
@@ -155,7 +132,7 @@ def _default_color(method, hue, color, kws, saturation=1):
155132

156133
elif method.__name__ == "fill_between":
157134

158-
kws = _normalize_kwargs(kws, mpl.collections.PolyCollection)
135+
kws = normalize_kwargs(kws, mpl.collections.PolyCollection)
159136
scout = method([], [], **kws)
160137
facecolor = scout.get_facecolor()
161138
color = to_rgb(facecolor[0])
@@ -714,11 +691,7 @@ def get_view_interval(self):
714691
formatter.set_scientific(False)
715692
formatter.axis = dummy_axis()
716693

717-
# TODO: The following two lines should be replaced
718-
# once pinned matplotlib>=3.1.0 with:
719-
# formatted_levels = formatter.format_ticks(raw_levels)
720-
formatter.set_locs(raw_levels)
721-
formatted_levels = [formatter(x) for x in raw_levels]
694+
formatted_levels = formatter.format_ticks(raw_levels)
722695

723696
return raw_levels, formatted_levels
724697

@@ -774,26 +747,6 @@ def to_utf8(obj):
774747
return str(obj)
775748

776749

777-
def _normalize_kwargs(kws, artist):
778-
"""Wrapper for mpl.cbook.normalize_kwargs that supports <= 3.2.1."""
779-
_alias_map = {
780-
'color': ['c'],
781-
'linewidth': ['lw'],
782-
'linestyle': ['ls'],
783-
'facecolor': ['fc'],
784-
'edgecolor': ['ec'],
785-
'markerfacecolor': ['mfc'],
786-
'markeredgecolor': ['mec'],
787-
'markeredgewidth': ['mew'],
788-
'markersize': ['ms']
789-
}
790-
try:
791-
kws = normalize_kwargs(kws, artist)
792-
except AttributeError:
793-
kws = normalize_kwargs(kws, _alias_map)
794-
return kws
795-
796-
797750
def _check_argument(param, options, value, prefix=False):
798751
"""Raise if value for param is not in options."""
799752
if prefix and value is not None:
@@ -905,7 +858,7 @@ def _version_predates(lib: ModuleType, version: str) -> bool:
905858

906859
def _scatter_legend_artist(**kws):
907860

908-
kws = _normalize_kwargs(kws, mpl.collections.PathCollection)
861+
kws = normalize_kwargs(kws, mpl.collections.PathCollection)
909862

910863
edgecolor = kws.pop("edgecolor", None)
911864
rc = mpl.rcParams

tests/_core/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ def test_theme_params(self):
934934
def test_theme_error(self):
935935

936936
p = Plot()
937-
with pytest.raises(TypeError, match=r"theme\(\) takes 1 positional"):
937+
with pytest.raises(TypeError, match=r"theme\(\) takes 2 positional"):
938938
p.theme("arg1", "arg2")
939939

940940
def test_theme_validation(self):

0 commit comments

Comments
 (0)