Skip to content

Commit b58c32e

Browse files
authored
use public get_/ set_hatchcolor in newer mpl (#161)
* use public get_/ set_hatchcolor in newer mpl * changelog
1 parent 5d09e68 commit b58c32e

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
### Internal changes
2424

25+
- Avoid using the private `_hatch_color` property and use `set_hatchcolor` and `get_hatchcolor` instead in matplotlib
26+
versions where they are available ([#161](https://github.com/mpytools/mplotutils/pull/161)).
2527
- Consolidate package metadata and configuration in *pyproject.toml* ([#165](https://github.com/mpytools/mplotutils/pull/165)).
2628
- Replace deprecated `matplotlib.rcsetup.all_backends` with `matplotlib.backends.backend_registry.list_builtin()`
2729
([#160](https://github.com/mpytools/mplotutils/pull/160)).

mplotutils/_hatch.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from mplotutils._mpl import _maybe_gca
1515

1616
MPL_GE_310 = Version(Version(mpl.__version__).base_version) >= Version("3.10")
17+
MPL_GE_311 = Version(Version(mpl.__version__).base_version) >= Version("3.11")
1718

1819

1920
def hatch(da, hatch, *, ax=None, label=None, linewidth=None, color="0.1"):
@@ -225,11 +226,16 @@ def _hatch(
225226
label=label,
226227
)
227228

228-
# NOTE: manually overwrites the private _hatch_color property - allows to have
229-
# different ec and hatch color (so the box of the legend is black)
230-
empty_legend_patch._hatch_color = mpl.colors.to_rgba(
231-
mpl.rcParams["hatch.color"]
232-
)
229+
hatch_color = mpl.colors.to_rgba(mpl.rcParams["hatch.color"])
230+
231+
if not MPL_GE_311:
232+
# NOTE: manually overwrites the private _hatch_color property - allows to
233+
# have different ec and hatch color (so the box of the legend is black)
234+
empty_legend_patch._hatch_color = hatch_color
235+
else:
236+
# TODO: is this still needed in MPL_GE_311?
237+
empty_legend_patch.set_hatchcolor(hatch_color)
238+
233239
ax.add_patch(empty_legend_patch)
234240

235241
if cyclic:

mplotutils/tests/test_hatch.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from . import assert_no_warnings, subplots_context
1111

1212
MPL_GE_310 = Version(Version(mpl.__version__).base_version) >= Version("3.10")
13+
MPL_GE_311 = Version(Version(mpl.__version__).base_version) >= Version("3.11")
1314
requires_mpl_ge_310 = pytest.mark.skipif(not MPL_GE_310, reason="requires mpl >= 3.10")
1415

1516

@@ -20,6 +21,13 @@
2021
)
2122

2223

24+
def get_hatchcolor(h):
25+
if MPL_GE_311:
26+
return mpl.colors.to_rgba(h.get_hatchcolor())
27+
28+
return h._hatch_color
29+
30+
2331
@pytest.mark.parametrize("obj", (None, xr.Dataset(), np.array([])))
2432
@pytest.mark.parametrize("function", HATCH_FUNCTIONS)
2533
def test_hatch_not_a_dataarray(obj, function):
@@ -91,7 +99,7 @@ def test_hatch_label(function):
9199
(rect,) = h
92100

93101
assert rect.get_label() == "label"
94-
assert mpl.colors.to_rgba("0.1") == rect._hatch_color
102+
assert mpl.colors.to_rgba("0.1") == get_hatchcolor(rect)
95103

96104
# test 2 labels with non-default color
97105
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
@@ -106,7 +114,7 @@ def test_hatch_label(function):
106114

107115
for i, rect in enumerate(h):
108116
assert rect.get_label() == f"label{i}"
109-
assert mpl.colors.to_rgba("#2ca25f") == rect._hatch_color
117+
assert mpl.colors.to_rgba("#2ca25f") == get_hatchcolor(rect)
110118

111119

112120
@pytest.mark.skipif(MPL_GE_310, reason="only for mpl < 3.10")
@@ -215,16 +223,16 @@ def test_hatch_color(function):
215223
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
216224
h = function(da, "*", ax=ax)
217225

218-
assert mpl.colors.to_rgba("0.1") == h._hatch_color
226+
assert mpl.colors.to_rgba("0.1") == get_hatchcolor(h)
219227

220228
# different colors can be set
221229
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
222230

223231
h = function(da, "*", ax=ax, color="#2ca25f")
224-
assert mpl.colors.to_rgba("#2ca25f") == h._hatch_color
232+
assert mpl.colors.to_rgba("#2ca25f") == get_hatchcolor(h)
225233

226234
h = function(da, "*", ax=ax, color="#e5f5f9")
227-
assert mpl.colors.to_rgba("#e5f5f9") == h._hatch_color
235+
assert mpl.colors.to_rgba("#e5f5f9") == get_hatchcolor(h)
228236

229237

230238
def test_hatch_bbox():

0 commit comments

Comments
 (0)