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 #simple plotting with different labels
  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 #explicit specifications of styles
  38 
  39 #... the dirty way
  40 close('all')
  41 plot(x,sin(x),'r--')
  42 plot(x,cos(x),'go-.')
  43 title('quick & dirty')
  44 
  45 figure()
  46 #... the "clean" way
  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) #<<< subplot
  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 import matplotlib.gridspec as gridspec
 108 '''
 109 
 110 #TODO
 111 
 112 
 113 ########################################################################
 114 # 2D plotting
 115 ########################################################################
 116 close('all')
 117 fname='/home/m300028/shared/data/SEP/data_sources/LSMASK/jsbach_T63_GR15_4tiles_1992.nc'
 118 F=Nio.open_file(fname,'r')
 119 z=F.variables['elevation'].get_value()
 120 
 121 #... again dirty
 122 figure()
 123 imshow(z)
 124 colorbar() #shrink=0.5
 125 
 126 #... better
 127 f=figure()
 128 ax=f.add_subplot(111)
 129 im=ax.imshow(z)
 130 colorbar(im,ax=ax,shrink=0.5)
 131 
 132 #... changing colorbar
 133 figure()
 134 imshow(z,cmap='RdBu')
 135 colorbar(shrink=0.75,orientation='horizontal')
 136 
 137 
 138 mycmap = cm.get_cmap('RdBu_r', 11)
 139 
 140 figure()
 141 imshow(z,cmap=mycmap)
 142 colorbar(shrink=0.75,orientation='horizontal')
 143 
 144 
 145 
 146 
 147 #--- another way to get a nice colorbar ---
 148 def add_nice_legend(ax,im,cmap,cticks=None):
 149     '''
 150     add a nice looking legend
 151 
 152     @param ax: major axis with plot
 153     @type ax: matpltlib axis object
 154 
 155     @param im: result from command like imshow
 156     @param im: matplotlib im object (???)
 157 
 158     #http://old.nabble.com/manual-placement-of-a-colorbar-td28112662.html
 159 
 160 
 161 
 162     '''
 163     from mpl_toolkits.axes_grid import make_axes_locatable
 164     import  matplotlib.axes as maxes
 165 
 166     #set legend aligned with plot (nice looking)
 167     divider = make_axes_locatable(ax)
 168     cax = divider.new_horizontal("5%", pad=0.05, axes_class=maxes.Axes)
 169     ax.figure.add_axes(cax)
 170     norm = mpl.colors.Normalize(vmin=im.get_clim()[0], vmax=im.get_clim()[1])
 171     cb   = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm,ticks=cticks)
 172 
 173 
 174 
 175 close('all')
 176 
 177 f=figure(); ax=f.add_subplot(111)
 178 im=ax.imshow(z,cmap=mycmap)
 179 add_nice_legend(ax,im,mycmap)
 180 
 181 #----
 182 
 183 stop
 184 
 185 
 186 
 187 
 188 
 189 '''
 190 contourplots are generated with
 191 
 192 contour() or contourf() function
 193 '''
 194 
 195 
 196 '''
 197 saving a figure ...
 198 '''
 199 f=figure(); ax=f.add_subplot(111)
 200 ax.imshow(z); ax.set_title('global topography [m]')
 201 f.savefig('elevation1.png')
 202 f.savefig('elevation1.pdf')
 203 f.savefig('elevation2.pdf',bbox_inches='tight')
 204 
 205 
 206 '''
 207 ... one of my favorites
 208 '''
 209 close('all')
 210 f=figure(); ax1=f.add_subplot(211); ax2=f.add_subplot(212)
 211 ax1.fill_between(x,sin(x))
 212 ax2.fill_between(x,sin(x),y2=cos(x),color='red')