If a 2D numpy array has NaNs (thereby interpreted by pcolormesh as masked), and pcolormesh is used to plot the array with Gouraud shading, then when saving to a PDF, instead of rendering those cells as transparent (making anything underneath visible), the cells in question are rendered as black. This does not happen with rasterized plots, plots saved to PNG, or plots displayed interactively. This appears to be independent of backend. Simply using imshow is not an option if the data to be plotted is on a non-uniform grid.
Code for reproduction
importnumpyasnpimportmatplotlib.pyplotaspltx,y=np.meshgrid(np.linspace(0.,100.),np.linspace(0.,100.))
map1=np.sin(2*np.pi*x/100.)*np.sin(2*np.pi*y/100.)
map2=x*ymap2[20:30,20:30] =np.nan# Plot with Gouraud shading and write to PDFplt.pcolormesh(x,y,map1,shading='Gouraud',cmap='magma')
plt.pcolormesh(x,y,map2,shading='Gouraud',cmap='viridis',vmin=np.nanmin(map2),vmax=np.nanmax(map2))
plt.savefig("mwe_gouraud.pdf",bbox_inches='tight')
# Plot with no shading and write to PDFplt.pcolormesh(x,y,map1,shading='Gouraud',cmap='magma')
plt.pcolormesh(x,y,map2,cmap='viridis',vmin=np.nanmin(map2),vmax=np.nanmax(map2))
plt.savefig("mwe_noshading.pdf",bbox_inches='tight')
# Plot with Gouraud shading and write to PNGplt.pcolormesh(x,y,map1,shading='Gouraud',cmap='magma')
plt.pcolormesh(x,y,map2,shading='Gouraud',cmap='viridis',vmin=np.nanmin(map2),vmax=np.nanmax(map2))
plt.savefig("mwe_gouraud.png",bbox_inches='tight')
Actual outcome
With Gouraud shading, written to PDF:
Without Gouraud shading, written to PDF:
With Gouraud shading, written to PNG:
Expected outcome
All three outputs should show map1 in the central rectangle where values have been set to np.nan (i.e. the masked values should be transparent).
I think this was solved by #14414. Currently the pdf backend produces results similar to the png backend. Note that there is some discussion in #8802 on what the correct output should be.
Not exactly the same, but not a black rectangle either. I think interpolating values towards a transparent black isn't quite right, but see #8802 (and if someone who finds this issue has opinions on how the result should look, please comment there).
Activity
anntzer commented on Sep 18, 2018
(As a reference point: mplcairo does not suffer from this issue, so it "should" be fixable :))
jkseppan commented on Aug 5, 2020
I think this was solved by #14414. Currently the pdf backend produces results similar to the png backend. Note that there is some discussion in #8802 on what the correct output should be.
QuLogic commented on Aug 5, 2020
Are you sure? It doesn't look the same to me. Maybe it depends on viewer?
jkseppan commented on Aug 6, 2020
Not exactly the same, but not a black rectangle either. I think interpolating values towards a transparent black isn't quite right, but see #8802 (and if someone who finds this issue has opinions on how the result should look, please comment there).
slayoo commented on Nov 9, 2022
just came across this bug working with matplotlib 3.5.2, updated to 3.6.2 and the issue is still there - exactly as described in the original post.
If one adds:
then the outputs look like here:
