diff --git a/mslib/mswms/generics.py b/mslib/mswms/generics.py index b5bb20697..23ea01e90 100644 --- a/mslib/mswms/generics.py +++ b/mslib/mswms/generics.py @@ -315,6 +315,30 @@ def get_log_levels(cmin, cmax, levels=None): return clev +CBAR_LABEL_FORMATS = { + "log": "%.3g", + "log_ice_cloud": "%.0E", +} + + +def get_cbar_label_format(style, maxvalue): + if style in CBAR_LABEL_FORMATS: + return CBAR_LABEL_FORMATS[style] + if 100 <= maxvalue < 10000.: + label_format = "%4i" + elif 10 <= maxvalue < 100.: + label_format = "%.1f" + elif 1 <= maxvalue < 10.: + label_format = "%.2f" + elif 0.1 <= maxvalue < 1.: + label_format = "%.3f" + elif 0.01 <= maxvalue < 0.1: + label_format = "%.4f" + else: + label_format = "%.3g" + return label_format + + def _style_default(_dataname, _style, cmin, cmax, cmap, _data): clev = np.linspace(cmin, cmax, 16) norm = matplotlib.colors.BoundaryNorm(clev, cmap.N) diff --git a/mslib/mswms/mpl_hsec_styles.py b/mslib/mswms/mpl_hsec_styles.py index 2e43e9b4e..bbec22d65 100644 --- a/mslib/mswms/mpl_hsec_styles.py +++ b/mslib/mswms/mpl_hsec_styles.py @@ -74,7 +74,7 @@ from matplotlib import patheffects from mslib.mswms.mpl_hsec import MPLBasemapHorizontalSectionStyle -from mslib.mswms.utils import get_cbar_label_format, make_cbar_labels_readable +from mslib.mswms.utils import make_cbar_labels_readable import mslib.mswms.generics as generics from mslib.utils import thermolib from mslib.utils.units import convert_to @@ -88,6 +88,7 @@ class HS_GenericStyle(MPLBasemapHorizontalSectionStyle): styles = [ ("auto", "auto colour scale"), ("autolog", "auto logcolour scale"), ] + cbar_format = None def _plot_style(self): bm = self.bm @@ -123,13 +124,14 @@ def _plot_style(self): # Format for colorbar labels cbar_label = self.title - cbar_format = get_cbar_label_format(self.style, np.median(np.abs(clevs))) + if self.cbar_format is None: + cbar_format = generics.get_cbar_label_format(self.style, np.median(np.abs(clevs))) + else: + cbar_format = self.cbar_format if not self.noframe: - cbar = self.fig.colorbar(tc, fraction=0.05, pad=0.08, shrink=0.7, - label=cbar_label, format=cbar_format, ticks=ticks, extend="both") - cbar.set_ticks(clevs) - cbar.set_ticklabels(clevs) + self.fig.colorbar(tc, fraction=0.05, pad=0.08, shrink=0.7, + label=cbar_label, format=cbar_format, ticks=ticks, extend="both") else: axins1 = mpl_toolkits.axes_grid1.inset_locator.inset_axes( ax, width="3%", height="40%", loc=cbar_location) diff --git a/mslib/mswms/mpl_vsec_styles.py b/mslib/mswms/mpl_vsec_styles.py index 1b20417b3..a1cf21148 100644 --- a/mslib/mswms/mpl_vsec_styles.py +++ b/mslib/mswms/mpl_vsec_styles.py @@ -38,7 +38,7 @@ import numpy as np from mslib.mswms.mpl_vsec import AbstractVerticalSectionStyle -from mslib.mswms.utils import get_cbar_label_format, make_cbar_labels_readable +from mslib.mswms.utils import make_cbar_labels_readable import mslib.mswms.generics as generics from mslib.utils import thermolib from mslib.utils.units import convert_to @@ -52,6 +52,7 @@ class VS_GenericStyle(AbstractVerticalSectionStyle): styles = [ ("auto", "auto colour scale"), ("autolog", "auto log colour scale"), ] + cbar_format = None def _plot_style(self): ax = self.ax @@ -97,7 +98,11 @@ def _plot_style(self): self._latlon_logp_setup() # Format for colorbar labels - cbar_format = get_cbar_label_format(self.style, np.abs(clevs).max()) + if self.cbar_format is None: + cbar_format = generics.get_cbar_label_format(self.style, np.median(np.abs(clevs))) + else: + cbar_format = self.cbar_format + cbar_label = self.title # Add colorbar. diff --git a/mslib/mswms/utils.py b/mslib/mswms/utils.py index 4811dab5c..03196527d 100644 --- a/mslib/mswms/utils.py +++ b/mslib/mswms/utils.py @@ -28,24 +28,6 @@ import matplotlib -def get_cbar_label_format(style, maxvalue): - format = "%.3g" - if style != "log": - if 100 <= maxvalue < 10000.: - format = "%4i" - elif 10 <= maxvalue < 100.: - format = "%.1f" - elif 1 <= maxvalue < 10.: - format = "%.2f" - elif 0.1 <= maxvalue < 1.: - format = "%.3f" - elif 0.01 <= maxvalue < 0.1: - format = "%.4f" - if style == 'log_ice_cloud': - format = "%.0E" - return format - - def make_cbar_labels_readable(fig, axs): """ Adjust font size of the colorbar labels and put a white background behind them