Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2f33a3

Browse files
committed
Merge pull request matplotlib#3957 from inclement/imagegrid_edge_cbar
BUG : Corrected cax attributes of ImageGrid axes
2 parents fe1a88a + 59ee4bd commit a2f33a3

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,7 @@ def tk_window_focus():
14291429
'matplotlib.sphinxext.tests.test_tinypages',
14301430
'mpl_toolkits.tests.test_mplot3d',
14311431
'mpl_toolkits.tests.test_axes_grid1',
1432+
'mpl_toolkits.tests.test_axes_grid',
14321433
]
14331434

14341435

lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@ def __init__(self, fig,
642642
if self._colorbar_mode == "single":
643643
for ax in self.axes_all:
644644
ax.cax = self.cbar_axes[0]
645+
elif self._colorbar_mode == "edge":
646+
for index, ax in enumerate(self.axes_all):
647+
col, row = self._get_col_row(index)
648+
if self._colorbar_location in ("left", "right"):
649+
ax.cax = self.cbar_axes[row]
650+
else:
651+
ax.cax = self.cbar_axes[col]
645652
else:
646653
for ax, cax in zip(self.axes_all, self.cbar_axes):
647654
ax.cax = cax
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
from matplotlib.testing.decorators import image_comparison
3+
from mpl_toolkits.axes_grid1 import ImageGrid
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
8+
@image_comparison(baseline_images=['imagegrid_cbar_mode'],
9+
extensions=['png'],
10+
remove_text=True)
11+
def test_imagegrid_cbar_mode_edge():
12+
X, Y = np.meshgrid(np.linspace(0, 6, 30), np.linspace(0, 6, 30))
13+
arr = np.sin(X) * np.cos(Y) + 1j*(np.sin(3*Y) * np.cos(Y/2.))
14+
15+
fig = plt.figure(figsize=(18, 9))
16+
17+
positions = (241, 242, 243, 244, 245, 246, 247, 248)
18+
directions = ['row']*4 + ['column']*4
19+
cbar_locations = ['left', 'right', 'top', 'bottom']*2
20+
21+
for position, direction, location in zip(positions,
22+
directions,
23+
cbar_locations):
24+
grid = ImageGrid(fig, position,
25+
nrows_ncols=(2, 2),
26+
direction=direction,
27+
cbar_location=location,
28+
cbar_size='20%',
29+
cbar_mode='edge')
30+
ax1, ax2, ax3, ax4, = grid
31+
32+
im1 = ax1.imshow(arr.real, cmap='spectral')
33+
im2 = ax2.imshow(arr.imag, cmap='hot')
34+
im3 = ax3.imshow(np.abs(arr), cmap='jet')
35+
im4 = ax4.imshow(np.arctan2(arr.imag, arr.real), cmap='hsv')
36+
37+
# Some of these colorbars will be overridden by later ones,
38+
# depending on the direction and cbar_location
39+
ax1.cax.colorbar(im1)
40+
ax2.cax.colorbar(im2)
41+
ax3.cax.colorbar(im3)
42+
ax4.cax.colorbar(im4)

0 commit comments

Comments
 (0)