3
3
import numpy as np
4
4
import pytest
5
5
import xarray as xr
6
+ from packaging .version import Version
6
7
7
8
import mplotutils as mpu
8
9
9
10
from . import assert_no_warnings , subplots_context
10
11
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
+
11
16
HATCH_FUNCTIONS = (
12
17
pytest .param (mpu .hatch , id = "hatch" ),
13
18
pytest .param (mpu .hatch_map , id = "hatch_map" ),
@@ -104,8 +109,9 @@ def test_hatch_label(function):
104
109
assert mpl .colors .to_rgba ("#2ca25f" ) == rect ._hatch_color
105
110
106
111
112
+ @pytest .mark .skipif (MPL_GE_310 , reason = "only for mpl < 3.10" )
107
113
@pytest .mark .parametrize ("function" , HATCH_FUNCTIONS )
108
- def test_hatch_linewidth (function ):
114
+ def test_hatch_linewidth_mpl_lt_310 (function ):
109
115
110
116
da = xr .DataArray (
111
117
np .ones ([3 , 3 ], dtype = bool ),
@@ -132,18 +138,67 @@ def test_hatch_linewidth(function):
132
138
133
139
assert mpl .rcParams ["hatch.linewidth" ] == 1
134
140
135
- # changing away from the default linewidth does not raise a warning
141
+ # changing away from the linewidth does raise a warning
136
142
with subplots_context (1 , 1 , subplot_kw = subplot_kw ) as (__ , ax ):
137
143
138
144
function (da , "*" , ax = ax , linewidth = 2 )
139
145
assert mpl .rcParams ["hatch.linewidth" ] == 2
140
146
141
- with pytest .warns (match = "Can only set one `linewidth` per figure " ):
147
+ with pytest .warns (match = "Setting more than one hatch `linewidth`" ):
142
148
function (da , "*" , ax = ax , linewidth = 1 )
143
149
144
150
assert mpl .rcParams ["hatch.linewidth" ] == 1
145
151
146
152
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
+
147
202
@pytest .mark .parametrize ("function" , HATCH_FUNCTIONS )
148
203
def test_hatch_color (function ):
149
204
0 commit comments