Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
16 changes: 11 additions & 5 deletions mplotutils/_hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 13 additions & 5 deletions mplotutils/tests/test_hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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")
Expand Down Expand Up @@ -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():
Expand Down
Loading