Proposal: Support explicit mesh coordinates in `xarray.plot.pcolormesh`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- matplotlib, numpy, python
調査の方向性
xarray.plot.pcolormesh のエントリポイントから始め、座標配列をどのように扱うか、その infer_intervals ロジックを確認します。1D および 2D の x_edges/y_edges に対する API とテストを定義し、明示的なエッジが推論をバイパスして Matplotlib に渡されることを保証するとともに、既存のラベリングと FacetGrid の動作を維持します。
索引モデルが issue の本文から書いたものです。
説明
Hi @dcherian @jhamman, @kmuehlbauer
I would like to open a PR to address a current limitation in xarray.plot.pcolormesh regarding the correct display of:
- Irregular 1D grids (non-uniform bin spacing)
- Curvilinear 2D grids (e.g., distorted or geophysical meshes)
Other linked issues:
- https://github.com/pydata/xarray/issues/792
- https://github.com/pydata/xarray/issues/4364
- https://github.com/pydata/xarray/issues/5901
Problem
xarray.plot.pcolormesh assumes that coordinates represent cell centers and internally reconstructs edges (infer_intervals=True logic).
This leads to incorrect rendering when:
- Grid spacing is irregular
- True cell boundaries are known and should be respected
- The grid is curvilinear and defined by 2D vertex coordinates
Such grids are common in many domains:
- Numerical Weather Prediction (NWP) model grids
- Satellite swath data
- Radar products
- Ocean and climate model outputs
- ...
This limitation significantly impacts accurate scientific and cartographic visualization.
Current Workaround
A workaround exists: directly calling matplotlib.pyplot.pcolormesh with explicit edge arrays.
However, this comes with significant drawbacks:
- Loss of
xarray’s high-level plotting API - Loss of automatic labeling and metadata handling
- Loss of
FacetGridsupport (e.g., faceting over time or ensemble dimensions) - More verbose code
In practice, this makes advanced visualization workflows much harder, especially when working with multi-dimensional datasets.
Describe the solution you'd like
I propose adding support for explicitly passing edge coordinates to pcolormesh, following the matplotlib.pyplot.pcolormesh(..., shading="flat") convention.
1D Case
x_edges: shape(n+1,)y_edges: shape(m+1,)
2D Curvilinear Case
x_edges: shape(m+1, n+1)y_edges: shape(m+1, n+1)
When provided, these edge coordinates would:
- Bypass edge inference
- Be passed directly to Matplotlib
- Ensure geometrically correct rendering
I suggest therefore to introduce x_edges and y_edges keyword arguments to pcolormesh
da.pcolormesh(..., x_edges=..., y_edges=...)
Under this approach:
xandymust still be specified- If
x_edges/y_edgesare provided, edge inference is skipped - The edges /mesh arrays are passed directly to Matplotlib
Before starting work on the PR, I would appreciate your feedback on:
- Whether you are in favor of adding this capability
- Preferred API design direction
- Any architectural considerations to keep in mind
Minimal, reproducible example of the problem
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
####-----------------------------------------------------------------
#### Rectilinear grid (1D case)
# Regular edges
x_edges = np.arange(0, 4) # 4 edges
y_edges = np.arange(6, 9) # 3 edges
# Irregular edges
x_edges = np.array([0, 1, 4, 10]) # 4 edges
y_edges = np.array([30, 50, 100]) # 3 edges
# Plot limits
xlim = x_edges.min(), x_edges.max()
ylim = y_edges.min(), y_edges.max()
# Compute centers
x_centers = 0.5 * (x_edges[:-1] + x_edges[1:])
y_centers = 0.5 * (y_edges[:-1] + y_edges[1:])
# Create 2D data
Xc, Yc = np.meshgrid(x_centers, y_centers)
data = np.sin(Xc/2) * np.cos(Yc/5)
# Build Dataset
ds = xr.Dataset(
{
"data": (("y", "x"), data)
},
coords={
"x": x_centers,
"y": y_centers,
}
)
####-----------------------------------------------------------------
#### Curvilinear grid (2D case)
# Create 2D irregular edges from your 1D irregular edge
x_edges, y_edges = np.meshgrid(x_edges, y_edges)
# Add distortion (to make geometry clearly non-rectangular)
x_edges = x_edges + 0.8 * np.sin(y_edges / 40)
y_edges = y_edges + 3.0 * np.cos(x_edges / 5)
# Compute cell centers from corner average
Xc2 = 0.25 * (
x_edges[:-1, :-1] +
x_edges[1:, :-1] +
x_edges[:-1, 1:] +
x_edges[1:, 1:]
)
Yc2 = 0.25 * (
y_edges[:-1, :-1] +
y_edges[1:, :-1] +
y_edges[:-1, 1:] +
y_edges[1:, 1:]
)
data2 = np.sin(Xc2/2) * np.cos(Yc2/5)
ds = xr.Dataset(
{
"data": (("y", "x"), data2)
},
coords={
"x": (("y", "x"), Xc2),
"y": (("y", "x"), Yc2),
}
)
# Plot limits from true edges
xlim = x_edges.min(), x_edges.max()
ylim = y_edges.min(), y_edges.max()
#--------------------------------------------------------------------
#### XARRAY PCOLORMESH PLOTTING IS CURRENTLY WRONG
p = ds["data"].plot.pcolormesh(x="x", y="y", add_colorbar=False)
p.axes.set_title("")
p.axes.set_xlabel("")
p.axes.set_ylabel("")
p.axes.set_xlim(*xlim)
p.axes.set_ylim(*ylim)
#--------------------------------------------------------------------
#### PLOT CORRECTLY WITH MATPLOTLIB PCOLORMESH
fig, ax = plt.subplots(1,1)
ax.pcolormesh(x_edges, y_edges, data)
p.axes.set_title("")
p.axes.set_xlabel("")
p.axes.set_ylabel("")
p.axes.set_xlim(*xlim)
p.axes.set_ylim(*ylim)
- 主要言語
- Python
- スター
- 4.2k
- フォーク
- 1.5k
- 平均マージ
- 2日 21時間
- マージ済み PR(30日)
- 74
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
pydata/xarray のほかの issue
-
xarray removes axis grid from pcolormesh対応中かも @Sealdot が今日担当しました。 オープンbug needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
`DataTree.map_over_datasets` behavior can be obscure sometimes対応中かも @mohansree14 が 24 日前に担当しました。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
pydata/xarray#11813 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identical対応中かも @SohamXYZDev が 29 日前に担当しました。 オープンbug needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
pydata/xarray#11777 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)対応中かも @d-chambers が 60 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
pydata/xarray#11598 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
DataTree constructor error message says dict even though dict is not accepted対応中かも @aryansk が 100 日前に担当しました。 オープンbug contrib-good-first-issue topic-DataTree topic-error reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
pydata/xarray#11514 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
abetlen/llama-cpp-python#2424 ·
メンテナーはふだん 1 日以内に返信
-
Bug IronPython Python 3
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
pyrevitlabs/pyRevit#4184 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信