1010import numpy as np
1111
1212from matplotlib import cbook
13- import matplotlib .colors
1413from mpl_toolkits .axes_grid1 import ImageGrid
1514
1615
@@ -35,16 +34,11 @@ def add_inner_title(ax, title, loc, **kwargs):
3534extent = extent [0 ], extent [1 ]/ 3. , extent [2 ], extent [3 ]
3635
3736# *** Demo 1: colorbar at each axes ***
38- grid = ImageGrid (fig , 211 , # similar to subplot(211)
39- nrows_ncols = (1 , 3 ),
40- axes_pad = 0.05 ,
41- label_mode = "1" ,
42- share_all = True ,
43- cbar_location = "top" ,
44- cbar_mode = "each" ,
45- cbar_size = "7%" ,
46- cbar_pad = "1%" ,
47- )
37+ grid = ImageGrid (
38+ # 211 = at the position of fig.add_subplot(211)
39+ fig , 211 , nrows_ncols = (1 , 3 ), axes_pad = 0.05 , label_mode = "1" , share_all = True ,
40+ cbar_location = "top" , cbar_mode = "each" , cbar_size = "7%" , cbar_pad = "1%" )
41+ grid [0 ].set (xticks = [- 2 , 0 ], yticks = [- 2 , 0 , 2 ])
4842
4943for i , (ax , z ) in enumerate (zip (grid , ZS )):
5044 im = ax .imshow (z , origin = "lower" , extent = extent )
@@ -54,46 +48,22 @@ def add_inner_title(ax, title, loc, **kwargs):
5448 cb .set_ticks ([- 1 , 0 , 1 ])
5549
5650for ax , im_title in zip (grid , ["Image 1" , "Image 2" , "Image 3" ]):
57- t = add_inner_title (ax , im_title , loc = 'lower left' )
58- t .patch .set_alpha (0.5 )
59-
60- for ax , z in zip (grid , ZS ):
61- ax .cax .toggle_label (True )
62-
63- grid [0 ].set_xticks ([- 2 , 0 ])
64- grid [0 ].set_yticks ([- 2 , 0 , 2 ])
51+ add_inner_title (ax , im_title , loc = 'lower left' )
6552
6653# *** Demo 2: shared colorbar ***
67- grid2 = ImageGrid (fig , 212 ,
68- nrows_ncols = (1 , 3 ),
69- axes_pad = 0.05 ,
70- label_mode = "1" ,
71- share_all = True ,
72- cbar_location = "right" ,
73- cbar_mode = "single" ,
74- cbar_size = "10%" ,
75- cbar_pad = 0.05 ,
76- )
77-
78- grid2 [0 ].set_xlabel ("X" )
79- grid2 [0 ].set_ylabel ("Y" )
80-
81- vmax , vmin = np .max (ZS ), np .min (ZS )
82- norm = matplotlib .colors .Normalize (vmax = vmax , vmin = vmin )
54+ grid2 = ImageGrid (
55+ fig , 212 , nrows_ncols = (1 , 3 ), axes_pad = 0.05 , label_mode = "1" , share_all = True ,
56+ cbar_location = "right" , cbar_mode = "single" , cbar_size = "10%" , cbar_pad = 0.05 )
57+ grid2 [0 ].set (xlabel = "X" , ylabel = "Y" , xticks = [- 2 , 0 ], yticks = [- 2 , 0 , 2 ])
8358
59+ clim = (np .min (ZS ), np .max (ZS ))
8460for ax , z in zip (grid2 , ZS ):
85- im = ax .imshow (z , norm = norm , origin = "lower" , extent = extent )
61+ im = ax .imshow (z , clim = clim , origin = "lower" , extent = extent )
8662
8763# With cbar_mode="single", cax attribute of all axes are identical.
8864ax .cax .colorbar (im )
89- ax .cax .toggle_label (True )
9065
9166for ax , im_title in zip (grid2 , ["(a)" , "(b)" , "(c)" ]):
92- t = add_inner_title (ax , im_title , loc = 'upper left' )
93- t .patch .set_ec ("none" )
94- t .patch .set_alpha (0.5 )
95-
96- grid2 [0 ].set_xticks ([- 2 , 0 ])
97- grid2 [0 ].set_yticks ([- 2 , 0 , 2 ])
67+ add_inner_title (ax , im_title , loc = 'upper left' )
9868
9969plt .show ()
0 commit comments