Skip to content

Commit

Permalink
Now use cb.mappable.set_norm to set color bar range
Browse files Browse the repository at this point in the history
This commit addresses GCPy issue #24:
   #24

which says that the set_clim function will be deprecated in a future
version of matplotlib.

The matplotlib documentation about API changes
    https://matplotlib.org/3.1.0/api/api_changes.html

says this:
   "matplotlib.colorbar.ColorbarBase is no longer a subclass of
    cm.ScalarMappable. This inheritance lead to a confusing situation
    where the cm.ScalarMappable passed to matplotlib.colorbar.Colorbar
    (colorbar) had a set_norm method, as did the colorbar. The
    colorbar is now purely a follower to the ScalarMappable norm
    and colormap, and the old inherited methods set_norm,
    set_cmap, set_clim are deprecated, as are the getter versions
    of those calls. To set the norm associated with a colorbar
    do colorbar.mappable.set_norm() etc."

Therefore, we now use cb.mappable.set_norm(norm) to manually pass
the norm object (which renders the data range into the range of
0..1 for plotting) to the colorbar.  This produces the same effect
as before.

Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
  • Loading branch information
yantosca committed Sep 6, 2019
1 parent b98e273 commit b3a8311
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions gcpy/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ def compare_single_level(refdata, refstr, devdata, devstr, varlist=None,
# If Ref is zero or NaN everywhere, set a tick in the middle
# of the normalized color range (which will be 0..1).
cb = plt.colorbar(plot0, ax=ax0, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if ref_is_all_zero or ref_is_all_nan:
cb.set_ticks([0.5])
if ref_is_all_nan:
Expand Down Expand Up @@ -767,6 +768,7 @@ def compare_single_level(refdata, refstr, devdata, devstr, varlist=None,
# If Dev is zero or NaN everywhere, set a tick in the middle of
# the normalized color range (which will be 0..1).
cb = plt.colorbar(plot1, ax=ax1, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if dev_is_all_zero or dev_is_all_nan:
cb.set_ticks([0.5])
if dev_is_all_nan:
Expand Down Expand Up @@ -850,6 +852,7 @@ def compare_single_level(refdata, refstr, devdata, devstr, varlist=None,
# If all values of absdiff = 0, manually set a single tickmark at 0,
# which falls into the center of the blue-white-red color scale.
cb = plt.colorbar(plot2, ax=ax2, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or absdiff_is_all_nan:
cb.set_ticks([0.0])
if absdiff_is_all_nan:
Expand Down Expand Up @@ -913,6 +916,7 @@ def compare_single_level(refdata, refstr, devdata, devstr, varlist=None,
# Define the colorbar for the plot.
# If all values of absdiff = 0, then set a tick at 0.
cb = plt.colorbar(plot3, ax=ax3, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or absdiff_is_all_nan:
cb.set_ticks([0.0])
if absdiff_is_all_nan:
Expand Down Expand Up @@ -1003,6 +1007,7 @@ def compare_single_level(refdata, refstr, devdata, devstr, varlist=None,
# Use is_absdiff_all_zero to force the plot to show as all gray
# when both Dev is zero and Ref is zero (since 0/0 = undefined).
cb = plt.colorbar(plot4, ax=ax4, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or fracdiff_is_all_nan:
cb.set_ticks([0.0])
if fracdiff_is_all_nan:
Expand Down Expand Up @@ -1067,6 +1072,7 @@ def compare_single_level(refdata, refstr, devdata, devstr, varlist=None,
# Use is_absdiff_all_zero to force the plot to show as all gray
# when both Dev is zero and Ref is zero (since 0/0 = undefined).
cb = plt.colorbar(plot5, ax=ax5, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or fracdiff_is_all_nan:
cb.set_ticks([0.0])
if fracdiff_is_all_nan:
Expand Down Expand Up @@ -1713,6 +1719,7 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
# is zero everywhere or NaN everywhere, set a tick in the middle
# of the normalized color range (which will be 0..1).
cb = plt.colorbar(plot0, ax=ax0, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if ref_is_all_zero or ref_is_all_nan:
cb.set_ticks([0.5])
if ref_is_all_nan:
Expand Down Expand Up @@ -1779,6 +1786,7 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
# is zero everywhere or NaN everywhere, set a tick in the middle
# of the normalized color range (which will be 0..1).
cb = plt.colorbar(plot1, ax=ax1, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if dev_is_all_zero or dev_is_all_nan:
cb.set_ticks([0.5])
if dev_is_all_nan:
Expand Down Expand Up @@ -1860,6 +1868,7 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
# is zero everywhere or NaN everywhere, set a tick in the middle
# of the normalized color range (which will be 0..1).
cb = plt.colorbar(plot2, ax=ax2, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or absdiff_is_all_nan:
cb.set_ticks([0.0])
if absdiff_is_all_nan:
Expand Down Expand Up @@ -1917,6 +1926,7 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
# is zero everywhere or NaN everywhere, set a tick in the middle
# of the normalized color range (which will be 0..1).
cb = plt.colorbar(plot3, ax=ax3, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or absdiff_is_all_nan:
cb.set_ticks([0.0])
if absdiff_is_all_nan:
Expand Down Expand Up @@ -1985,6 +1995,7 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
# Use is_absdiff_all_zero to force the plot to show as all gray
# when both Dev is zero and Ref is zero (since 0/0 = undefined).
cb = plt.colorbar(plot4, ax=ax4, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or fracdiff_is_all_nan:
cb.set_ticks([0.0])
if fracdiff_is_all_nan:
Expand All @@ -1995,7 +2006,6 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
if (vmax - vmin) < 0.1 or (vmax - vmin) > 100:
cb.locator = mticker.MaxNLocator(nbins=4)
cb.update_ticks()
cb.set_clim(vmin=vmin, vmax=vmax)
cb.set_label('unitless')

# ==============================================================
Expand Down Expand Up @@ -2041,6 +2051,7 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
# Use is_absdiff_all_zero to force the plot to show as all gray
# when both Dev is zero and Ref is zero (since 0/0 = undefined).
cb = plt.colorbar(plot5, ax=ax5, orientation='horizontal', pad=0.10)
cb.mappable.set_norm(norm)
if absdiff_is_all_zero or fracdiff_is_all_nan:
cb.set_ticks([0.0])
if fracdiff_is_all_nan:
Expand All @@ -2051,7 +2062,6 @@ def compare_zonal_mean(refdata, refstr, devdata, devstr, varlist=None,
if (vmax - vmin) < 0.1 or (vmax - vmin) > 100:
cb.locator = mticker.MaxNLocator(nbins=4)
cb.update_ticks()
cb.set_clim(vmin=vmin, vmax=vmax)
cb.set_label('unitless')

# ==============================================================
Expand Down

0 comments on commit b3a8311

Please sign in to comment.