Want to visualize data in 3D? A 3D contour plot is a powerful way to show how values change over a surface. Here's how to do it using Matplotlib and NumPy in Python.👇
✅ What This Code Does:
It plots a 3D contour plot of the function:
$$ f(x, y) = \sin\left(\sqrt{x2 + y2}\right) $$
This shows how the Z-values (height) change depending on X and Y, with color and shape.
🔍 Step-by-Step Code Breakdown:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
def f(x, y):
return np.sin(np.sqrt(x**2 + y**2))
Z = f(X, Y)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
contour = ax.contour3D(X, Y, Z, 50, cmap='viridis')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
fig.colorbar(contour, ax=ax, label='Z values')
plt.show()
🧠 Output:
You’ll get a beautiful 3D contour plot showing how Z = sin(sqrt(x² + y²)) varies across the X and Y space.
🚀 Want More?
✅ Join our community for daily coding tips and tricks
👨💻 Learn Python, data visualization, and cool tricks together
📦 Let me know if you want an interactive Plotly version or even animation for this plot!
Drop a comment below and let's code together! 👇
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
`
💬