{{{#!python from pylab import * import Nio ''' a remark in advance. When importing a module, best practice is actually to import always into a variable e.g. import numpy as np from matplotlib import pylab as pl ... reason: some functions are available in different modules with different implementation. If you want to be sure that you work with the same routine always, then you can e.g. call np.cos() for the cosine function and you know where it comes from. ''' ''' Best start for plotting is to always look for examples in the matplotlib gallery: http://matplotlib.sourceforge.net/gallery.html ''' close('all') #simple plotting with different labels x=arange(100)/100. *6*pi plot(x,sin(x)) plot(x,cos(x)) plot(x,sin(x),label='test1') plot(x,cos(x),label='test2') legend() #explicit specifications of styles #... the dirty way close('all') plot(x,sin(x),'r--') plot(x,cos(x),'go-.') title('quick & dirty') figure() #... the "clean" way plot(x,sin(x),color='red',linestyle='--') plot(x,cos(x),color='green',linestyle='-.',marker='o') title('the cleaner way') xlabel('x-label here') ylabel('y-label here') ######################################################################## ''' It is recommended to NOT follow the previous examples, but to use always figure and axis objects to work with, as this always allows allows a proper handling of different axes. This is of particular importance when working with multiple axes or subplots ''' f=figure(figsize=(12,3)) ax1 = f.add_subplot(111) #<<< subplot ax1.plot(x,sin(x),'r') ax2=twinx() ax2.plot(x,100*cos(x),'g') ax1.set_ylabel('This is the one label') ax2.set_ylabel('This is the other label') ''' for interactive working, this also helps a lot as it allows to synchronize different axes try the sharex option! ''' close('all') f = figure() ax1=f.add_subplot(1,2,1) ax2=f.add_subplot(1,2,2,sharex=ax1) ax1.plot(x,sin(x) ) ax2.plot(x,cos(x) ) ax1.set_title('Axis synchronization rocks ...') ''' cheating with subplots ''' close('all') f = figure() ax1 = f.add_subplot(221) ax2 = f.add_subplot(222) ax3 = f.add_subplot(212) ax1.plot(x,sin(x)) ax2.plot(x,sin(x)) ax3.plot(x,sin(x)); ax3.plot(x,cos(x)) ''' a more sophisticated approach to handle subplots is provided by gridspec for more detailed documentation see http://matplotlib.sourceforge.net/users/gridspec.html ''' import matplotlib.gridspec as gs f= figure() grid = gs.GridSpec(2, 1, height_ratios=[3,1]) ax1 = f.add_subplot(grid[0]) ax2 = f.add_subplot(grid[1]) ax1.plot(x,sin(x)) ax2.plot(x,x**2) stop ######################################################################## # 2D plotting ######################################################################## close('all') fname='/home/m300028/shared/data/SEP/data_sources/LSMASK/jsbach_T63_GR15_4tiles_1992.nc' F=Nio.open_file(fname,'r') z=F.variables['elevation'].get_value() #... again dirty figure() imshow(z) colorbar() #shrink=0.5 #... better f=figure() ax=f.add_subplot(111) im=ax.imshow(z) colorbar(im,ax=ax,shrink=0.5) #... changing colorbar figure() imshow(z,cmap='RdBu') colorbar(shrink=0.75,orientation='horizontal') mycmap = cm.get_cmap('RdBu_r', 11) figure() imshow(z,cmap=mycmap) colorbar(shrink=0.75,orientation='horizontal') #--- another way to get a nice colorbar --- def add_nice_legend(ax,im,cmap,cticks=None): ''' add a nice looking legend @param ax: major axis with plot @type ax: matpltlib axis object @param im: result from command like imshow @param im: matplotlib im object (???) #http://old.nabble.com/manual-placement-of-a-colorbar-td28112662.html ''' from mpl_toolkits.axes_grid import make_axes_locatable import matplotlib.axes as maxes #set legend aligned with plot (nice looking) divider = make_axes_locatable(ax) cax = divider.new_horizontal("5%", pad=0.05, axes_class=maxes.Axes) ax.figure.add_axes(cax) norm = mpl.colors.Normalize(vmin=im.get_clim()[0], vmax=im.get_clim()[1]) cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm,ticks=cticks) close('all') f=figure(); ax=f.add_subplot(111) im=ax.imshow(z,cmap=mycmap) add_nice_legend(ax,im,mycmap) #---- stop ''' contourplots are generated with contour() or contourf() function ''' ''' saving a figure ... ''' f=figure(); ax=f.add_subplot(111) ax.imshow(z); ax.set_title('global topography [m]') f.savefig('elevation1.png') f.savefig('elevation1.pdf') f.savefig('elevation2.pdf',bbox_inches='tight') ''' ... one of my favorites ''' close('all') f=figure(); ax1=f.add_subplot(211); ax2=f.add_subplot(212) ax1.fill_between(x,sin(x)) ax2.fill_between(x,sin(x),y2=cos(x),color='red') }}}