Skip to content

Commit

Permalink
Fix colorbar in plot_contour()
Browse files Browse the repository at this point in the history
Every panel was overwriting the location of the colorbar, so only the bias
contour levels were shown. Now the first two plots (with same vmin and vmax)
have a colorbar between them while bias plot colorbar is off to the right
  • Loading branch information
mnlevy1981 committed Jul 30, 2024
1 parent f714646 commit b80a8c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 14 additions & 1 deletion examples/nblibrary/glc/LIWG_SMB_diagnostic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
" smb_case_climo,\n",
" fig,\n",
" ax[0],\n",
" 0.35,\n",
" f\"{case_name}\\nSMB (mm/y w.e.)\\nMean from {first_year:04d} - {last_year:04d}\",\n",
" vmin,\n",
" vmax,\n",
Expand All @@ -283,14 +284,23 @@
"\n",
"## Center panel\n",
"utils.plot_contour(\n",
" smb_obs_climo, fig, ax[1], \"SMB Obs\\n(mm/y w.e.)\", vmin, vmax, my_cmap, mm_to_Gt\n",
" smb_obs_climo,\n",
" fig,\n",
" ax[1],\n",
" 0.35,\n",
" \"SMB Obs\\n(mm/y w.e.)\",\n",
" vmin,\n",
" vmax,\n",
" my_cmap,\n",
" mm_to_Gt,\n",
")\n",
"\n",
"## Right panel\n",
"utils.plot_contour(\n",
" smb_case_climo - smb_obs_climo,\n",
" fig,\n",
" ax[2],\n",
" 0.89,\n",
" \"SMB bias (mm/yr w.e.)\",\n",
" vmin,\n",
" vmax,\n",
Expand Down Expand Up @@ -324,6 +334,7 @@
" smb_case_climo,\n",
" fig,\n",
" ax[0],\n",
" 0.35,\n",
" f\"{case_name}\\nSMB (mm/y w.e.)\\nMean from {first_year:04d} - {last_year:04d}\",\n",
" vmin,\n",
" vmax,\n",
Expand All @@ -336,6 +347,7 @@
" smb_base_climo,\n",
" fig,\n",
" ax[1],\n",
" 0.35,\n",
" f\"{base_case_name}\\nSMB (mm/y w.e.)\\nMean from {base_first_year:04d} - {base_last_year:04d}\",\n",
" vmin,\n",
" vmax,\n",
Expand All @@ -348,6 +360,7 @@
" smb_case_climo - smb_base_climo,\n",
" fig,\n",
" ax[2],\n",
" 0.89,\n",
" \"SMB difference (mm/yr w.e.)\",\n",
" vmin,\n",
" vmax,\n",
Expand Down
9 changes: 5 additions & 4 deletions examples/nblibrary/glc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,31 @@ def set_plot_prop_clean(ax):
ax.set_yticks([])


def plot_contour(da, fig, ax, title, vmin, vmax, cmap, mm_to_Gt):
def plot_contour(da, fig, ax, left, title, vmin, vmax, cmap, mm_to_Gt):
"""
Plot a contour map of surface mass balance (assumed to be in da.data).
Also computes global mean, in Gt, and prints average in lower left corner.
Arguments:
da - xr.DataArray containing SMB in units of mm/yr
fig - matplotlib.figure.Figure
ax - matplotlib.axes.Axes
left - left dimension of rect (dimensions for colorbar)
title - string containing title of plot
vmin - minimum value for contours
vmax - maximum value for contours
cmap - matplotlib.colors.Colormap
mm_to_Gt - conversion factor for mm/yr -> Gt/yr
"""
avg_data = np.round(da.sum().data * mm_to_Gt, 2)
last_panel0 = ax.imshow(da.data[:, :], vmin=vmin, vmax=vmax, cmap=cmap)
last_panel = ax.imshow(da.data[:, :], vmin=vmin, vmax=vmax, cmap=cmap)
ax.set_title(title, fontsize=16)
set_plot_prop_clean(ax)
ax.annotate("net avg =" + str(avg_data) + " Gt/yr", xy=(5, 5), fontsize=16)

pos = ax.get_position()
cax = fig.add_axes([0.35, pos.y0, 0.02, pos.y1 - pos.y0])
cax = fig.add_axes([left, pos.y0, 0.02, pos.y1 - pos.y0])

cbar = fig.colorbar(last_panel0, cax=cax)
cbar = fig.colorbar(last_panel, cax=cax)
cbar.ax.tick_params(labelsize=16)


Expand Down

0 comments on commit b80a8c5

Please sign in to comment.