1""" 2================== 3Demo Edge Colorbar 4================== 5 6This example shows how to use one common colorbar for each row or column 7of an image grid. 8""" 9 10from matplotlib import cbook 11import matplotlib.pyplot as plt 12from mpl_toolkits.axes_grid1 import AxesGrid 13 14 15def get_demo_image(): 16 z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) 17 # z is a numpy array of 15x15 18 return z, (-3, 4, -4, 3) 19 20 21def demo_bottom_cbar(fig): 22 """ 23 A grid of 2x2 images with a colorbar for each column. 24 """ 25 grid = AxesGrid(fig, 121, # similar to subplot(121) 26 nrows_ncols=(2, 2), 27 axes_pad=0.10, 28 share_all=True, 29 label_mode="1", 30 cbar_location="bottom", 31 cbar_mode="edge", 32 cbar_pad=0.25, 33 cbar_size="15%", 34 direction="column" 35 ) 36 37 Z, extent = get_demo_image() 38 cmaps = [plt.get_cmap("autumn"), plt.get_cmap("summer")] 39 for i in range(4): 40 im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2]) 41 if i % 2: 42 grid.cbar_axes[i//2].colorbar(im) 43 44 for cax in grid.cbar_axes: 45 cax.toggle_label(True) 46 cax.axis[cax.orientation].set_label("Bar") 47 48 # This affects all axes as share_all = True. 49 grid.axes_llc.set_xticks([-2, 0, 2]) 50 grid.axes_llc.set_yticks([-2, 0, 2]) 51 52 53def demo_right_cbar(fig): 54 """ 55 A grid of 2x2 images. Each row has its own colorbar. 56 """ 57 grid = AxesGrid(fig, 122, # similar to subplot(122) 58 nrows_ncols=(2, 2), 59 axes_pad=0.10, 60 label_mode="1", 61 share_all=True, 62 cbar_location="right", 63 cbar_mode="edge", 64 cbar_size="7%", 65 cbar_pad="2%", 66 ) 67 Z, extent = get_demo_image() 68 cmaps = [plt.get_cmap("spring"), plt.get_cmap("winter")] 69 for i in range(4): 70 im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2]) 71 if i % 2: 72 grid.cbar_axes[i//2].colorbar(im) 73 74 for cax in grid.cbar_axes: 75 cax.toggle_label(True) 76 cax.axis[cax.orientation].set_label('Foo') 77 78 # This affects all axes because we set share_all = True. 79 grid.axes_llc.set_xticks([-2, 0, 2]) 80 grid.axes_llc.set_yticks([-2, 0, 2]) 81 82 83fig = plt.figure(figsize=(5.5, 2.5)) 84fig.subplots_adjust(left=0.05, right=0.93) 85 86demo_bottom_cbar(fig) 87demo_right_cbar(fig) 88 89plt.show() 90