Skip to content

matplotlib 3D surface - gaps / holes in surface #8222

Closed

Description

@fmevers

Bug report

Bug summary

A grid is interpolated with scipy's griddata() and contains values and NaNs. However, when the grid is plotted with mplot3d's plot_surface() there are gaps in the surface (see upper plot). The lower the view height, the more of such gaps are rendered. When the grid is plotted in 2D with imshow() there are no gaps (see lower plot).

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

Z = np.loadtxt('data.txt')
X = np.linspace(0,3,301)
Y = np.linspace(0,3,301)
X = np.array([X,]*(301)).transpose()
Y = np.array([Y,]*(301))
fig = plt.figure(figsize=(15,15), dpi=100)
ax = fig.add_subplot(2, 1, 1, projection='3d')
ax.view_init(40,300)
surf = ax.plot_surface(X, Y, Z, cmap='jet', rstride=1, cstride=1, antialiased=False, shade=False, alpha=1.0, linewidth=0, vmin=0.25, vmax=0.35);

ax.invert_yaxis()
ax.dist = 11

ax = fig.add_subplot(2, 1, 2)
plt.imshow(Z, cmap='jet', vmin=0.25, vmax=0.35, origin='lower');

plt.show()

Actual outcome


Close-up:

Expected outcome

No holes in the 3D surface plot.

Matplotlib version

Matplotlib 1.3.0 to 1.5.3 (WinPython 3.3.2.3 to 3.5.2.3), but it was also reproduced up to Matplotlib 2.0: http://stackoverflow.com/questions/41268550/matplotlib-3d-surface-gaps-holes-in-surface

Activity

  1. WeatherGod commented on Mar 7, 2017

    @WeatherGod
    Member
  2. WeatherGod commented on Mar 7, 2017

    @WeatherGod
    Member
  3. fmevers commented on Mar 7, 2017

    @fmevers
    Author

    Did I get you right, that if I create an array with values instead of NaNs, plot_surface() would be able to handle the array correctly? This would provide me a workaround. Thank you for your encouragement! But, to be honest, adding support would exceed my coding skills by light-years.

  4. eric-wieser commented on Jul 12, 2018

    @eric-wieser
    Contributor

    Can you attach the data that causes the problem?

  5. fmevers commented on Jul 20, 2018

    @fmevers
    Author
  6. kbruegge commented on Mar 7, 2019

    @kbruegge

    I still have that problem with matplotlib 3.0.3. Would be happy to know about a workaround or solution.

  7. franchesoni commented on Oct 16, 2019

    @franchesoni

    Same issue!

  8. timhoffm commented on Oct 24, 2019

    @timhoffm
    Member

    This is indeed related to the NaN values.

    If one does Z[np.isnan(Z)] = np.nanmean(Z) the artifacts are gone.

    Note: Updated the example code since plt.hold(True) is no more (and is unneeded) in matplotlib 3.1.

  9. added this to the v3.5.0 milestone on Aug 12, 2021
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions