Skip to content

[Bug]: Gouraud shading for plt.tripcolor is slightly off #28769

@spanag

Description

@spanag

Bug summary

When plt.tripcolor(... shading='gouraud') is used, triangles are not interpolated exactly as they should, there is a small but visible difference at times.
A simple way to see it is to draw a rectangle with high aspect ratio, where each end has a different color (same in both vertices of each end).

Code for reproduction

from matplotlib import pyplot as plt
# Make a figure with a gouraud shaded band
fig = plt.figure(figsize=[6.4, 4.8], dpi=100) # increase dpi for detail
z = .05 # half width. try also: .02, .01
plt.tripcolor([-1,-1,+1,+1], [-z,+z,-z,+z],  [0,0,1,1], 
              shading='gouraud', cmap='gray');
plt.xlim((-1,+1)); plt.ylim((-1,+1)); 
plt.axis('off'); fig.patch.set_facecolor('k')

# Create a bytes buffer to save the plot
import io, numpy as np; from PIL import Image
buf = io.BytesIO()
plt.savefig(buf, format='png') # format doesn't affect the result
buf.seek(0)
# Open the PNG image from the buffer and convert it to a NumPy array
image = np.array(Image.open(buf),dtype='float32')
# Close the buffer
buf.close()

plt.show() # after saving

# Inspect the RGB array
img = image[...,1]/255; h,w = img.shape
plt.figure(); plt.title('Brightness per pixel column')
for s in [0.3, 0.5, 0.7]:
    plt.plot(img[:,round(s*w)], '.');
plt.xlim((.504-z/1.9)*h,(.504+z/1.9)*h)
plt.show()

Actual outcome

The shading is different for the two triangles, when linear interpolation should have captured the gradient exactly. It is visibly discontinuous along the diagonal of the rectangle where the triangles meet.

reality

Expected outcome

A smooth linear gradient should be shown.

expectation

Additional information

Numerical inspection shows that there is a steady difference in brightness between the triangles.

The effect also appears for different (non-rect) vertex positions, values, colormaps etc.
Interestingly enough, it vanishes as the aspect ratio approaches 1.

I wonder if the different number of vertices from the 'bright' side per triangle (1 or 2) causes the offset.

For comparison, this rectangle with a linear gradient is shown smoothly with no seam, with classic OpenGL:

float hh = -.05;
glBegin (GL_TRIANGLE_STRIP);
glColor3f (0.0f, 0.0f, 0.0f);   glVertex2f (-1.0f, -hh);
glColor3f (1.0f, 1.0f, 1.0f);   glVertex2f (+1.0f, -hh);
glColor3f (0.0f, 0.0f, 0.0f);   glVertex2f (-1.0f, +hh);
glColor3f (1.0f, 1.0f, 1.0f);   glVertex2f (+1.0f, +hh);
glEnd ();

Operating system

Ubuntu

Matplotlib Version

3.9.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.11.4

Jupyter version

lab 4.0.5

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions