Skip to content

Rendering issue occurs when plotting 3D surfaces at a discontinuity #12395

Closed

Description

@mattforestyoung

Bug report

Bug summary

When running the code below to plot the surface for the equation 1 - y/x, the plot works but I get an error and the rendering of the surface breaks when viewed from some angles.

Code for reproduction

import math
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

# Generate the data
x = np.linspace(-1.0, 1.0, 50)
y = np.linspace(-1.0, 1.0, 50)
z = np.zeros(len(x))
x,y = np.meshgrid(x,y)
z = 1 - y/x
z[z >= 5.0] = 5.0            # Clips top of surface
z[z <= -5.0] = -5.0          # Clips bottom of surface

# place NaNs at the discontinuity
pos = np.where(np.abs(np.diff(z)) >= 5.0)
z[pos] = np.nan

# Create the plot
p1 = plt.figure()
ax = p1.gca(projection='3d')
surf = ax.plot_surface(x,y,z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0,
                           vmin=np.nanmin(z), vmax=np.nanmax(z), antialiased=False)
plt.title("1 - y/x")
ax.set_xlim(-1.0, 1.0)
ax.set_ylim(-1.0, 1.0)
ax.set_zlim(-5.0, 5.0)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
p1.show()
raw_input("Press any key to close all plots: ")

Actual outcome

/usr/lib/pymodules/python2.7/matplotlib/colors.py:576: RuntimeWarning: invalid value encountered in less
  cbook._putmask(xa, xa < 0.0, -1)

Odd rendering issues appear:

rendering_angle1

render_angle3

Expected outcome

I'd expect to not get errors and to not see the rendering issue. I am using older versions of virtually everything, and this may be a platform-specific issue. Or I may be using this code in a way it was not intended to be used, in which case if there is a more-correct way to do this I'd appreciate someone showing me how to do it.

Matplotlib version

  • Operating system: Ubuntu 14.04
  • Matplotlib version: 1.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): 'TkAgg'
  • Python version: 2.7.6
  • Jupyter version (if applicable): N/A
  • Other libraries:

Activity

  1. dstansby commented on Oct 4, 2018

    @dstansby
    Member

    I can reproduce this on the latest code (master branch).

  2. WeatherGod commented on Oct 4, 2018

    @WeatherGod
    Member
  3. mattforestyoung commented on Oct 7, 2018

    @mattforestyoung
    Author

    @WeatherGod so if the plot_surface does not support NaNs, then I guess this is a user error. Is there an alternative way to achieve this plot without using plot_surface?

  4. timhoffm commented on Oct 7, 2018

    @timhoffm
    Member

    Side remark: If plot_surface does not support NaNs, we should probably check the inputs and issue a warning.

  5. WeatherGod commented on Oct 8, 2018

    @WeatherGod
    Member
  6. 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