Skip to content

pcolormesh: masked parts of array render black when saving to PDF with Gouraud shading #11769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alphaparrot opened this issue Jul 25, 2018 · 5 comments

Comments

@alphaparrot
Copy link

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:
Output with Gouraud shading to PDF

Without Gouraud shading, written to PDF:
Output with no shading to PDF

With Gouraud shading, written to PNG:
Output with Gouraud shading 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

@anntzer
Copy link
Contributor

anntzer commented Sep 18, 2018

(As a reference point: mplcairo does not suffer from this issue, so it "should" be fixable :))

@jkseppan
Copy link
Member

jkseppan commented 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.

@jkseppan jkseppan closed this as completed Aug 5, 2020
@QuLogic
Copy link
Member

QuLogic commented Aug 5, 2020

Are you sure? It doesn't look the same to me. Maybe it depends on viewer?

@jkseppan
Copy link
Member

jkseppan commented 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
Copy link

slayoo commented 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:

plt.xlim(45, 55) 
plt.ylim(45, 55)

then the outputs look like here:
image

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

No branches or pull requests

6 participants