5
5
6
6
Grid of images with shared xaxis and yaxis.
7
7
"""
8
+ import numpy as np
9
+
8
10
import matplotlib .pyplot as plt
9
11
from mpl_toolkits .axes_grid1 import ImageGrid
10
- import numpy as np
12
+ import matplotlib . colors
11
13
12
14
13
15
def get_demo_image ():
@@ -30,90 +32,86 @@ def add_inner_title(ax, title, loc, size=None, **kwargs):
30
32
at .txt ._text .set_path_effects ([withStroke (foreground = "w" , linewidth = 3 )])
31
33
return at
32
34
33
- if 1 :
34
- F = plt .figure (1 , (6 , 6 ))
35
- F .clf ()
36
-
37
- # prepare images
38
- Z , extent = get_demo_image ()
39
- ZS = [Z [i ::3 , :] for i in range (3 )]
40
- extent = extent [0 ], extent [1 ]/ 3. , extent [2 ], extent [3 ]
41
-
42
- # demo 1 : colorbar at each axes
43
-
44
- grid = ImageGrid (F , 211 , # similar to subplot(111)
45
- nrows_ncols = (1 , 3 ),
46
- direction = "row" ,
47
- axes_pad = 0.05 ,
48
- add_all = True ,
49
- label_mode = "1" ,
50
- share_all = True ,
51
- cbar_location = "top" ,
52
- cbar_mode = "each" ,
53
- cbar_size = "7%" ,
54
- cbar_pad = "1%" ,
55
- )
56
-
57
- for ax , z in zip (grid , ZS ):
58
- im = ax .imshow (
59
- z , origin = "lower" , extent = extent , interpolation = "nearest" )
60
- ax .cax .colorbar (im )
61
-
62
- for ax , im_title in zip (grid , ["Image 1" , "Image 2" , "Image 3" ]):
63
- t = add_inner_title (ax , im_title , loc = 'lower left' )
64
- t .patch .set_alpha (0.5 )
65
-
66
- for ax , z in zip (grid , ZS ):
67
- ax .cax .toggle_label (True )
68
- #axis = ax.cax.axis[ax.cax.orientation]
69
- #axis.label.set_text("counts s$^{-1}$")
70
- #axis.label.set_size(10)
71
- #axis.major_ticklabels.set_size(6)
72
-
73
- # changing the colorbar ticks
74
- grid [1 ].cax .set_xticks ([- 1 , 0 , 1 ])
75
- grid [2 ].cax .set_xticks ([- 1 , 0 , 1 ])
76
-
77
- grid [0 ].set_xticks ([- 2 , 0 ])
78
- grid [0 ].set_yticks ([- 2 , 0 , 2 ])
79
-
80
- # demo 2 : shared colorbar
81
-
82
- grid2 = ImageGrid (F , 212 ,
83
- nrows_ncols = (1 , 3 ),
84
- direction = "row" ,
85
- axes_pad = 0.05 ,
86
- add_all = True ,
87
- label_mode = "1" ,
88
- share_all = True ,
89
- cbar_location = "right" ,
90
- cbar_mode = "single" ,
91
- cbar_size = "10%" ,
92
- cbar_pad = 0.05 ,
93
- )
94
-
95
- grid2 [0 ].set_xlabel ("X" )
96
- grid2 [0 ].set_ylabel ("Y" )
97
-
98
- vmax , vmin = np .max (ZS ), np .min (ZS )
99
- import matplotlib .colors
100
- norm = matplotlib .colors .Normalize (vmax = vmax , vmin = vmin )
101
-
102
- for ax , z in zip (grid2 , ZS ):
103
- im = ax .imshow (z , norm = norm ,
104
- origin = "lower" , extent = extent ,
105
- interpolation = "nearest" )
106
-
107
- # With cbar_mode="single", cax attribute of all axes are identical.
108
- ax .cax .colorbar (im )
109
- ax .cax .toggle_label (True )
110
35
111
- for ax , im_title in zip (grid2 , ["(a)" , "(b)" , "(c)" ]):
112
- t = add_inner_title (ax , im_title , loc = 'upper left' )
113
- t .patch .set_ec ("none" )
114
- t .patch .set_alpha (0.5 )
36
+ fig = plt .figure (figsize = (6 , 6 ))
37
+
38
+ # Prepare images
39
+ Z , extent = get_demo_image ()
40
+ ZS = [Z [i ::3 , :] for i in range (3 )]
41
+ extent = extent [0 ], extent [1 ]/ 3. , extent [2 ], extent [3 ]
42
+
43
+ # *** Demo 1: colorbar at each axes ***
44
+ grid = ImageGrid (fig , 211 , # similar to subplot(211)
45
+ nrows_ncols = (1 , 3 ),
46
+ direction = "row" ,
47
+ axes_pad = 0.05 ,
48
+ add_all = True ,
49
+ label_mode = "1" ,
50
+ share_all = True ,
51
+ cbar_location = "top" ,
52
+ cbar_mode = "each" ,
53
+ cbar_size = "7%" ,
54
+ cbar_pad = "1%" ,
55
+ )
56
+
57
+ for ax , z in zip (grid , ZS ):
58
+ im = ax .imshow (
59
+ z , origin = "lower" , extent = extent , interpolation = "nearest" )
60
+ ax .cax .colorbar (im )
115
61
116
- grid2 [0 ].set_xticks ([- 2 , 0 ])
117
- grid2 [0 ].set_yticks ([- 2 , 0 , 2 ])
62
+ for ax , im_title in zip (grid , ["Image 1" , "Image 2" , "Image 3" ]):
63
+ t = add_inner_title (ax , im_title , loc = 'lower left' )
64
+ t .patch .set_alpha (0.5 )
118
65
119
- plt .show ()
66
+ for ax , z in zip (grid , ZS ):
67
+ ax .cax .toggle_label (True )
68
+ #axis = ax.cax.axis[ax.cax.orientation]
69
+ #axis.label.set_text("counts s$^{-1}$")
70
+ #axis.label.set_size(10)
71
+ #axis.major_ticklabels.set_size(6)
72
+
73
+ # Changing the colorbar ticks
74
+ grid [1 ].cax .set_xticks ([- 1 , 0 , 1 ])
75
+ grid [2 ].cax .set_xticks ([- 1 , 0 , 1 ])
76
+
77
+ grid [0 ].set_xticks ([- 2 , 0 ])
78
+ grid [0 ].set_yticks ([- 2 , 0 , 2 ])
79
+
80
+ # *** Demo 2: shared colorbar ***
81
+ grid2 = ImageGrid (fig , 212 ,
82
+ nrows_ncols = (1 , 3 ),
83
+ direction = "row" ,
84
+ axes_pad = 0.05 ,
85
+ add_all = True ,
86
+ label_mode = "1" ,
87
+ share_all = True ,
88
+ cbar_location = "right" ,
89
+ cbar_mode = "single" ,
90
+ cbar_size = "10%" ,
91
+ cbar_pad = 0.05 ,
92
+ )
93
+
94
+ grid2 [0 ].set_xlabel ("X" )
95
+ grid2 [0 ].set_ylabel ("Y" )
96
+
97
+ vmax , vmin = np .max (ZS ), np .min (ZS )
98
+ norm = matplotlib .colors .Normalize (vmax = vmax , vmin = vmin )
99
+
100
+ for ax , z in zip (grid2 , ZS ):
101
+ im = ax .imshow (z , norm = norm ,
102
+ origin = "lower" , extent = extent ,
103
+ interpolation = "nearest" )
104
+
105
+ # With cbar_mode="single", cax attribute of all axes are identical.
106
+ ax .cax .colorbar (im )
107
+ ax .cax .toggle_label (True )
108
+
109
+ for ax , im_title in zip (grid2 , ["(a)" , "(b)" , "(c)" ]):
110
+ t = add_inner_title (ax , im_title , loc = 'upper left' )
111
+ t .patch .set_ec ("none" )
112
+ t .patch .set_alpha (0.5 )
113
+
114
+ grid2 [0 ].set_xticks ([- 2 , 0 ])
115
+ grid2 [0 ].set_yticks ([- 2 , 0 , 2 ])
116
+
117
+ plt .show ()
0 commit comments