Description
Bug report
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
import numpy as np
import matplotlib.pyplot as plt
x,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*y
map2[20:30,20:30] = np.nan
# Plot with Gouraud shading and write to PDF
plt.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 PDF
plt.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 PNG
plt.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).
Matplotlib version
- Operating system: Ubuntu 16.04
- Matplotlib version: 2.2.2
- Matplotlib backend: All
- Python version: 2.7.9
- Jupyter version (if applicable): N/A
- Other libraries: NumPy 1.14.1
matplotlib installed via pip