-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_PLC_2018.py
executable file
·127 lines (100 loc) · 3.75 KB
/
plot_PLC_2018.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
#!/usr/bin/env python
"""
Plot DJF for each year of the Millennium drought
"""
__author__ = "Martin De Kauwe"
__version__ = "1.0 (25.07.2019)"
__email__ = "mdekauwe@gmail.com"
import os
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
import cartopy.crs as ccrs
import cartopy
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import sys
import matplotlib.ticker as mticker
from cartopy.mpl.geoaxes import GeoAxes
from mpl_toolkits.axes_grid1 import AxesGrid
from calendar import monthrange
def main(fname, plot_dir):
ds = xr.open_dataset(fname)
lat = ds.y.values
lon = ds.x.values
bottom, top = lat[0], lat[-1]
left, right = lon[0], lon[-1]
plc = ds.plc[48-13:(48-13)+6,0,:,:].values
plc = np.nanmean(plc, axis=0)
# just keep deficit areas
#cmi = np.where(cmi >= 300., np.nan, cmi)
fig = plt.figure(figsize=(9, 6))
plt.rcParams['font.family'] = "sans-serif"
plt.rcParams['font.size'] = "14"
plt.rcParams['font.sans-serif'] = "Helvetica"
cmap = plt.cm.get_cmap('YlOrRd', 6) # discrete colour map
projection = ccrs.PlateCarree()
axes_class = (GeoAxes, dict(map_projection=projection))
rows = 1
cols = 1
axgr = AxesGrid(fig, 111, axes_class=axes_class,
nrows_ncols=(rows, cols),
axes_pad=0.2,
cbar_location='right',
cbar_mode='single',
cbar_pad=0.5,
cbar_size='5%',
label_mode='') # note the empty label_mode
for i, ax in enumerate(axgr):
# add a subplot into the array of plots
#ax = fig.add_subplot(rows, cols, i+1, projection=ccrs.PlateCarree())
plims = plot_map(ax, plc, cmap, i, top, bottom, left, right)
#plims = plot_map(ax, ds.plc[0,0,:,:], cmap, i)
cbar = axgr.cbar_axes[0].colorbar(plims)
cbar.ax.set_title("Mean PLC\n(%)", fontsize=16)
#cbar.ax.set_yticklabels([' ', '-60', '-30', '0', '30', '<=1490'])
ofname = os.path.join(plot_dir, "plc_2018.png")
fig.savefig(ofname, dpi=300, bbox_inches='tight',
pad_inches=0.1)
def plot_map(ax, var, cmap, i, top, bottom, left, right):
print(np.nanmin(var), np.nanmax(var))
vmin, vmax = 0, 80
#top, bottom = 90, -90
#left, right = -180, 180
img = ax.imshow(var, origin='lower',
transform=ccrs.PlateCarree(),
interpolation='nearest', cmap=cmap,
extent=(left, right, bottom, top),
vmin=vmin, vmax=vmax)
ax.coastlines(resolution='10m', linewidth=1.0, color='black')
#ax.add_feature(cartopy.feature.OCEAN)
ax.set_xlim(140, 154)
ax.set_ylim(-39.4, -28)
ax.set_title("DJF-MAM - 2017-2018", fontsize=16)
if i == 0 or i >= 5:
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=0.5, color='black', alpha=0.5,
linestyle='--')
else:
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False,
linewidth=0.5, color='black', alpha=0.5,
linestyle='--')
#if i < 5:
#s gl.xlabels_bottom = False
if i > 5:
gl.ylabels_left = False
gl.xlabels_top = False
gl.ylabels_right = False
gl.xlines = False
gl.ylines = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlocator = mticker.FixedLocator([141, 145, 149, 153])
gl.ylocator = mticker.FixedLocator([-29, -32, -35, -38])
return img
if __name__ == "__main__":
plot_dir = "plots"
if not os.path.exists(plot_dir):
os.makedirs(plot_dir)
fname = "outputs/all_yrs_plc.nc"
main(fname, plot_dir)