Skip to content

Attach plot_prediction colorbar to the data mesh - #903

Open
nikhil3495 wants to merge 4 commits into
mllam:mainfrom
nikhil3495:fix/plot-prediction-colorbar
Open

nikhil3495 wants to merge 4 commits into
mllam:mainfrom
nikhil3495:fix/plot-prediction-colorbar

Conversation

@nikhil3495

@nikhil3495 nikhil3495 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes

plot_prediction built its shared colorbar from axes[0].collections[0]. Cartopy adds coastlines and borders as FeatureArtist collections before plot_on_axis calls pcolormesh, so the colorbar used the coastline artist's default 0-1 norm. Every example-prediction plot logged during training and evaluation showed 0-1 tick labels instead of the data range. The fix passes the mesh returned by plot_on_axis to fig.colorbar, as plot_spatial_error already does.

The existing test_plot_prediction stubs out coastlines/add_feature, which hid the bug. The new test_plot_prediction_colorbar_uses_data_range restores the real methods (creating the artists needs no download) and asserts that the colorbar spans vrange. It fails on unmodified main (colorbar limits (0.0, 1.0), expected (250, 350)) and passes with the fix; I checked both by stashing neural_lam/vis.py. The full tests/test_plotting.py passes (28 tests), and the dummydata cases of tests/test_training.py pass. Not run here: the mdp cases (local SSL failures downloading the ECMWF test data) and the lychee pre-commit hook (SKIP=lychee, a local hook-config error on this Windows setup).

Note: #636 makes the same one-line change as part of its boundary-overlay feature. This PR fixes only the bug, so it can land on its own; either merge order conflicts trivially.

Issue Link

closes #902

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (Addition or improvements to documentation)

Checklist before requesting a review

  • My branch is up-to-date with the target branch
  • I have performed a self-review of my code
  • For any new/modified functions/classes I have added docstrings that clearly describe its purpose, expected inputs and returned values
  • I have placed in-line comments to clarify the intent of any hard-to-understand passages of my code
  • I have updated the README to cover introduced code changes (not applicable)
  • If this PR changes contributor workflow, I've updated CONTRIBUTING.md / AGENTS.md (not applicable)
  • I have added tests that prove my fix is effective or that my feature works
  • I have given the PR a name that clearly describes the change, written in imperative form
  • I have requested a reviewer and an assignee (no write access; maintainers please assign)

Author checklist after completed review

  • I have added a line to the CHANGELOG describing this change, in a section reflecting type of change (Fixed)

nikhil3495 and others added 2 commits November 23, 2026 12:50
plot_prediction built its shared colorbar from axes[0].collections[0].
cartopy adds coastlines and borders as FeatureArtist collections before
plot_on_axis draws the pcolormesh, so the colorbar used the coastline
artist's default 0-1 norm and its ticks never matched the plotted data.
Use the mesh returned by plot_on_axis instead, as plot_spatial_error
already does.

The existing test stubs out coastlines and add_feature, which hid this;
the new test restores them (creating the artists needs no download).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nikhil3495

Copy link
Copy Markdown
Contributor Author

Open PR #636 replaces the same fig.colorbar(axes[0].collections[0], ...) line with the mesh returned by plot_on_axis, which incidentally fixes this too.
Whichever lands first makes the other a trivial rebase; this one also adds a regression test that draws real coastlines.

@AshNicolus AshNicolus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right: plot_on_axis returns the data mesh (vis.py:477), the same object plot_spatial_error already passes, so the colorbar can no longer pick up the coastline/border artists or the boundary overlay mesh.

flowchart LR
    C["ax.collections =<br/>[coastlines, borders, mesh]"] --> O["collections[0]<br/>(main)"] --> W["norm 0 to 1 ❌"]
    M["mesh returned by<br/>plot_on_axis"] --> N["meshes[0]<br/>(this PR)"] --> OK["norm vmin to vmax ✅"]
Loading

Optional nit: test_plot_prediction (tests/test_plotting.py:144-147) still reads ax.collections[0].norm. It passes only because the autouse fixture stubs coastlines/add_feature; without the stubs it would check the coastline artist. Picking the QuadMesh out of ax.collections would keep it in step with this fix.

collections[0] is only the data mesh because the autouse fixture stubs
out coastlines/add_feature; with real cartopy features it is a
FeatureArtist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nikhil3495

Copy link
Copy Markdown
Contributor Author

@AshNicolus Good catch, confirmed: with real features collections[0] is a FeatureArtist. test_plot_prediction now picks the QuadMesh out of ax.collections instead (41bb114).

@AshNicolus AshNicolus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified 41bb114: GeoQuadMesh subclasses QuadMesh, and plot_on_axis draws the data mesh (vis.py:423) before the optional boundary overlay (vis.py:445), so the first QuadMesh in ax.collections is always the data mesh, with or without real coastlines and borders.

flowchart LR
    C["ax.collections"] --> F["FeatureArtist<br/>coastlines, borders"]
    C --> D["data mesh<br/>first QuadMesh"]
    C --> B["boundary overlay<br/>QuadMesh, optional"]
    D --> T["norm == vrange ✅"]
Loading

@nikhil3495

Copy link
Copy Markdown
Contributor Author

Thanks for checking 41bb114, @AshNicolus.

The failing link-check-job is unrelated to this diff: lychee got transient 503s from github.com for three existing blob/main links in docs/getting-started/installation.md and docs/user-guide/datastores.md, files this PR doesn't touch. The same 503s hit fix/mlflow-end-run-on-finalize and passed on its next run, main is green, and Documentation / Link check passes here. Could someone re-run the job?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plot_prediction colorbar shows a 0-1 range instead of the data range

2 participants