#!/usr/bin/env python """ Mock-up of adcp timeseries using pcolor and messing with plot stuff 1. pcolor adcp velocity components with depth 2. colormap 3. right-hand scale using second axis on top 4. legend 5. ylabel """ import os, sys import datetime, time import pycdf import numpy sys.path.append('/home/haines/nccoos/raw2proc') del(sys) from pylab import figure, twinx, savefig, setp, getp, cm, colorbar from matplotlib.ticker import FixedLocator, FormatStrFormatter from matplotlib.dates import DayLocator, HourLocator, DateFormatter, date2num, num2date from procutil import dt2es, es2dt print ' ... loading data for graph' ncFile1='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_01.nc' ncFile2='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_02.nc' nc = pycdf.CDFMF((ncFile1, ncFile2)) ncvars = nc.variables() print ncvars es = nc.var('time')[:] units = nc.var('time').units dt = [es2dt(e) for e in es] dn = date2num(dt) z = nc.var('z')[:] wd = nc.var('water_depth')[:] en = nc.var('en')[:] u = nc.var('u')[:] v = nc.var('v')[:] label_str = 'Current Velocity' + '(' + nc.var('u').units + ')' # nc.close() # create a masked array for plotting um = numpy.ma.masked_where(numpy.isnan(u), u) fig = figure(figsize=(10, 8)) fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9) ax = fig.add_subplot(4,1,1) # pc = ax.pcolor(dn, z, u.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) # using masked array # pc = ax.pcolor(dn, z, um.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) # pc = ax.pcolor(dn, z, um.transpose(), vmin=um.min(), vmax=um.max()) # pc = ax.pcolor(dn, z, um.T, vmin=um.min(), vmax=um.max()) pc = ax.pcolor(dn, z, um.T, vmin=-0.5, vmax=0.5) pc.set_label('True Eastward Current (m s-1)') # in data coords # ax.text(date2num(datetime.datetime(2008,1,2,0,0,0), 4, pc.get_label(), fontsize="small") # in axes coords ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) # setup colorbar axes instance. l,b,w,h = ax.get_position() cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar cb.set_label('Current Velocity (m s-1)') cb.ax.xaxis.set_label_position('top') cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) # cb.ax.xaxis.set_major_locator(FixedLocator([-0.4, -0.2, 0, 0.2, 0.4])) # cb.ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f')) # note that plot returns a list of lines. The "l1, = plot" usage # extracts the first element of the list inot l1 using tuple # unpacking. So l1 is a Line2D instance, not a sequence of lines l1, = ax.plot_date(dt, wd, fmt='k-') l1.set_label('Water Depth (m)') ax.set_ylabel('HAB (m)') ax.set_ylim(2.,10.) # first to last regardless of what # ax.set_xlim(dt[0], dt[-1]) # last minus 30 days, ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) xlabel_str = 'Last 30 days' ax.set_xlabel(xlabel_str) # also can set properties # setp(ax, xticklabels=['']) # setp(ax, xlabel='') # right-hand side scale ax2 = twinx(ax) ax2.yaxis.tick_right() # convert (lhs) meters to (rhs) feet feet = [3.281*val for val in ax.get_ylim()] ax2.set_ylim(feet) ax2.set_ylabel('HAB (ft)') # legend (labels already specified above for pcolor and lines # ax.legend(loc='upper left') leg = ax.legend((l1,), (l1.get_label(),), loc='upper left') # leg = gca().get_legend() ltext = leg.get_texts() # all the text.Text instance in the legend llines = leg.get_lines() # all the lines.Line2D instance in the legend frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend # see text.Text, lines.Line2D, and patches.Rectangle for more info on # the settable properties of lines, text, and rectangles frame.set_facecolor('0.80') # set the frame face color to light gray frame.set_alpha(0.5) setp(ltext, fontsize='small') # the legend text fontsize setp(llines, linewidth=1.5) # the legend linewidth # leg.draw_frame(False) # don't draw the legend frame savefig('test_colorbar.png') os.system('ls *.png') os.system('scp -r *.png haines@pitot.marine.unc.edu:/afs/isis.unc.edu/depts/marine/workspace/haines/public_html/nccoos/test')