pydata / pydata/xarray

Maximum value not always included in pcolormesh output when levels are requested

Open
#10,911 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug contrib-help-wanted topic-plotting
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

When plotting data with "pcolormesh" and requesting levels, if the calculated "vmax" if equal to the upper bound of the last bin, it is not included in the map, as if it was "over" or "missing".

What did you expect to happen?

I expected values == vmax to be painted with the color of the last bin, which is what contourf does.

Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!

import xarray as xr
xr.show_versions()
# your reproducer code ...

import xarray as xr
ds = xr.tutorial.open_dataset('air_temperature')
air = ds.air.isel(time=0).round(-1)
air.plot(levels=5)
Steps to reproduce

The example above gives the image below:

Image

You can see that with the heavy discretization I imposed, the values at the bottom are equal to the calculated "vmax" and are not shown.

MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

Anything else we need to know?

Matplotlib's from_levels_and_colors, it is said that :

The quantization levels used to construct the BoundaryNorm. Value v is quantized to level i if lev[i] <= v < lev[i+1].

So this explains why xarray's implementation gives no color. However, the result seems to be unexpected and incoherent with the output of contourf. I think it is logic to expect the maximum value to be included in the plot when simply passing levels as a scalar.

Environment

INSTALLED VERSIONS

commit: None
python: 3.13.3 | packaged by conda-forge | (main, Apr 14 2025, 20:44:03) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.17.4-200.fc42.x86_64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: fr_CA.UTF-8
LOCALE: ('fr_CA', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.2

xarray: 2025.10.2.dev18+ge49cfc4f2
pandas: 2.2.3
numpy: 2.2.6
scipy: 1.16.1
netCDF4: 1.7.2
pydap: None
h5netcdf: 1.6.1
h5py: 3.13.0
zarr: None
cftime: 1.6.4
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.5.0
dask: 2025.4.1
distributed: 2025.4.1
matplotlib: 3.10.3
cartopy: None
seaborn: None
numbagg: 0.9.0
fsspec: 2025.3.2
cupy: None
pint: 0.25
sparse: None
flox: 0.10.5
numpy_groupies: 0.11.2
setuptools: 80.1.0
pip: 25.1.1
conda: None
pytest: 8.3.5
mypy: 1.15.0
IPython: 9.2.0
sphinx: 8.2.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided xarray pcolormesh example with scalar levels and inspect the plotting path that constructs the Matplotlib normalization. Compare the vmax boundary behavior with contourf and Matplotlib's from_levels_and_colors documentation. Done means values equal to the calculated vmax receive the final bin color, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.