Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
Whenplt.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
frommatplotlibimportpyplotasplt# Make a figure with a gouraud shaded bandfig=plt.figure(figsize=[6.4,4.8],dpi=100)# increase dpi for detailz=.05# half width. try also: .02, .01plt.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 plotimportio,numpyasnp;fromPILimportImagebuf=io.BytesIO()plt.savefig(buf,format='png')# format doesn't affect the resultbuf.seek(0)# Open the PNG image from the buffer and convert it to a NumPy arrayimage=np.array(Image.open(buf),dtype='float32')# Close the bufferbuf.close()plt.show()# after saving# Inspect the RGB arrayimg=image[...,1]/255;h,w=img.shapeplt.figure();plt.title('Brightness per pixel column')forsin [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.
Expected outcome
A smooth linear gradient should be shown.
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 approaches1
.
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:
floathh=-.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