Skip to content

[ENH]: Consider bilinear interpolation for pcolormesh #32518

Open

Description

@timhoffm

Problem

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.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(6)
y = np.arange(6)
X, Y = np.meshgrid(x, y)
Z = X + 2*Y
Z[2,3] = 15
plt.pcolormesh(X, Y, Z, shading="gouraud", edgecolors='red', linewidth=2)
plt.show()
Image

Proposed solution

It would be nice to additionally have a simple bilinear interpolation.

Activity

  1. changed the title [-][ENH]: Condsider bilinear interpolation for pcolormesh[/-] [+][ENH]: Consider bilinear interpolation for pcolormesh[/+] on Oct 22, 2026
  2. ayshih commented on Oct 22, 2026

    @ayshih
    Contributor

    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).

  3. timhoffm commented on Oct 23, 2026

    @timhoffm
    MemberAuthor

    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.

  4. Adithya6721 commented on Nov 2, 2026

    @Adithya6721

    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?

  5. timhoffm commented on Nov 12, 2026

    @timhoffm
    MemberAuthor

    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.

  6. jklymak commented on Nov 13, 2026

    @jklymak
    Member

    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?

  7. timhoffm commented on Nov 16, 2026

    @timhoffm
    MemberAuthor

    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.
  8. jklymak commented on Nov 17, 2026

    @jklymak
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions