1 |
#!/usr/bin/env python |
---|
2 |
""" |
---|
3 |
Mock-up of adcp timeseries using pcolor and messing with plot stuff |
---|
4 |
1. pcolor adcp velocity components with depth |
---|
5 |
2. colormap |
---|
6 |
3. right-hand scale using second axis on top |
---|
7 |
4. legend |
---|
8 |
5. ylabel |
---|
9 |
""" |
---|
10 |
|
---|
11 |
import os, sys |
---|
12 |
import datetime, time |
---|
13 |
import pycdf |
---|
14 |
import numpy |
---|
15 |
|
---|
16 |
sys.path.append('/home/haines/nccoos/raw2proc') |
---|
17 |
del(sys) |
---|
18 |
|
---|
19 |
from pylab import figure, twinx, savefig, setp, getp, cm, colorbar |
---|
20 |
from matplotlib.ticker import FixedLocator, FormatStrFormatter |
---|
21 |
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, date2num, num2date |
---|
22 |
from procutil import dt2es, es2dt |
---|
23 |
|
---|
24 |
print ' ... loading data for graph' |
---|
25 |
ncFile1='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_01.nc' |
---|
26 |
ncFile2='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_02.nc' |
---|
27 |
nc = pycdf.CDFMF((ncFile1, ncFile2)) |
---|
28 |
ncvars = nc.variables() |
---|
29 |
print ncvars |
---|
30 |
es = nc.var('time')[:] |
---|
31 |
units = nc.var('time').units |
---|
32 |
dt = [es2dt(e) for e in es] |
---|
33 |
dn = date2num(dt) |
---|
34 |
z = nc.var('z')[:] |
---|
35 |
wd = nc.var('water_depth')[:] |
---|
36 |
en = nc.var('en')[:] |
---|
37 |
u = nc.var('u')[:] |
---|
38 |
v = nc.var('v')[:] |
---|
39 |
|
---|
40 |
label_str = 'Current Velocity' + '(' + nc.var('u').units + ')' |
---|
41 |
# nc.close() |
---|
42 |
|
---|
43 |
# create a masked array for plotting |
---|
44 |
um = numpy.ma.masked_where(numpy.isnan(u), u) |
---|
45 |
|
---|
46 |
fig = figure(figsize=(10, 8)) |
---|
47 |
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9) |
---|
48 |
ax = fig.add_subplot(4,1,1) |
---|
49 |
# pc = ax.pcolor(dn, z, u.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) |
---|
50 |
|
---|
51 |
# using masked array |
---|
52 |
# pc = ax.pcolor(dn, z, um.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) |
---|
53 |
# pc = ax.pcolor(dn, z, um.transpose(), vmin=um.min(), vmax=um.max()) |
---|
54 |
# pc = ax.pcolor(dn, z, um.T, vmin=um.min(), vmax=um.max()) |
---|
55 |
pc = ax.pcolor(dn, z, um.T, vmin=-0.5, vmax=0.5) |
---|
56 |
pc.set_label('True Eastward Current (m s-1)') |
---|
57 |
# in data coords |
---|
58 |
# ax.text(date2num(datetime.datetime(2008,1,2,0,0,0), 4, pc.get_label(), fontsize="small") |
---|
59 |
# in axes coords |
---|
60 |
ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) |
---|
61 |
|
---|
62 |
# setup colorbar axes instance. |
---|
63 |
l,b,w,h = ax.get_position() |
---|
64 |
cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) |
---|
65 |
|
---|
66 |
cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar |
---|
67 |
cb.set_label('Current Velocity (m s-1)') |
---|
68 |
cb.ax.xaxis.set_label_position('top') |
---|
69 |
cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) |
---|
70 |
cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) |
---|
71 |
# cb.ax.xaxis.set_major_locator(FixedLocator([-0.4, -0.2, 0, 0.2, 0.4])) |
---|
72 |
# cb.ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f')) |
---|
73 |
|
---|
74 |
# note that plot returns a list of lines. The "l1, = plot" usage |
---|
75 |
# extracts the first element of the list inot l1 using tuple |
---|
76 |
# unpacking. So l1 is a Line2D instance, not a sequence of lines |
---|
77 |
l1, = ax.plot_date(dt, wd, fmt='k-') |
---|
78 |
l1.set_label('Water Depth (m)') |
---|
79 |
|
---|
80 |
ax.set_ylabel('HAB (m)') |
---|
81 |
ax.set_ylim(2.,10.) |
---|
82 |
# first to last regardless of what |
---|
83 |
# ax.set_xlim(dt[0], dt[-1]) |
---|
84 |
# last minus 30 days, |
---|
85 |
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) |
---|
86 |
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) |
---|
87 |
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) |
---|
88 |
ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) |
---|
89 |
|
---|
90 |
xlabel_str = 'Last 30 days' |
---|
91 |
ax.set_xlabel(xlabel_str) |
---|
92 |
# also can set properties |
---|
93 |
# setp(ax, xticklabels=['']) |
---|
94 |
# setp(ax, xlabel='') |
---|
95 |
|
---|
96 |
# right-hand side scale |
---|
97 |
ax2 = twinx(ax) |
---|
98 |
ax2.yaxis.tick_right() |
---|
99 |
# convert (lhs) meters to (rhs) feet |
---|
100 |
feet = [3.281*val for val in ax.get_ylim()] |
---|
101 |
ax2.set_ylim(feet) |
---|
102 |
ax2.set_ylabel('HAB (ft)') |
---|
103 |
|
---|
104 |
# legend (labels already specified above for pcolor and lines |
---|
105 |
# ax.legend(loc='upper left') |
---|
106 |
leg = ax.legend((l1,), (l1.get_label(),), loc='upper left') |
---|
107 |
# leg = gca().get_legend() |
---|
108 |
ltext = leg.get_texts() # all the text.Text instance in the legend |
---|
109 |
llines = leg.get_lines() # all the lines.Line2D instance in the legend |
---|
110 |
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend |
---|
111 |
|
---|
112 |
# see text.Text, lines.Line2D, and patches.Rectangle for more info on |
---|
113 |
# the settable properties of lines, text, and rectangles |
---|
114 |
frame.set_facecolor('0.80') # set the frame face color to light gray |
---|
115 |
frame.set_alpha(0.5) |
---|
116 |
setp(ltext, fontsize='small') # the legend text fontsize |
---|
117 |
setp(llines, linewidth=1.5) # the legend linewidth |
---|
118 |
# leg.draw_frame(False) # don't draw the legend frame |
---|
119 |
|
---|
120 |
savefig('test_colorbar.png') |
---|
121 |
os.system('ls *.png') |
---|
122 |
os.system('scp -r *.png haines@pitot.marine.unc.edu:/afs/isis.unc.edu/depts/marine/workspace/haines/public_html/nccoos/test') |
---|
123 |
|
---|