-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_plots.py
216 lines (173 loc) · 9.48 KB
/
make_plots.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import absolute_import, division, print_function, \
unicode_literals
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as cols
from matplotlib.colors import BoundaryNorm
import cartopy
import cartopy.crs as ccrs
from cartopy.util import add_cyclic_point
import matplotlib.ticker as mticker
import cmocean
import copy
from common_functions import add_land_lakes_coastline
def make_scatter_plot(lon, lat, dotSize, figTitle, figFile, projectionName='Robinson', lon0=-180, lon1=180, dlon=40, lat0=-90, lat1=90, dlat=20, fld=None, cmap=None, clevels=None, cindices=None, cbarLabel=None):
figdpi = 150
figsize = [20, 20]
data_crs = ccrs.PlateCarree()
plt.figure(figsize=figsize, dpi=figdpi)
if projectionName=='NorthPolarStereo':
ax = plt.axes(projection=ccrs.NorthPolarStereo(central_longitude=0))
elif projectionName=='SouthPolarStereo':
ax = plt.axes(projection=ccrs.SouthPolarStereo(central_longitude=0))
else:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.set_extent([lon0, lon1, lat0, lat1], crs=data_crs)
gl = ax.gridlines(crs=data_crs, color='k', linestyle=':', zorder=6, draw_labels=True)
gl.xlocator = mticker.FixedLocator(np.arange(lon0, lon1+dlon, dlon))
gl.ylocator = mticker.FixedLocator(np.arange(lat0+dlat/2, lat1-dlat/2, dlat))
gl.n_steps = 100
gl.right_labels = False
gl.xformatter = cartopy.mpl.gridliner.LONGITUDE_FORMATTER
gl.yformatter = cartopy.mpl.gridliner.LATITUDE_FORMATTER
gl.rotate_labels = False
add_land_lakes_coastline(ax)
if fld is not None:
[colormap, cnorm] = _make_discrete_colormap(cmap, cindices, clevels)
sc = ax.scatter(lon, lat, s=dotSize, c=fld, cmap=colormap, norm=cnorm, marker='o', transform=data_crs)
if cindices is not None:
cbar = plt.colorbar(sc, ticks=clevels, boundaries=clevels, location='right', pad=0.05, shrink=.5, extend='both')
else:
cbar = plt.colorbar(sc, ticks=clevels, boundaries=clevels, location='right', pad=0.05, shrink=.5)
cbar.ax.tick_params(labelsize=16, labelcolor='black')
cbar.set_label(cbarLabel, fontsize=14)
else:
sc = ax.scatter(lon, lat, s=dotSize, c='k', marker='D', transform=data_crs)
ax.set_title(figTitle, y=1.04, fontsize=16)
plt.savefig(figFile, bbox_inches='tight')
plt.close()
def make_streamline_plot(lon, lat, u, v, speed, density, cmap, clevels, cindices, cbarLabel, projectionName, figTitle, figFile, lon0=-180, lon1=180, dlon=40, lat0=-90, lat1=90, dlat=20):
figdpi = 150
figsize = [20, 20]
[colormap, cnorm] = _make_discrete_colormap(cmap, cindices, clevels)
data_crs = ccrs.PlateCarree()
plt.figure(figsize=figsize, dpi=figdpi)
if projectionName=='NorthPolarStereo':
ax = plt.axes(projection=ccrs.NorthPolarStereo(central_longitude=0))
elif projectionName=='SouthPolarStereo':
ax = plt.axes(projection=ccrs.SouthPolarStereo(central_longitude=0))
else:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.set_extent([lon0, lon1, lat0, lat1], crs=data_crs)
gl = ax.gridlines(crs=data_crs, color='k', linestyle=':', zorder=6, draw_labels=True)
gl.xlocator = mticker.FixedLocator(np.arange(lon0, lon1+dlon, dlon))
gl.ylocator = mticker.FixedLocator(np.arange(lat0-dlat/2, lat1+dlat/2, dlat))
gl.n_steps = 100
gl.right_labels = False
gl.xformatter = cartopy.mpl.gridliner.LONGITUDE_FORMATTER
gl.yformatter = cartopy.mpl.gridliner.LATITUDE_FORMATTER
gl.rotate_labels = False
add_land_lakes_coastline(ax)
ucyclic, loncyclic = add_cyclic_point(u, lon)
vcyclic, loncyclic = add_cyclic_point(v, lon)
speedcyclic, loncyclic = add_cyclic_point(speed, lon)
loncyclic = np.where(loncyclic>=180., loncyclic-360., loncyclic)
sl = ax.streamplot(loncyclic, lat, ucyclic, vcyclic, density=density, broken_streamlines=False,
color=speedcyclic, cmap=colormap, norm=cnorm, linewidth=1.5, transform=data_crs)
#sl = ax.streamplot(lon, lat, u, v, density=density, broken_streamlines=False,
# color=speed, cmap=colormap, linewidth=1.5, transform=data_crs)
cbar = plt.colorbar(sl.lines, ticks=clevels, boundaries=clevels, location='right', pad=0.05, shrink=.5, extend='both')
cbar.ax.tick_params(labelsize=16, labelcolor='black')
cbar.set_label(cbarLabel, fontsize=14)
ax.set_title(figTitle, y=1.04, fontsize=16)
plt.savefig(figFile, bbox_inches='tight')
plt.close()
def make_contourf_plot(lon, lat, fld, cmap, clevels, cindices, cbarLabel, figTitle, figFile, contourFld=None, contourValues=None, projectionName='Robinson', lon0=-180, lon1=180, dlon=40, lat0=-90, lat1=90, dlat=20):
figdpi = 150
figsize = [20, 20]
[colormap, cnorm] = _make_discrete_colormap(cmap, cindices, clevels)
data_crs = ccrs.PlateCarree()
plt.figure(figsize=figsize, dpi=figdpi)
if projectionName=='NorthPolarStereo':
ax = plt.axes(projection=ccrs.NorthPolarStereo(central_longitude=0))
elif projectionName=='SouthPolarStereo':
ax = plt.axes(projection=ccrs.SouthPolarStereo(central_longitude=0))
else:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.set_extent([lon0, lon1, lat0, lat1], crs=data_crs)
gl = ax.gridlines(crs=data_crs, color='k', linestyle=':', zorder=6, draw_labels=True)
gl.xlocator = mticker.FixedLocator(np.arange(lon0, lon1+dlon, dlon))
gl.ylocator = mticker.FixedLocator(np.arange(lat0+dlat/2, lat1-dlat/2, dlat))
gl.n_steps = 100
gl.right_labels = False
gl.xformatter = cartopy.mpl.gridliner.LONGITUDE_FORMATTER
gl.yformatter = cartopy.mpl.gridliner.LATITUDE_FORMATTER
gl.rotate_labels = False
add_land_lakes_coastline(ax)
fldcyclic, loncyclic = add_cyclic_point(fld, lon)
[lon, lat] = np.meshgrid(loncyclic, lat)
cf = ax.contourf(lon, lat, fldcyclic, cmap=colormap, norm=cnorm, levels=clevels, extend='both', transform=data_crs)
cbar = plt.colorbar(cf, ticks=clevels, boundaries=clevels, location='right', pad=0.05, shrink=.5, extend='both')
cbar.ax.tick_params(labelsize=14, labelcolor='black')
cbar.set_label(cbarLabel, fontsize=12, fontweight='bold')
if (contourFld is not None) and (contourValues is not None):
cs = ax.contour(lon, lat, contourFld, contourValues, colors='k', linewidths=1.5)
cb = plt.clabel(cs, levels=contourValues, inline=True, inline_spacing=2, fmt='%2.1f', fontsize=9)
ax.set_title(figTitle, y=1.04, fontsize=16)
plt.savefig(figFile, bbox_inches='tight')
plt.close()
def make_pcolormesh_plot(lon, lat, fld, cmap, clevels, cindices, cbarLabel, figTitle, figFile, contourFld=None, contourValues=None, projectionName='Robinson', lon0=-180, lon1=180, dlon=40, lat0=-90, lat1=90, dlat=20):
figdpi = 150
figsize = [20, 20]
[colormap, cnorm] = _make_discrete_colormap(cmap, cindices, clevels)
data_crs = ccrs.PlateCarree()
plt.figure(figsize=figsize, dpi=figdpi)
if projectionName=='NorthPolarStereo':
ax = plt.axes(projection=ccrs.NorthPolarStereo(central_longitude=0))
elif projectionName=='SouthPolarStereo':
ax = plt.axes(projection=ccrs.SouthPolarStereo(central_longitude=0))
else:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.set_extent([lon0, lon1, lat0, lat1], crs=data_crs)
gl = ax.gridlines(crs=data_crs, color='k', linestyle=':', zorder=6, draw_labels=True)
gl.xlocator = mticker.FixedLocator(np.arange(lon0, lon1+dlon, dlon))
gl.ylocator = mticker.FixedLocator(np.arange(lat0+dlat/2, lat1-dlat/2, dlat))
gl.n_steps = 100
gl.right_labels = False
gl.xformatter = cartopy.mpl.gridliner.LONGITUDE_FORMATTER
gl.yformatter = cartopy.mpl.gridliner.LATITUDE_FORMATTER
gl.rotate_labels = False
add_land_lakes_coastline(ax)
fldcyclic, loncyclic = add_cyclic_point(fld, lon)
[lon, lat] = np.meshgrid(loncyclic, lat)
cf = ax.pcolormesh(lon, lat, fldcyclic, cmap=colormap, norm=cnorm, transform=data_crs)
cbar = plt.colorbar(cf, ticks=clevels, boundaries=clevels, location='right', pad=0.05, shrink=.5, extend='both')
cbar.ax.tick_params(labelsize=14, labelcolor='black')
cbar.set_label(cbarLabel, fontsize=12, fontweight='bold')
if (contourFld is not None) and (contourValues is not None):
cs = ax.contour(lon, lat, contourFld, contourValues, colors='k', linewidths=1.5)
cb = plt.clabel(cs, levels=contourValues, inline=True, inline_spacing=2, fmt='%2.1f', fontsize=9)
ax.set_title(figTitle, y=1.04, fontsize=16)
plt.savefig(figFile, bbox_inches='tight')
plt.close()
def _make_discrete_colormap(colormap, colorindices, colorlevels):
if colorindices is not None:
colorindices0 = colorindices
if len(colorlevels)+1 == len(colorindices0):
# we have 2 extra values for the under/over so make the colormap
# without these values
colorindices = colorindices0[1:-1]
underColor = colormap(colorindices0[0])
overColor = colormap(colorindices0[-1])
else:
colorindices = colorindices0
underColor = None
overColor = None
colormap = cols.ListedColormap(colormap(colorindices))
if underColor is not None:
colormap.set_under(underColor)
if overColor is not None:
colormap.set_over(overColor)
colornorm = mpl.colors.BoundaryNorm(colorlevels, colormap.N)
return [colormap, colornorm]