Skip to content

Commit

Permalink
Fix fencepost problem with colorbar bins
Browse files Browse the repository at this point in the history
This makes the numbers on the colourbar labels much rounder if the
range is a multiple of 20.

This comes from the fencepost problem, which describes how you need
n+1 dividers to support n bins. For example, imagine a fence with 5
segments, like so:

    |###|###|###|###|###|

Notice that we have 6 posts (denoted with | characters) for our 5
fence segments. This is the fence post problem.
  • Loading branch information
jfrost-mo committed Feb 14, 2025
1 parent 4b60f87 commit 8d4b82d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/CSET/operators/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _colorbar_map_levels(cube: iris.cube.Cube):
vmin, vmax = var_colorbar["min"], var_colorbar["max"]
logging.debug("Using min and max for %s colorbar.", varname)
# Calculate levels from range.
levels = np.linspace(vmin, vmax, 20)
levels = np.linspace(vmin, vmax, 21)
norm = None
return cmap, levels, norm
except KeyError:
Expand Down Expand Up @@ -709,7 +709,7 @@ def _plot_and_save_histogram_series(
vmin = 0
vmax = 400 # Manually set vmin/vmax to override json derived value.
else:
bins = np.linspace(vmin, vmax, 50)
bins = np.linspace(vmin, vmax, 51)

# Reshape cube data into a single array to allow for a single histogram.
# Otherwise we plot xdim histograms stacked.
Expand Down
6 changes: 3 additions & 3 deletions tests/operators/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_colorbar_map_levels(cube, tmp_working_dir):
"""Colorbar definition is found for cube."""
cmap, levels, norm = plot._colorbar_map_levels(cube)
assert cmap == mpl.colormaps["RdYlBu_r"]
assert (levels == np.linspace(223, 323, 20)).all()
assert (levels == np.linspace(223, 323, 21)).all()
assert norm is None


Expand All @@ -108,7 +108,7 @@ def test_colorbar_map_levels_name_fallback(cube, tmp_working_dir):
cube.standard_name = None
cmap, levels, norm = plot._colorbar_map_levels(cube)
assert cmap == mpl.colormaps["RdYlBu_r"]
assert (levels == np.linspace(223, 323, 20)).all()
assert (levels == np.linspace(223, 323, 21)).all()
assert norm is None


Expand All @@ -128,7 +128,7 @@ def test_colorbar_map_levels_pressure_level(transect_source_cube, tmp_working_di
cube_250hPa = transect_source_cube.extract(iris.Constraint(pressure=250))
cmap, levels, norm = plot._colorbar_map_levels(cube_250hPa)
assert cmap == mpl.colormaps["RdYlBu_r"]
assert (levels == np.linspace(200, 240, 20)).all()
assert (levels == np.linspace(200, 240, 21)).all()
assert norm is None


Expand Down

0 comments on commit 8d4b82d

Please sign in to comment.