Ways to resolve an 'invalid value encountered' in matplotlib surface plot of sin(x)/x?

249 Views Asked by At

I've been trying to resolve this error I keep getting with this 3D surface plot of z = sin(x)/x. It returns:

<ipython-input-11-562ca5778dec>:6: RuntimeWarning: invalid value encountered in true_divide
z = np.sin(x)/x
<ipython-input-11-562ca5778dec>:9: UserWarning: Z contains NaN values. This may result in rendering artifacts.
ax.plot_surface(x,y,z,

I can't figure out a way to get around it without making the code super chunky/inefficient. Here's the code I'm working with right now:

import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x,y = np.meshgrid(np.linspace(0,10,100),np.linspace(0,10,100))
z = np.sin(x)/x
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x,y,z,
               cmap='plasma')
ax.set_xlabel("x-axis",fontsize=20)
ax.set_ylabel("y-axis",fontsize=20)
ax.set_zlabel("z-axis",fontsize=20)
plt.show()
0

There are 0 best solutions below