Skip to content

Commit

Permalink
Make colorbar ticks format in generics plot configurable (#1829)
Browse files Browse the repository at this point in the history
* Make colorbar ticks format in generics plot configurable

Fix #1828

* make vsec consistent with hsec

* fix

* fix
  • Loading branch information
joernu76 authored Jul 25, 2023
1 parent 7b48369 commit 3ea5497
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
24 changes: 24 additions & 0 deletions mslib/mswms/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions mslib/mswms/mpl_hsec_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions mslib/mswms/mpl_vsec_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 0 additions & 18 deletions mslib/mswms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ea5497

Please sign in to comment.