|
3 | 3 | from copy import deepcopy |
4 | 4 | from math import ceil as MATHceil |
5 | 5 | from math import floor as MATHfloor |
| 6 | +import cartopy.crs as ccrs |
| 7 | +import cartopy.feature as cfeature |
| 8 | +from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter |
6 | 9 | from matplotlib.colors import BoundaryNorm, ListedColormap |
7 | 10 | from matplotlib.gridspec import GridSpec |
8 | 11 | from matplotlib.lines import Line2D |
9 | 12 | from matplotlib.patches import Polygon |
10 | 13 | from matplotlib.ticker import MaxNLocator |
| 14 | +import matplotlib.ticker as mticker |
11 | 15 | import matplotlib.pyplot as plt |
12 | | -from mpl_toolkits.basemap import Basemap |
13 | 16 | from numpy import arange as NUMPYarange |
14 | 17 | from numpy import array as NUMPYarray |
15 | 18 | from numpy import linspace as NUMPYlinspace |
@@ -1019,9 +1022,11 @@ def my_map(model, filename_nc, dict_param, reference, metric_variables, figure_n |
1019 | 1022 | "reg_pr_over_sst_map" in variables or "reg_slp_over_sst_map" in variables or |
1020 | 1023 | "reg_ts_over_sst_map" in variables or "djf_map__" in variables or "jja_map__" in variables)) or\ |
1021 | 1024 | (isinstance(variables, list) is True and ("djf_map__" in variables[0] or "jja_map__" in variables[0])): |
1022 | | - fig, axes = plt.subplots(nbrl, nbrc, figsize=(6 * nbrc, 6 * nbrl), sharex="col", sharey="row") |
| 1025 | + fig, axes = plt.subplots(nbrl, nbrc, figsize=(6 * nbrc, 6 * nbrl), sharex="col", sharey="row", |
| 1026 | + subplot_kw={'projection': ccrs.PlateCarree(central_longitude=180)}) |
1023 | 1027 | else: |
1024 | | - fig, axes = plt.subplots(nbrl, nbrc, figsize=(4 * nbrc, 4 * nbrl), sharex="col", sharey="row") |
| 1028 | + fig, axes = plt.subplots(nbrl, nbrc, figsize=(4 * nbrc, 4 * nbrl), sharex="col", sharey="row", |
| 1029 | + subplot_kw={'projection': ccrs.PlateCarree(central_longitude=180)}) |
1025 | 1030 | hspa1 = 0.1 |
1026 | 1031 | hspa2 = 0.01 |
1027 | 1032 | if ((nbrc == 2 and nbrl == 2) or (nbrc == 1 and plot_ref is True)) and isinstance(variables, list) is True and\ |
@@ -1143,26 +1148,33 @@ def my_map(model, filename_nc, dict_param, reference, metric_variables, figure_n |
1143 | 1148 | transform=ax.transAxes) |
1144 | 1149 | # map |
1145 | 1150 | xx, yy = NUMPYmeshgrid(lon, lat) |
| 1151 | + # set extent |
1146 | 1152 | if lat[-1] - lat[0] < 40: |
1147 | | - locmap = Basemap(projection="cyl", llcrnrlat=lat[0] - 5, urcrnrlat=lat[-1] + 5, llcrnrlon=lon[0], |
1148 | | - urcrnrlon=lon[-1], ax=ax) |
| 1153 | + ax.set_extent([lon[0], lon[-1], lat[0] - 5, lat[-1] + 5], crs=ccrs.PlateCarree()) |
1149 | 1154 | else: |
1150 | | - locmap = Basemap(projection="cyl", llcrnrlat=lat[0], urcrnrlat=lat[-1], llcrnrlon=lon[0], urcrnrlon=lon[-1], |
1151 | | - ax=ax) |
| 1155 | + ax.set_extent([lon[0], lon[-1], lat[0], lat[-1]], crs=ccrs.PlateCarree()) |
1152 | 1156 | # draw coastlines |
1153 | | - locmap.drawcoastlines() |
| 1157 | + ax.coastlines() |
1154 | 1158 | # fill continents |
1155 | | - if maskland is True: |
1156 | | - locmap.fillcontinents(color="gainsboro") |
1157 | | - if maskocean is True: |
1158 | | - locmap.drawmapboundary(fill_color="white") |
1159 | | - # draw parallels |
1160 | | - locmap.drawparallels(ylabel_ticks, labels=[1, 0, 0, 0], fontsize=12, dashes=[3, 1], linewidth=1) |
1161 | | - # draw meridians |
1162 | | - locmap.drawmeridians(xlabel_ticks, labels=[0, 0, 0, 1], fontsize=12, dashes=[3, 1], linewidth=1) |
1163 | | - #cs = locmap.pcolormesh(xx, yy, tab[ii], vmin=min(labelbar), vmax=max(labelbar), cmap=colorbar) |
| 1159 | + if maskland: |
| 1160 | + ax.add_feature(cfeature.LAND, color="gainsboro") |
| 1161 | + if maskocean: |
| 1162 | + ax.add_feature(cfeature.OCEAN, color="white") |
| 1163 | + # adjust the yticks to convert longitude over 180 to negative to properly add gridlines |
| 1164 | + xlabel_ticks_adjusted = [i if i < 180 else i - 360 for i in xlabel_ticks] |
| 1165 | + # draw parallels and meridians by adding grid lines only at specified ticks |
| 1166 | + gl = ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree(), linestyle='--', color='k') |
| 1167 | + gl.xlocator = mticker.FixedLocator(xlabel_ticks_adjusted) |
| 1168 | + gl.ylocator = mticker.FixedLocator(ylabel_ticks) |
| 1169 | + gl.xformatter = LongitudeFormatter() |
| 1170 | + gl.yformatter = LatitudeFormatter() |
| 1171 | + gl.top_labels = False |
| 1172 | + gl.right_labels = False |
| 1173 | + gl.xlabel_style = {'size': 12} |
| 1174 | + gl.ylabel_style = {'size': 12} |
| 1175 | + # contour plot |
1164 | 1176 | levels = create_levels(labelbar) |
1165 | | - cs = locmap.contourf(xx, yy, tab[ii], levels=levels, extend="both", cmap=colorbar) |
| 1177 | + cs = ax.contourf(xx, yy, tab[ii], levels=levels, extend="both", cmap=colorbar, transform=ccrs.PlateCarree()) |
1166 | 1178 | # my text |
1167 | 1179 | if (ii > 0 and plot_metric is True and isinstance(variables, list) is False) or\ |
1168 | 1180 | (isinstance(variables, list) is True and "nina" in variables[0] and "nino" in variables[1] and |
@@ -1219,7 +1231,7 @@ def my_map(model, filename_nc, dict_param, reference, metric_variables, figure_n |
1219 | 1231 | lreg = ReferenceRegions(regions[metric_variables[0]]) |
1220 | 1232 | lons = [lreg["longitude"][0]] * 2 + [lreg["longitude"][1]] * 2 |
1221 | 1233 | lats = list(lreg["latitude"]) + list(reversed(list(lreg["latitude"]))) |
1222 | | - x, y = locmap(lons, lats) |
| 1234 | + x, y = lons, lats |
1223 | 1235 | ax.add_patch(Polygon(list(zip(x, y)), edgecolor="k", linewidth=3, linestyle="-", facecolor="none")) |
1224 | 1236 | # if ii == 0 and plot_metric is True: |
1225 | 1237 | # x1 = ax.get_position().x1 |
|
0 commit comments