1
+ import matplotlib .pyplot as plt
1
2
import numpy as np
2
3
import pytest
3
4
4
5
from mplotutils import set_map_layout
5
6
6
- from . import subplots_context
7
+ from . import figure_context , subplots_context
8
+
9
+
10
+ def get_rtol (f ):
11
+ # macosx is only exact up to 1 / dpi
12
+
13
+ if plt .get_backend ().lower () != "macosx" :
14
+ rtol = 1e-07
15
+ else :
16
+ rtol = 1 / f .get_dpi ()
17
+ return rtol
7
18
8
19
9
20
def test_set_map_layout_default_width ():
10
21
with subplots_context () as (f , ax ):
11
22
set_map_layout (ax )
12
23
13
- assert f .get_size_inches ()[0 ] * 2.54 == 17.0
24
+ width = f .get_size_inches ()[0 ] * 2.54
25
+ np .testing .assert_allclose (width , 17.0 , rtol = get_rtol (f ))
14
26
15
27
16
28
@pytest .mark .parametrize (
@@ -25,7 +37,7 @@ def test_set_map_layout_no_borders(nrow_ncol):
25
37
set_map_layout (ax , 10 , ** nrow_ncol )
26
38
27
39
width , height = f .get_size_inches () * 2.54
28
- assert np .allclose ((width , height ), (10 , 10 ))
40
+ np .testing . assert_allclose ((width , height ), (10 , 10 ), rtol = get_rtol ( f ))
29
41
30
42
# width:height = 2:1
31
43
with subplots_context () as (f , ax ):
@@ -35,7 +47,7 @@ def test_set_map_layout_no_borders(nrow_ncol):
35
47
set_map_layout (ax , 10 , ** nrow_ncol )
36
48
37
49
width , height = f .get_size_inches () * 2.54
38
- assert np .allclose ((width , height ), (10 , 5 ))
50
+ np .testing . assert_allclose ((width , height ), (10 , 5 ), rtol = get_rtol ( f ))
39
51
40
52
# width:height = 1:2
41
53
with subplots_context () as (f , ax ):
@@ -45,7 +57,7 @@ def test_set_map_layout_no_borders(nrow_ncol):
45
57
set_map_layout (ax , 10 , ** nrow_ncol )
46
58
47
59
width , height = f .get_size_inches () * 2.54
48
- assert np .allclose ((width , height ), (10 , 20 ))
60
+ np .testing . assert_allclose ((width , height ), (10 , 20 ), rtol = get_rtol ( f ))
49
61
50
62
51
63
@pytest .mark .parametrize ("ax_to_arr" , (lambda ax : [ax ], lambda ax : np .array (ax )))
@@ -58,7 +70,7 @@ def test_set_map_layout_ax_arr(ax_to_arr):
58
70
set_map_layout (ax_to_arr (ax ), 10 )
59
71
60
72
width , height = f .get_size_inches () * 2.54
61
- assert np .allclose ((width , height ), (10 , 10 ))
73
+ np .testing . assert_allclose ((width , height ), (10 , 10 ), rtol = get_rtol ( f ))
62
74
63
75
64
76
def test_set_map_layout_vert_borders ():
@@ -72,7 +84,7 @@ def test_set_map_layout_vert_borders():
72
84
set_map_layout (ax , 10 )
73
85
74
86
width , height = f .get_size_inches () * 2.54
75
- assert np .allclose ((width , height ), (10 , 20 ))
87
+ np .testing . assert_allclose ((width , height ), (10 , 20 ), rtol = get_rtol ( f ))
76
88
77
89
# width:height = 1:1
78
90
with subplots_context () as (f , ax ):
@@ -84,7 +96,7 @@ def test_set_map_layout_vert_borders():
84
96
set_map_layout (ax , 10 )
85
97
86
98
width , height = f .get_size_inches () * 2.54
87
- assert np .allclose ((width , height ), (10 , 12.5 ))
99
+ np .testing . assert_allclose ((width , height ), (10 , 12.5 ), rtol = get_rtol ( f ))
88
100
89
101
# width:height = 1:1
90
102
with subplots_context () as (f , ax ):
@@ -96,7 +108,7 @@ def test_set_map_layout_vert_borders():
96
108
set_map_layout (ax , 10 )
97
109
98
110
width , height = f .get_size_inches () * 2.54
99
- assert np .allclose ((width , height ), (10 , 40 ))
111
+ np .testing . assert_allclose ((width , height ), (10 , 40 ), rtol = get_rtol ( f ))
100
112
101
113
102
114
def test_set_map_layout_horz_borders ():
@@ -110,7 +122,7 @@ def test_set_map_layout_horz_borders():
110
122
set_map_layout (ax , 10 )
111
123
112
124
width , height = f .get_size_inches () * 2.54
113
- assert np .allclose ((width , height ), (10 , 5 ))
125
+ np .testing . assert_allclose ((width , height ), (10 , 5 ), rtol = get_rtol ( f ))
114
126
115
127
# width:height = 1:1
116
128
with subplots_context () as (f , ax ):
@@ -122,7 +134,7 @@ def test_set_map_layout_horz_borders():
122
134
set_map_layout (ax , 10 )
123
135
124
136
width , height = f .get_size_inches () * 2.54
125
- assert np .allclose ((width , height ), (10 , 5 ))
137
+ np .testing . assert_allclose ((width , height ), (10 , 5 ), rtol = get_rtol ( f ))
126
138
127
139
128
140
def test_set_map_layout_two_axes_vert ():
@@ -137,7 +149,7 @@ def test_set_map_layout_two_axes_vert():
137
149
set_map_layout (ax , 10 )
138
150
139
151
width , height = f .get_size_inches () * 2.54
140
- assert np .allclose ((width , height ), (10 , 20 ))
152
+ np .testing . assert_allclose ((width , height ), (10 , 20 ), rtol = get_rtol ( f ))
141
153
142
154
# width:height = 1:1
143
155
with subplots_context (2 , 1 ) as (f , axs ):
@@ -150,7 +162,7 @@ def test_set_map_layout_two_axes_vert():
150
162
set_map_layout (ax , 10 )
151
163
152
164
width , height = f .get_size_inches () * 2.54
153
- assert np .allclose ((width , height ), (10 , 30 ))
165
+ np .testing . assert_allclose ((width , height ), (10 , 30 ), rtol = get_rtol ( f ))
154
166
155
167
156
168
def test_set_map_layout_two_axes_horz ():
@@ -165,7 +177,7 @@ def test_set_map_layout_two_axes_horz():
165
177
set_map_layout (ax , 10 )
166
178
167
179
width , height = f .get_size_inches () * 2.54
168
- assert np .allclose ((width , height ), (10 , 5 ))
180
+ np .testing . assert_allclose ((width , height ), (10 , 5 ), rtol = get_rtol ( f ))
169
181
170
182
# width:height = 1:1
171
183
with subplots_context (1 , 2 ) as (f , axs ):
@@ -178,7 +190,7 @@ def test_set_map_layout_two_axes_horz():
178
190
set_map_layout (ax , 10 )
179
191
180
192
width , height = f .get_size_inches () * 2.54
181
- assert np .allclose ((width , height ), (10 , 10 / 3 ))
193
+ np .testing . assert_allclose ((width , height ), (10 , 10 / 3 ), rtol = get_rtol ( f ))
182
194
183
195
184
196
def test_set_map_layout_nrow_ncol_only_one_raises ():
@@ -201,4 +213,23 @@ def test_set_map_layout_cartopy_2_2():
201
213
result = f .get_size_inches () * 2.54
202
214
expected = (17 , 8.5 )
203
215
204
- np .testing .assert_allclose (result , expected )
216
+ np .testing .assert_allclose (result , expected , rtol = get_rtol (f ))
217
+
218
+
219
+ @pytest .mark .skipif (plt .get_backend ().lower () != "macosx" , reason = "only for macosx" )
220
+ @pytest .mark .parametrize ("dpi" , (100 , 1000 ))
221
+ @pytest .mark .parametrize ("size" , ([17 , 6 ], [10 , 5 ]))
222
+ def test_set_size_inches_macosx (dpi , size ):
223
+
224
+ with figure_context () as f :
225
+
226
+ f .set_dpi (dpi )
227
+
228
+ size = np .array (size )
229
+
230
+ f .set_size_inches (size / 2.54 )
231
+
232
+ result = f .get_size_inches () * 2.54
233
+
234
+ expected_size = np .floor (size / 2.54 * dpi ) / dpi * 2.54
235
+ np .testing .assert_allclose (result , expected_size )
0 commit comments