Reputation: 84
I'd like to create a 3D plot from an equation with x and y, similar to Google's 3D graph.
An example:
input: sin(sqrt(x**2 + y**2))
The Z will obviously be equal to the given input, but how will x
and y
be calculated? Thanks for any help given!
Upvotes: 1
Views: 1453
Reputation: 4705
You can start by creating a meshgrid
for your X
and Y
. Then compute your Z
by doing Z=np.sin(np.sqrt(X**2 + Y**2))
. Finally, you can plot the surface by using the matplotlib function ax.plot_surface(X, Y, Z)
.
You can find the code below:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
N_points=100
x = np.linspace(-10, 10, N_points)
y = np.linspace(-10, 10, N_points)
X, Y = np.meshgrid(x, y)
Z=np.sin(np.sqrt(X**2 + Y**2))
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
And the output of this code gives:
Upvotes: 2