FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

pcolormesh: masked parts of array render black when saving to PDF with Gouraud shading · Issue #11769 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

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

Activity

  1. anntzer commented on Sep 18, 2018

    Contributor

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

  2. jkseppan commented on Aug 5, 2020

    Member

    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.

  3. QuLogic commented on Aug 5, 2020

    Member

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

  4. jkseppan commented on Aug 6, 2020

    Member

    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).

  5. 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:

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

    then the outputs look like here:

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

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


      Back | FazBrowse Home | New Git URL