-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathNCL_hov_3.py
119 lines (98 loc) · 3.76 KB
/
NCL_hov_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
NCL_hov_3.py
================
This script illustrates the following concepts:
- Creating a Hovmueller plot
- Hatching fill between contours
See following URLs to see the reproduced NCL plot & script:
- Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/hov_3.ncl
- Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/hov_3_lg.png
"""
###############################################################################
# Import packages:
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import geocat.datafiles as gdf
import geocat.viz as gv
###############################################################################
# Read in data:
# Open a netCDF data file using xarray default engine
# and load the data into xarrays
ds = xr.open_dataset(gdf.get('netcdf_files/chi200_ud_smooth.nc'))
lon = ds.lon
times = ds.time
scale = 1000000
chi = ds.CHI
chi = chi / scale
###############################################################################
# Plot:
# Initialize figure and axis
fig, ax = plt.subplots(figsize=(7, 7.5))
# Fill area between level 4 contours and level 10 contours with dot hatching
cf = ax.contourf(lon,
times,
chi,
levels=[4, 12],
colors='None',
hatches=['....'])
# Make all dot-filled areas light gray so contour lines are still visible
cf.set_edgecolor('lightgray')
cf.set_linewidth(0.)
# Fill area at the lowest contour level, -6, with line hatching
cf = ax.contourf(lon,
times,
chi,
levels=[-7, -6],
colors='None',
hatches=['///'])
# Draw contour lines at levels [-6, -4, -2, 0, 2, 4, 6, 8, 10]
cs = ax.contour(lon,
times,
chi,
levels=np.arange(-6, 12, 2),
colors='black',
linestyles="-",
linewidths=.2)
# Set 0 level contour line to a thicker linewidth
# If you try to access the "levels" attribute of cs (cs.levels),
# the list of levels is: [-6, -4, -2, 0, 2, 4, 6, 8, 10]
# level 0 is at the 3rd index of that list, so those contour lines
# can be accessed at cs.collections[3]
cs.collections[3].set_linewidth(1)
# Label the contour levels -4, 0, and 4
cl = ax.clabel(cs, fmt='%d', levels=[-4, 0, 4])
# Use geocat.viz.util convenience function to set axes limits & tick values
gv.set_axes_limits_and_ticks(ax,
xlim=[100, 220],
ylim=[0, 1.55 * 1e16],
xticks=[135, 180],
yticks=np.linspace(0, 1.55 * 1e16, 7),
xticklabels=['135E', '180'],
yticklabels=np.linspace(0, 180, 7, dtype='int'))
# Use geocat.viz.util convenience function to add minor and major tick lines
gv.add_major_minor_ticks(ax,
x_minor_per_major=3,
y_minor_per_major=3,
labelsize=16)
# Use geocat.viz.util convenience function to add titles
gv.set_titles_and_labels(ax,
maintitle="Pacific Region",
maintitlefontsize=20,
lefttitle="Velocity Potential",
lefttitlefontsize=18,
righttitle="m2/s",
righttitlefontsize=18,
ylabel="elapsed time",
labelfontsize=18)
# Add lower text box
ax.text(1,
-0.12,
"CONTOUR FROM -6 TO 10 BY 2",
horizontalalignment='right',
transform=ax.transAxes,
bbox=dict(boxstyle='square, pad=0.25',
facecolor='white',
edgecolor='black'))
plt.tight_layout()
plt.show()