diff --git a/CHANGELOG.md b/CHANGELOG.md index fb07c8c..a745f55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ ### Internal changes +- Avoid using the private `_hatch_color` property and use `set_hatchcolor` and `get_hatchcolor` instead in matplotlib + versions where they are available ([#161](https://github.com/mpytools/mplotutils/pull/161)). - Consolidate package metadata and configuration in *pyproject.toml* ([#165](https://github.com/mpytools/mplotutils/pull/165)). - Replace deprecated `matplotlib.rcsetup.all_backends` with `matplotlib.backends.backend_registry.list_builtin()` ([#160](https://github.com/mpytools/mplotutils/pull/160)). diff --git a/mplotutils/_hatch.py b/mplotutils/_hatch.py index 7e75642..4e4a234 100644 --- a/mplotutils/_hatch.py +++ b/mplotutils/_hatch.py @@ -14,6 +14,7 @@ from mplotutils._mpl import _maybe_gca MPL_GE_310 = Version(Version(mpl.__version__).base_version) >= Version("3.10") +MPL_GE_311 = Version(Version(mpl.__version__).base_version) >= Version("3.11") def hatch(da, hatch, *, ax=None, label=None, linewidth=None, color="0.1"): @@ -225,11 +226,16 @@ def _hatch( label=label, ) - # NOTE: manually overwrites the private _hatch_color property - allows to have - # different ec and hatch color (so the box of the legend is black) - empty_legend_patch._hatch_color = mpl.colors.to_rgba( - mpl.rcParams["hatch.color"] - ) + hatch_color = mpl.colors.to_rgba(mpl.rcParams["hatch.color"]) + + if not MPL_GE_311: + # NOTE: manually overwrites the private _hatch_color property - allows to + # have different ec and hatch color (so the box of the legend is black) + empty_legend_patch._hatch_color = hatch_color + else: + # TODO: is this still needed in MPL_GE_311? + empty_legend_patch.set_hatchcolor(hatch_color) + ax.add_patch(empty_legend_patch) if cyclic: diff --git a/mplotutils/tests/test_hatch.py b/mplotutils/tests/test_hatch.py index cb41c51..a210dd5 100644 --- a/mplotutils/tests/test_hatch.py +++ b/mplotutils/tests/test_hatch.py @@ -10,6 +10,7 @@ from . import assert_no_warnings, subplots_context MPL_GE_310 = Version(Version(mpl.__version__).base_version) >= Version("3.10") +MPL_GE_311 = Version(Version(mpl.__version__).base_version) >= Version("3.11") requires_mpl_ge_310 = pytest.mark.skipif(not MPL_GE_310, reason="requires mpl >= 3.10") @@ -20,6 +21,13 @@ ) +def get_hatchcolor(h): + if MPL_GE_311: + return mpl.colors.to_rgba(h.get_hatchcolor()) + + return h._hatch_color + + @pytest.mark.parametrize("obj", (None, xr.Dataset(), np.array([]))) @pytest.mark.parametrize("function", HATCH_FUNCTIONS) def test_hatch_not_a_dataarray(obj, function): @@ -91,7 +99,7 @@ def test_hatch_label(function): (rect,) = h assert rect.get_label() == "label" - assert mpl.colors.to_rgba("0.1") == rect._hatch_color + assert mpl.colors.to_rgba("0.1") == get_hatchcolor(rect) # test 2 labels with non-default color with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax): @@ -106,7 +114,7 @@ def test_hatch_label(function): for i, rect in enumerate(h): assert rect.get_label() == f"label{i}" - assert mpl.colors.to_rgba("#2ca25f") == rect._hatch_color + assert mpl.colors.to_rgba("#2ca25f") == get_hatchcolor(rect) @pytest.mark.skipif(MPL_GE_310, reason="only for mpl < 3.10") @@ -215,16 +223,16 @@ def test_hatch_color(function): with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax): h = function(da, "*", ax=ax) - assert mpl.colors.to_rgba("0.1") == h._hatch_color + assert mpl.colors.to_rgba("0.1") == get_hatchcolor(h) # different colors can be set with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax): h = function(da, "*", ax=ax, color="#2ca25f") - assert mpl.colors.to_rgba("#2ca25f") == h._hatch_color + assert mpl.colors.to_rgba("#2ca25f") == get_hatchcolor(h) h = function(da, "*", ax=ax, color="#e5f5f9") - assert mpl.colors.to_rgba("#e5f5f9") == h._hatch_color + assert mpl.colors.to_rgba("#e5f5f9") == get_hatchcolor(h) def test_hatch_bbox():