33import numpy as np
44import pytest
55import xarray as xr
6+ from packaging .version import Version
67
78import mplotutils as mpu
89
910from . 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+
1116HATCH_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 )
148203def test_hatch_color (function ):
149204
0 commit comments