You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ENH]: Consider bilinear interpolation for pcolormesh #32518
Currently the only "interpolating" shading of pcolormesh() is "gouraud". This is somewhat special: It splits the quadrilateral into two triangles and uses Gouraud shading for each. If the z values of the corners are not in a plane, this produces characteristic patterns and the result will depend on how the two triangles are chosen; e.g.
changed the title [-][ENH]: Condsider bilinear interpolation for pcolormesh[/-][+][ENH]: Consider bilinear interpolation for pcolormesh[/+]on Oct 22, 2026
This would be simple to implement when X/Y are not provided, but proper bilinear interpolation is mathematically complicated for arbitrary X/Y input (e.g., an irregular grid where the quadrilaterals are not parallelograms).
It would be ok to only do this for rectangular / parallelogram quadrilaterals and error out otherwise.
There may also be reasonable invertible transformations that transform an arbitrary quadrilateral to a “closest” regular quadrilateral on which we could apply the bilinear interpolation and transform the result back. But that would need a closer look.
Looking at _MeshData._convert_mesh_to_triangles(), the current Gouraud implementation creates four triangles per quad around an averaged center, and interpolates the mapped RGBA colors within those triangles. This gives a piecewise-linear color surface rather than a bilinear one across the whole quad, which explains the triangle/fan-shaped patterns. The center color is already the average of the corner colors, so changing that average alone would not address the limitation.
Following the suggested scope, I propose starting with an opt-in shading="bilinear" mode for rectangles/parallelograms, including grids generated when X/Y are omitted, and raising a clear error for unsupported or degenerate quadrilaterals. Existing Gouraud behavior would remain unchanged.
The implementation would use four-corner bilinear weights in each quad’s local coordinates, with numerical and rendering tests for nonplanar corner values, supported geometry, and masking/transparency.
Before choosing the rendering approach, should this interpolate mapped RGBA colors, consistent with Gouraud, or scalar data before norm/cmap? Also, would a rasterized implementation—including image output within vector formats—be acceptable for an initial version?
Someone has to sit down and decide how exactly i.e. in which coordinates the bilinear interpolation should be done. In particular this should compare the properties and thus advantages/disadvantages of the different methods. Spoiler: From a quick reseach, I'm inclined to say that we want isoparametric bilinear interpolation - but that's to be confirmed.
I question if this is worth the effort. SciPy has a pretty complete suite of 2-D interpolation routines that users can and should tune to their own uses. If they want to interpolate further they can put on a regular grid and use the image-based filters. I'm not sure I think Matplotlib should duplicate that past the turnkey Gouraud solution we already provide?
One can debate whether we should support interpolation at all.
From a practical standpoint this is similar to using markers vs lines in plot(). There are use cases where the (linear) interpolation between data points is reasonable and helps the visualization. There are two points in favor of us doing this
for better or worse, we have shading=gouraud, which is an interpolation with quite peculiar properties. But since it's the only built-in solution people will use it, whether it's a good choice or not.
Interpolation done on the user side must pre-determine a suitable grid. It cannot adapt with zooming. It's hard to find a suitable scale. If you are below the pixel resolution, you'll still see steps. If you are close to the pixel resolution, you will get rasterization artifacts. Matching the interpolated grid so that each element is exactly one pixel would be extremely tedious. Additionally, I suspect that bilinear interpolation inside matplotlib is much cheaper than an external interpolation to a fine-grained grid and then plotting this; but t.b.d.
I'd over interpolate with an appropriate kernel and then use gouraud if I was concerned about getting smooth results on a zoom. But usually if you are zooming you want to see data not interpolations.
Activity
[-][ENH]: Condsider bilinear interpolation for pcolormesh[/-][+][ENH]: Consider bilinear interpolation for pcolormesh[/+]ayshih commentedon Oct 22, 2026
This would be simple to implement when X/Y are not provided, but proper bilinear interpolation is mathematically complicated for arbitrary X/Y input (e.g., an irregular grid where the quadrilaterals are not parallelograms).
timhoffm commentedon Oct 23, 2026
It would be ok to only do this for rectangular / parallelogram quadrilaterals and error out otherwise.
There may also be reasonable invertible transformations that transform an arbitrary quadrilateral to a “closest” regular quadrilateral on which we could apply the bilinear interpolation and transform the result back. But that would need a closer look.
Adithya6721 commentedon Nov 2, 2026
I’d like to work on adding bilinear shading here.
Looking at _MeshData._convert_mesh_to_triangles(), the current Gouraud implementation creates four triangles per quad around an averaged center, and interpolates the mapped RGBA colors within those triangles. This gives a piecewise-linear color surface rather than a bilinear one across the whole quad, which explains the triangle/fan-shaped patterns. The center color is already the average of the corner colors, so changing that average alone would not address the limitation.
Following the suggested scope, I propose starting with an opt-in shading="bilinear" mode for rectangles/parallelograms, including grids generated when X/Y are omitted, and raising a clear error for unsupported or degenerate quadrilaterals. Existing Gouraud behavior would remain unchanged.
The implementation would use four-corner bilinear weights in each quad’s local coordinates, with numerical and rendering tests for nonplanar corner values, supported geometry, and masking/transparency.
Before choosing the rendering approach, should this interpolate mapped RGBA colors, consistent with Gouraud, or scalar data before norm/cmap? Also, would a rasterized implementation—including image output within vector formats—be acceptable for an initial version?
timhoffm commentedon Nov 12, 2026
This is not yet ready to work on.
Someone has to sit down and decide how exactly i.e. in which coordinates the bilinear interpolation should be done. In particular this should compare the properties and thus advantages/disadvantages of the different methods. Spoiler: From a quick reseach, I'm inclined to say that we want isoparametric bilinear interpolation - but that's to be confirmed.
jklymak commentedon Nov 13, 2026
I question if this is worth the effort. SciPy has a pretty complete suite of 2-D interpolation routines that users can and should tune to their own uses. If they want to interpolate further they can put on a regular grid and use the image-based filters. I'm not sure I think Matplotlib should duplicate that past the turnkey Gouraud solution we already provide?
timhoffm commentedon Nov 16, 2026
One can debate whether we should support interpolation at all.
From a practical standpoint this is similar to using markers vs lines in
plot(). There are use cases where the (linear) interpolation between data points is reasonable and helps the visualization. There are two points in favor of us doing thisshading=gouraud, which is an interpolation with quite peculiar properties. But since it's the only built-in solution people will use it, whether it's a good choice or not.jklymak commentedon Nov 17, 2026
I'd over interpolate with an appropriate kernel and then use gouraud if I was concerned about getting smooth results on a zoom. But usually if you are zooming you want to see data not interpolations.