Skip to content

Commit fb6fe53

Browse files
committed
adapt hatch for matplotlib 3.10 (#143)
* adapt hatch for matplotlib ge 3.10 * changelog
1 parent 889a6e7 commit fb6fe53

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
2. `mpu.hatch_map`: for cartopy GeoAxes
2828
3. `mpu.hatch_map_global`: as 2. but also adds a cyclic point to the array
2929

30-
all three functions expect a 2D boolean `xr.DataArray` and a hatch pattern. Values that are `True` are hatched.
30+
all three functions expect a 2D boolean `xr.DataArray` and a hatch pattern. Values that are `True` are hatched
31+
([#123](https://github.com/mathause/mplotutils/pull/123) and [#143](https://github.com/mathause/mplotutils/pull/143)).
32+
3133

3234
- Enable passing `AxesGrid` (from `mpl_toolkits.axes_grid1`) to `set_map_layout` ([#116](https://github.com/mathause/mplotutils/pull/116)).
3335
- Raise more informative error when a wrong type is passed to `set_map_layout` ([#121](https://github.com/mathause/mplotutils/pull/121)).

mplotutils/_hatch.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import matplotlib as mpl
55
import numpy as np
66
import xarray as xr
7+
from packaging.version import Version
78

89
import mplotutils as mpu
910

@@ -12,6 +13,8 @@
1213

1314
from mplotutils._mpl import _maybe_gca
1415

16+
MPL_GE_310 = Version(Version(mpl.__version__).base_version) >= Version("3.10")
17+
1518

1619
def hatch(da, hatch, *, ax=None, label=None, linewidth=None, color="0.1"):
1720
"""add hatch pattern to an axes
@@ -185,22 +188,29 @@ def _hatch(
185188

186189
fig = ax.figure
187190

188-
# only one linewidth is possible per figure (actually it is just read before
189-
# saving the figure, so the above is not 100 % correct)
190-
if linewidth is None:
191-
# only set linewidth if not yet set
192-
if not _HATCHES_PER_FIGURE.get(fig):
193-
mpl.rcParams["hatch.linewidth"] = 0.25
191+
if not MPL_GE_310:
192+
# only one linewidth is possible per figure (actually it is just read before
193+
# saving the figure, so the above is not 100 % correct)
194+
if linewidth is None:
195+
# only set linewidth if not yet set
196+
if not _HATCHES_PER_FIGURE.get(fig):
197+
mpl.rcParams["hatch.linewidth"] = 0.25
198+
else:
199+
if _HATCHES_PER_FIGURE.get(fig):
200+
warnings.warn(
201+
"Setting more than one hatch `linewidth` per figure requires"
202+
" matplotlib v3.10 or later Overwriting previous value of"
203+
f" {_HATCHES_PER_FIGURE[fig]}."
204+
)
205+
206+
mpl.rcParams["hatch.linewidth"] = linewidth
207+
208+
_HATCHES_PER_FIGURE[fig] = linewidth
194209
else:
195-
if _HATCHES_PER_FIGURE.get(fig):
196-
warnings.warn(
197-
"Can only set one `linewidth` per figure. Overwriting previous value of"
198-
f" {_HATCHES_PER_FIGURE[fig]}."
199-
)
200-
201-
mpl.rcParams["hatch.linewidth"] = linewidth
202-
203-
_HATCHES_PER_FIGURE[fig] = linewidth
210+
if linewidth is None:
211+
mpl.rcParams["hatch.linewidth"] = 0.25
212+
else:
213+
mpl.rcParams["hatch.linewidth"] = linewidth
204214

205215
mpl.rcParams["hatch.color"] = color
206216

mplotutils/tests/test_hatch.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import numpy as np
44
import pytest
55
import xarray as xr
6+
from packaging.version import Version
67

78
import mplotutils as mpu
89

910
from . import assert_no_warnings, subplots_context
1011

12+
MPL_GE_310 = Version(Version(mpl.__version__).base_version) >= Version("3.10")
13+
requires_mpl_ge_310 = pytest.mark.skipif(not MPL_GE_310, reason="requires mpl >= 3.10")
14+
15+
1116
HATCH_FUNCTIONS = (
1217
pytest.param(mpu.hatch, id="hatch"),
1318
pytest.param(mpu.hatch_map, id="hatch_map"),
@@ -104,8 +109,9 @@ def test_hatch_label(function):
104109
assert mpl.colors.to_rgba("#2ca25f") == rect._hatch_color
105110

106111

112+
@pytest.mark.skipif(MPL_GE_310, reason="only for mpl < 3.10")
107113
@pytest.mark.parametrize("function", HATCH_FUNCTIONS)
108-
def test_hatch_linewidth(function):
114+
def test_hatch_linewidth_mpl_lt_310(function):
109115

110116
da = xr.DataArray(
111117
np.ones([3, 3], dtype=bool),
@@ -132,18 +138,67 @@ def test_hatch_linewidth(function):
132138

133139
assert mpl.rcParams["hatch.linewidth"] == 1
134140

135-
# changing away from the default linewidth does not raise a warning
141+
# changing away from the linewidth does raise a warning
136142
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
137143

138144
function(da, "*", ax=ax, linewidth=2)
139145
assert mpl.rcParams["hatch.linewidth"] == 2
140146

141-
with pytest.warns(match="Can only set one `linewidth` per figure"):
147+
with pytest.warns(match="Setting more than one hatch `linewidth`"):
142148
function(da, "*", ax=ax, linewidth=1)
143149

144150
assert mpl.rcParams["hatch.linewidth"] == 1
145151

146152

153+
@requires_mpl_ge_310
154+
@pytest.mark.parametrize("function", HATCH_FUNCTIONS)
155+
def test_hatch_linewidth_mpl_ge_310(function):
156+
157+
da = xr.DataArray(
158+
np.ones([3, 3], dtype=bool),
159+
dims=("lat", "lon"),
160+
coords={"lat": [0, 1, 2], "lon": [1, 2, 3]},
161+
)
162+
163+
subplot_kw = {"projection": ccrs.PlateCarree()}
164+
165+
# test linewidth default width
166+
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
167+
q = function(da, "*", ax=ax)
168+
assert q.get_hatch_linewidth() == 0.25
169+
assert mpl.rcParams["hatch.linewidth"] == 0.25
170+
171+
# changing away from the default linewidth does not raise a warning
172+
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
173+
174+
q = function(da, "*", ax=ax)
175+
assert q.get_hatch_linewidth() == 0.25
176+
assert mpl.rcParams["hatch.linewidth"] == 0.25
177+
178+
with assert_no_warnings():
179+
q = function(da, "*", ax=ax, linewidth=1)
180+
181+
assert q.get_hatch_linewidth() == 1
182+
assert mpl.rcParams["hatch.linewidth"] == 1
183+
184+
q = function(da, "*", ax=ax)
185+
assert q.get_hatch_linewidth() == 0.25
186+
assert mpl.rcParams["hatch.linewidth"] == 0.25
187+
188+
# changing away from the linewidth does NOT raise a warning
189+
with subplots_context(1, 1, subplot_kw=subplot_kw) as (__, ax):
190+
191+
q = function(da, "*", ax=ax, linewidth=2)
192+
assert q.get_hatch_linewidth() == 2
193+
assert mpl.rcParams["hatch.linewidth"] == 2
194+
195+
with assert_no_warnings():
196+
q = function(da, "*", ax=ax, linewidth=1)
197+
198+
assert q.get_hatch_linewidth() == 1
199+
assert mpl.rcParams["hatch.linewidth"] == 1
200+
201+
147202
@pytest.mark.parametrize("function", HATCH_FUNCTIONS)
148203
def test_hatch_color(function):
149204

0 commit comments

Comments
 (0)