1+ from contextlib import ExitStack
12
2- import matplotlib
3- from matplotlib .testing .decorators import image_comparison
4- from mpl_toolkits .axes_grid1 import ImageGrid
53import numpy as np
4+ import pytest
5+
6+ import matplotlib as mpl
7+ from matplotlib .testing .decorators import image_comparison
68import matplotlib .pyplot as plt
9+ from mpl_toolkits .axes_grid1 import ImageGrid
710
811
9- @image_comparison (['imagegrid_cbar_mode.png' ], remove_text = True , style = 'mpl20' )
10- def test_imagegrid_cbar_mode_edge ():
11- matplotlib .rcParams ['image.interpolation' ] = 'nearest'
12+ # The original version of this test relied on mpl_toolkits's slightly different
13+ # colorbar implementation; moving to matplotlib's own colorbar implementation
14+ # caused the small image comparison error.
15+ @pytest .mark .parametrize ("legacy_colorbar" , [False , True ])
16+ @image_comparison (['imagegrid_cbar_mode.png' ],
17+ remove_text = True , style = 'mpl20' , tol = 0.3 )
18+ def test_imagegrid_cbar_mode_edge (legacy_colorbar ):
19+ mpl .rcParams ["mpl_toolkits.legacy_colorbar" ] = legacy_colorbar
1220
1321 X , Y = np .meshgrid (np .linspace (0 , 6 , 30 ), np .linspace (0 , 6 , 30 ))
1422 arr = np .sin (X ) * np .cos (Y ) + 1j * (np .sin (3 * Y ) * np .cos (Y / 2. ))
@@ -19,9 +27,8 @@ def test_imagegrid_cbar_mode_edge():
1927 directions = ['row' ]* 4 + ['column' ]* 4
2028 cbar_locations = ['left' , 'right' , 'top' , 'bottom' ]* 2
2129
22- for position , direction , location in zip (positions ,
23- directions ,
24- cbar_locations ):
30+ for position , direction , location in zip (
31+ positions , directions , cbar_locations ):
2532 grid = ImageGrid (fig , position ,
2633 nrows_ncols = (2 , 2 ),
2734 direction = direction ,
@@ -30,14 +37,15 @@ def test_imagegrid_cbar_mode_edge():
3037 cbar_mode = 'edge' )
3138 ax1 , ax2 , ax3 , ax4 , = grid
3239
33- im1 = ax1 .imshow (arr .real , cmap = 'nipy_spectral' )
34- im2 = ax2 .imshow (arr .imag , cmap = 'hot' )
35- im3 = ax3 .imshow (np .abs (arr ), cmap = 'jet' )
36- im4 = ax4 .imshow (np .arctan2 (arr .imag , arr .real ), cmap = 'hsv' )
37-
38- # Some of these colorbars will be overridden by later ones,
39- # depending on the direction and cbar_location
40- ax1 .cax .colorbar (im1 )
41- ax2 .cax .colorbar (im2 )
42- ax3 .cax .colorbar (im3 )
43- ax4 .cax .colorbar (im4 )
40+ ax1 .imshow (arr .real , cmap = 'nipy_spectral' )
41+ ax2 .imshow (arr .imag , cmap = 'hot' )
42+ ax3 .imshow (np .abs (arr ), cmap = 'jet' )
43+ ax4 .imshow (np .arctan2 (arr .imag , arr .real ), cmap = 'hsv' )
44+
45+ with (pytest .warns (mpl .MatplotlibDeprecationWarning ) if legacy_colorbar
46+ else ExitStack ()):
47+ # In each row/column, the "first" colorbars must be overwritten by
48+ # the "second" ones. To achieve this, clear out the axes first.
49+ for ax in grid :
50+ ax .cax .cla ()
51+ ax .cax .colorbar (ax .images [0 ])
0 commit comments