how to make a 3d graph with matplotlib python in python

Here is an example code for making a 3D graph using matplotlib in Python.

main.py
import matplotlib.pyplot as plt
import numpy as np

# Define a function to create a 3D graph
def func3d(x, y):
    return np.sin(np.sqrt(x ** 2 + y ** 2))

# Define the range of values for x and y
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

# Create a 2D meshgrid
X, Y = np.meshgrid(x, y)

# Calculate Z values for the 3D graph using the function
Z = func3d(X, Y)

# Create a 3D plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, cmap='coolwarm')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
554 chars
26 lines

You can modify the func3d function to generate different graphs based on your requirements.

related categories

gistlibby LogSnag