From b80a8c5649b94394d257f759cd8fd9af592ed2fc Mon Sep 17 00:00:00 2001 From: Michael Levy Date: Tue, 30 Jul 2024 17:09:24 -0600 Subject: [PATCH] Fix colorbar in plot_contour() 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 --- examples/nblibrary/glc/LIWG_SMB_diagnostic.ipynb | 15 ++++++++++++++- examples/nblibrary/glc/utils.py | 9 +++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/nblibrary/glc/LIWG_SMB_diagnostic.ipynb b/examples/nblibrary/glc/LIWG_SMB_diagnostic.ipynb index d661686..bc123c9 100644 --- a/examples/nblibrary/glc/LIWG_SMB_diagnostic.ipynb +++ b/examples/nblibrary/glc/LIWG_SMB_diagnostic.ipynb @@ -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", @@ -283,7 +284,15 @@ "\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", @@ -291,6 +300,7 @@ " 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", @@ -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", @@ -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", @@ -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", diff --git a/examples/nblibrary/glc/utils.py b/examples/nblibrary/glc/utils.py index b93300d..2a11bc3 100644 --- a/examples/nblibrary/glc/utils.py +++ b/examples/nblibrary/glc/utils.py @@ -66,7 +66,7 @@ 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. @@ -74,6 +74,7 @@ def plot_contour(da, fig, ax, title, vmin, vmax, cmap, mm_to_Gt): 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 @@ -81,15 +82,15 @@ def plot_contour(da, fig, ax, title, vmin, vmax, cmap, mm_to_Gt): 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)