1 from pylab import *
2 import Nio
3
4 '''
5 a remark in advance. When importing a module, best practice is actually
6 to import always into a variable
7
8 e.g. import numpy as np
9 from matplotlib import pylab as pl
10 ...
11
12 reason: some functions are available in different modules with different
13 implementation. If you want to be sure that you work with the same
14 routine always, then you can e.g. call np.cos() for the cosine function
15 and you know where it comes from.
16 '''
17
18
19 '''
20 Best start for plotting is to always look for examples
21 in the matplotlib gallery: http://matplotlib.sourceforge.net/gallery.html
22 '''
23 close('all')
24
25
26 x=arange(100)/100. *6*pi
27
28 plot(x,sin(x))
29 plot(x,cos(x))
30
31 plot(x,sin(x),label='test1')
32 plot(x,cos(x),label='test2')
33 legend()
34
35
36
37
38
39
40 close('all')
41 plot(x,sin(x),'r--')
42 plot(x,cos(x),'go-.')
43 title('quick & dirty')
44
45 figure()
46
47 plot(x,sin(x),color='red',linestyle='--')
48 plot(x,cos(x),color='green',linestyle='-.',marker='o')
49 title('the cleaner way')
50 xlabel('x-label here')
51 ylabel('y-label here')
52
53
54
55 '''
56 It is recommended to NOT follow the previous examples, but to use always
57 figure and axis objects to work with, as this always allows allows a
58 proper handling of different axes. This is of particular importance
59 when working with multiple axes or subplots
60 '''
61
62 f=figure(figsize=(12,3))
63 ax1 = f.add_subplot(111)
64
65 ax1.plot(x,sin(x),'r')
66 ax2=twinx()
67 ax2.plot(x,100*cos(x),'g')
68 ax1.set_ylabel('This is the one label')
69 ax2.set_ylabel('This is the other label')
70
71
72
73 '''
74 for interactive working, this also helps a lot as it allows
75 to synchronize different axes
76
77 try the sharex option!
78 '''
79 close('all')
80 f = figure()
81 ax1=f.add_subplot(1,2,1)
82 ax2=f.add_subplot(1,2,2,sharex=ax1)
83
84 ax1.plot(x,sin(x) )
85 ax2.plot(x,cos(x) )
86 ax1.set_title('Axis synchronization rocks ...')
87
88
89
90 '''
91 cheating with subplots
92 '''
93 close('all')
94 f = figure()
95 ax1 = f.add_subplot(221)
96 ax2 = f.add_subplot(222)
97 ax3 = f.add_subplot(212)
98
99 ax1.plot(x,sin(x))
100 ax2.plot(x,sin(x))
101 ax3.plot(x,sin(x)); ax3.plot(x,cos(x))
102
103 '''
104 a more sophisticated approach to handle subplots
105 is provided by gridspec
106
107 for more detailed documentation see http://matplotlib.sourceforge.net/users/gridspec.html
108 '''
109 import matplotlib.gridspec as gs
110
111 f= figure()
112 grid = gs.GridSpec(2, 1, height_ratios=[3,1])
113
114 ax1 = f.add_subplot(grid[0])
115 ax2 = f.add_subplot(grid[1])
116 ax1.plot(x,sin(x))
117 ax2.plot(x,x**2)
118
119
120 stop
121
122
123
124
125
126
127 close('all')
128 fname='/home/m300028/shared/data/SEP/data_sources/LSMASK/jsbach_T63_GR15_4tiles_1992.nc'
129 F=Nio.open_file(fname,'r')
130 z=F.variables['elevation'].get_value()
131
132
133 figure()
134 imshow(z)
135 colorbar()
136
137
138 f=figure()
139 ax=f.add_subplot(111)
140 im=ax.imshow(z)
141 colorbar(im,ax=ax,shrink=0.5)
142
143
144 figure()
145 imshow(z,cmap='RdBu')
146 colorbar(shrink=0.75,orientation='horizontal')
147
148
149 mycmap = cm.get_cmap('RdBu_r', 11)
150
151 figure()
152 imshow(z,cmap=mycmap)
153 colorbar(shrink=0.75,orientation='horizontal')
154
155
156
157
158
159 def add_nice_legend(ax,im,cmap,cticks=None):
160 '''
161 add a nice looking legend
162
163 @param ax: major axis with plot
164 @type ax: matpltlib axis object
165
166 @param im: result from command like imshow
167 @param im: matplotlib im object (???)
168
169 #http://old.nabble.com/manual-placement-of-a-colorbar-td28112662.html
170
171
172
173 '''
174 from mpl_toolkits.axes_grid import make_axes_locatable
175 import matplotlib.axes as maxes
176
177
178 divider = make_axes_locatable(ax)
179 cax = divider.new_horizontal("5%", pad=0.05, axes_class=maxes.Axes)
180 ax.figure.add_axes(cax)
181 norm = mpl.colors.Normalize(vmin=im.get_clim()[0], vmax=im.get_clim()[1])
182 cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm,ticks=cticks)
183
184
185
186 close('all')
187
188 f=figure(); ax=f.add_subplot(111)
189 im=ax.imshow(z,cmap=mycmap)
190 add_nice_legend(ax,im,mycmap)
191
192
193
194 stop
195
196
197
198
199
200 '''
201 contourplots are generated with
202
203 contour() or contourf() function
204 '''
205
206
207 '''
208 saving a figure ...
209 '''
210 f=figure(); ax=f.add_subplot(111)
211 ax.imshow(z); ax.set_title('global topography [m]')
212 f.savefig('elevation1.png')
213 f.savefig('elevation1.pdf')
214 f.savefig('elevation2.pdf',bbox_inches='tight')
215
216
217 '''
218 ... one of my favorites
219 '''
220 close('all')
221 f=figure(); ax1=f.add_subplot(211); ax2=f.add_subplot(212)
222 ax1.fill_between(x,sin(x))
223 ax2.fill_between(x,sin(x),y2=cos(x),color='red')