8
8
from matplotlib .testing .decorators import image_comparison
9
9
import matplotlib .pyplot as plt
10
10
import matplotlib .transforms as mtransforms
11
+ import matplotlib .patches as mpatches
11
12
from matplotlib import gridspec , ticker
12
13
13
14
15
+ def fake_ylabel (ax , * , right = False , fontsize = 12 ):
16
+ """
17
+ Fake ylabel to test if the label is set correctly.
18
+ """
19
+ fig = ax .figure
20
+ width = fontsize / 72 * 1.2
21
+ height = fontsize / 72 * 4
22
+ trans = (fig .dpi_scale_trans +
23
+ mtransforms .ScaledTranslation (0 , 0.5 , ax .transAxes ) +
24
+ mtransforms .ScaledTranslation (- 2 * width , - height / 2 ,
25
+ ax .figure .dpi_scale_trans ))
26
+ rect = mpatches .Rectangle ((0 , 0 ), width , height , transform = trans ,
27
+ color = 'red' , alpha = 0.5 , clip_on = False )
28
+ ax .add_patch (rect )
29
+
30
+
31
+ def fake_xlabel (ax , * , right = False , fontsize = 12 ):
32
+ """
33
+ Fake xlabel to test if the label is set correctly.
34
+ """
35
+ fig = ax .figure
36
+ height = fontsize / 72 * 1.2
37
+ width = fontsize / 72 * 4
38
+ trans = (fig .dpi_scale_trans +
39
+ mtransforms .ScaledTranslation (0.5 , 0 , ax .transAxes ) +
40
+ mtransforms .ScaledTranslation (- width / 2 , - 2 * height ,
41
+ ax .figure .dpi_scale_trans ))
42
+ rect = mpatches .Rectangle ((0 , 0 ), width , height , transform = trans ,
43
+ color = 'red' , alpha = 0.5 , clip_on = False )
44
+ ax .add_patch (rect )
45
+
46
+
47
+ def fake_title (ax , * , right = False , fontsize = 12 ):
48
+ """
49
+ Fake xlabel to test if the label is set correctly.
50
+ """
51
+ fig = ax .figure
52
+ height = fontsize / 72 * 1.2
53
+ width = fontsize / 72 * 4
54
+ # Create rectangle
55
+ trans = (fig .dpi_scale_trans +
56
+ mtransforms .ScaledTranslation (0.5 , 1.0 , ax .transAxes ) +
57
+ mtransforms .ScaledTranslation (- width / 2 , 0.2 * height ,
58
+ ax .figure .dpi_scale_trans ))
59
+
60
+ rect = mpatches .Rectangle ((0 , 0 ), width , height , transform = trans ,
61
+ color = 'blue' , alpha = 0.5 , clip_on = False )
62
+ ax .add_patch (rect )
63
+
64
+
14
65
def example_plot (ax , fontsize = 12 , nodec = False ):
15
66
ax .plot ([1 , 2 ])
16
67
ax .locator_params (nbins = 3 )
17
68
if not nodec :
18
- ax .set_xlabel ('x-label' , fontsize = fontsize )
19
- ax .set_ylabel ('y-label' , fontsize = fontsize )
20
- ax .set_title ('Title' , fontsize = fontsize )
69
+ fake_ylabel (ax , fontsize = fontsize )
70
+ fake_xlabel (ax , fontsize = fontsize )
71
+ fake_title (ax , fontsize = fontsize )
72
+ # fake_xticks(ax, fontsize=10)
73
+ ax .set_xticklabels ([])
74
+ ax .set_yticklabels ([])
21
75
else :
22
76
ax .set_xticklabels ([])
23
77
ax .set_yticklabels ([])
24
78
25
79
26
- def example_pcolor (ax , fontsize = 12 ):
80
+ def example_pcolor (ax , xlabel = True , ylabel = True , title = True , fontsize = 12 ):
27
81
dx , dy = 0.6 , 0.6
28
82
y , x = np .mgrid [slice (- 3 , 3 + dy , dy ),
29
83
slice (- 3 , 3 + dx , dx )]
30
84
z = (1 - x / 2. + x ** 5 + y ** 3 ) * np .exp (- x ** 2 - y ** 2 )
31
85
pcm = ax .pcolormesh (x , y , z [:- 1 , :- 1 ], cmap = 'RdBu_r' , vmin = - 1. , vmax = 1. ,
32
86
rasterized = True )
33
- ax .set_xlabel ('x-label' , fontsize = fontsize )
34
- ax .set_ylabel ('y-label' , fontsize = fontsize )
35
- ax .set_title ('Title' , fontsize = fontsize )
87
+ if xlabel :
88
+ fake_xlabel (ax , fontsize = fontsize )
89
+ if ylabel :
90
+ fake_ylabel (ax , fontsize = fontsize )
91
+ if title :
92
+ fake_title (ax , fontsize = fontsize )
93
+ ax .set_xticklabels ([])
94
+ ax .set_yticklabels ([])
36
95
return pcm
37
96
38
97
@@ -52,7 +111,7 @@ def test_constrained_layout2():
52
111
example_plot (ax , fontsize = 24 )
53
112
54
113
55
- @image_comparison (['constrained_layout3.png' ])
114
+ @image_comparison (['constrained_layout3.png' ], remove_text = True )
56
115
def test_constrained_layout3 ():
57
116
"""Test constrained_layout for colorbars with subplots"""
58
117
@@ -66,7 +125,7 @@ def test_constrained_layout3():
66
125
fig .colorbar (pcm , ax = ax , pad = pad )
67
126
68
127
69
- @image_comparison (['constrained_layout4.png' ])
128
+ @image_comparison (['constrained_layout4.png' ], remove_text = True )
70
129
def test_constrained_layout4 ():
71
130
"""Test constrained_layout for a single colorbar with subplots"""
72
131
@@ -76,7 +135,7 @@ def test_constrained_layout4():
76
135
fig .colorbar (pcm , ax = axs , pad = 0.01 , shrink = 0.6 )
77
136
78
137
79
- @image_comparison (['constrained_layout5.png' ], tol = 0.002 )
138
+ @image_comparison (['constrained_layout5.png' ], tol = 0.002 , remove_text = True )
80
139
def test_constrained_layout5 ():
81
140
"""
82
141
Test constrained_layout for a single colorbar with subplots,
@@ -91,7 +150,7 @@ def test_constrained_layout5():
91
150
location = 'bottom' )
92
151
93
152
94
- @image_comparison (['constrained_layout6.png' ], tol = 0.002 )
153
+ @image_comparison (['constrained_layout6.png' ], tol = 0.002 , remove_text = True )
95
154
def test_constrained_layout6 ():
96
155
"""Test constrained_layout for nested gridspecs"""
97
156
# Remove this line when this test image is regenerated.
@@ -106,7 +165,6 @@ def test_constrained_layout6():
106
165
ax = fig .add_subplot (gs )
107
166
axsl += [ax ]
108
167
example_plot (ax , fontsize = 12 )
109
- ax .set_xlabel ('x-label\n MultiLine' )
110
168
axsr = []
111
169
for gs in gsr :
112
170
ax = fig .add_subplot (gs )
@@ -154,7 +212,7 @@ def test_constrained_layout7():
154
212
fig .draw_without_rendering ()
155
213
156
214
157
- @image_comparison (['constrained_layout8.png' ])
215
+ @image_comparison (['constrained_layout8.png' ], remove_text = True )
158
216
def test_constrained_layout8 ():
159
217
"""Test for gridspecs that are not completely full"""
160
218
@@ -189,9 +247,7 @@ def test_constrained_layout9():
189
247
fig , axs = plt .subplots (2 , 2 , layout = "constrained" ,
190
248
sharex = False , sharey = False )
191
249
for ax in axs .flat :
192
- pcm = example_pcolor (ax , fontsize = 24 )
193
- ax .set_xlabel ('' )
194
- ax .set_ylabel ('' )
250
+ pcm = example_pcolor (ax , fontsize = 24 , xlabel = False , ylabel = False )
195
251
ax .set_aspect (2. )
196
252
fig .colorbar (pcm , ax = axs , pad = 0.01 , shrink = 0.6 )
197
253
fig .suptitle ('Test Suptitle' , fontsize = 28 )
@@ -207,7 +263,7 @@ def test_constrained_layout10():
207
263
ax .legend (loc = 'center left' , bbox_to_anchor = (0.8 , 0.5 ))
208
264
209
265
210
- @image_comparison (['constrained_layout11.png' ])
266
+ @image_comparison (['constrained_layout11.png' ], remove_text = True )
211
267
def test_constrained_layout11 ():
212
268
"""Test for multiple nested gridspecs"""
213
269
@@ -227,7 +283,7 @@ def test_constrained_layout11():
227
283
example_plot (ax , fontsize = 9 )
228
284
229
285
230
- @image_comparison (['constrained_layout11rat.png' ])
286
+ @image_comparison (['constrained_layout11rat.png' ], remove_text = True )
231
287
def test_constrained_layout11rat ():
232
288
"""Test for multiple nested gridspecs with width_ratios"""
233
289
@@ -266,10 +322,10 @@ def test_constrained_layout12():
266
322
example_plot (ax , nodec = True )
267
323
ax = fig .add_subplot (gs0 [4 :, 0 ])
268
324
example_plot (ax , nodec = True )
269
- ax . set_xlabel ( 'x-label' )
325
+ fake_xlabel ( ax )
270
326
271
327
272
- @image_comparison (['constrained_layout13.png' ], tol = 2.e-2 )
328
+ @image_comparison (['constrained_layout13.png' ], tol = 2.e-2 , remove_text = True )
273
329
def test_constrained_layout13 ():
274
330
"""Test that padding works."""
275
331
fig , axs = plt .subplots (2 , 2 , layout = "constrained" )
@@ -281,7 +337,7 @@ def test_constrained_layout13():
281
337
fig .get_layout_engine ().set (w_pad = 24. / 72. , h_pad = 24. / 72. )
282
338
283
339
284
- @image_comparison (['constrained_layout14.png' ])
340
+ @image_comparison (['constrained_layout14.png' ], remove_text = True )
285
341
def test_constrained_layout14 ():
286
342
"""Test that padding works."""
287
343
fig , axs = plt .subplots (2 , 2 , layout = "constrained" )
@@ -302,7 +358,7 @@ def test_constrained_layout15():
302
358
example_plot (ax , fontsize = 12 )
303
359
304
360
305
- @image_comparison (['constrained_layout16.png' ])
361
+ @image_comparison (['constrained_layout16.png' ], remove_text = True )
306
362
def test_constrained_layout16 ():
307
363
"""Test ax.set_position."""
308
364
fig , ax = plt .subplots (layout = "constrained" )
@@ -414,9 +470,7 @@ def test_colorbar_location():
414
470
415
471
fig , axs = plt .subplots (4 , 5 , layout = "constrained" )
416
472
for ax in axs .flat :
417
- pcm = example_pcolor (ax )
418
- ax .set_xlabel ('' )
419
- ax .set_ylabel ('' )
473
+ pcm = example_pcolor (ax , xlabel = False , ylabel = False , title = False )
420
474
fig .colorbar (pcm , ax = axs [:, 1 ], shrink = 0.4 )
421
475
fig .colorbar (pcm , ax = axs [- 1 , :2 ], shrink = 0.5 , location = 'bottom' )
422
476
fig .colorbar (pcm , ax = axs [0 , 2 :], shrink = 0.5 , location = 'bottom' , pad = 0.05 )
@@ -470,7 +524,7 @@ def test_colorbar_align():
470
524
cbs [3 ].ax .get_position ().y0 )
471
525
472
526
473
- @image_comparison (['test_colorbars_no_overlapV.png' ], style = 'mpl20' )
527
+ @image_comparison (['test_colorbars_no_overlapV.png' ], style = 'mpl20' , remove_text = True )
474
528
def test_colorbars_no_overlapV ():
475
529
fig = plt .figure (figsize = (2 , 4 ), layout = "constrained" )
476
530
axs = fig .subplots (2 , 1 , sharex = True , sharey = True )
@@ -479,13 +533,11 @@ def test_colorbars_no_overlapV():
479
533
ax .tick_params (axis = 'both' , direction = 'in' )
480
534
im = ax .imshow ([[1 , 2 ], [3 , 4 ]])
481
535
fig .colorbar (im , ax = ax , orientation = "vertical" )
482
- fig .suptitle ("foo" )
483
536
484
537
485
- @image_comparison (['test_colorbars_no_overlapH.png' ], style = 'mpl20' )
538
+ @image_comparison (['test_colorbars_no_overlapH.png' ], style = 'mpl20' , remove_text = True )
486
539
def test_colorbars_no_overlapH ():
487
540
fig = plt .figure (figsize = (4 , 2 ), layout = "constrained" )
488
- fig .suptitle ("foo" )
489
541
axs = fig .subplots (1 , 2 , sharex = True , sharey = True )
490
542
for ax in axs :
491
543
ax .yaxis .set_major_formatter (ticker .NullFormatter ())
0 commit comments