You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Rendering issue occurs when plotting 3D surfaces at a discontinuity #12395
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
importmathimportnumpyasnpimportmatplotlibasmplimportmatplotlib.pyplotaspltimportmatplotlib.tickerfrommatplotlibimportcmfrommpl_toolkits.mplot3dimportAxes3D# Generate the datax=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/xz[z>=5.0] =5.0# Clips top of surfacez[z<=-5.0] =-5.0# Clips bottom of surface# place NaNs at the discontinuitypos=np.where(np.abs(np.diff(z)) >=5.0)
z[pos] =np.nan# Create the plotp1=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:
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.
@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?
Activity
dstansby commentedon Oct 4, 2018
I can reproduce this on the latest code (master branch).
WeatherGod commentedon Oct 4, 2018
mattforestyoung commentedon Oct 7, 2018
@WeatherGod so if the
plot_surfacedoes not support NaNs, then I guess this is a user error. Is there an alternative way to achieve this plot without usingplot_surface?timhoffm commentedon Oct 7, 2018
Side remark: If
plot_surfacedoes not support NaNs, we should probably check the inputs and issue a warning.WeatherGod commentedon Oct 8, 2018