How to get a 3D colored surface with Python?
How to get a 3D color surface in Python?
To get a 3D color surface in Python, you can follow these steps −
Steps
-
Set the plot size and adjust the padding between and around the subplots.
-
Use NumPy to create x and y data points.
-
Get the 3D data, i.e., z.
-
Create a new figure or activate an existing figure.
-
Get the 3D axes.
-
Create a surface plot.
-
To display the figure, use the show() method.
Example
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-3, 3, 100)
y = np.cos(x)
x, y = np.meshgrid(x, y)
z = x ** 2 + y ** 2 - 2
fig = plt.figure()
ax = plt.axes(projection='3d')
surf = ax.plot_surface(x, y, z,
cmap=plt.get_cmap('hot'),
edgecolor='none')
plt.show()
Output
This will produce the following output −