WikiGalaxy

Personalize

Surface Plots and Wireframes

Introduction to Surface Plots:

Surface plots are three-dimensional plots that help visualize the relationship between three variables. They are particularly useful when you want to understand how a dependent variable changes over a range of two independent variables. In Python, the Matplotlib library provides tools to create surface plots using the Axes3D class from the mpl_toolkits.mplot3d module.

Wireframes:

Wireframe plots are similar to surface plots but display only the edges of the surface, giving a skeletal representation. This can be useful for examining the structure of the data without the distraction of a filled surface.

Example 1: Basic Surface Plot

Creating a Simple Surface Plot:

In this example, we will create a basic surface plot using a mathematical function z = sin(sqrt(x^2 + y^2)). This function creates a wave-like pattern on the surface plot.


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()
        

Explanation:

Here, we use NumPy to create a grid of x and y values, and then compute the z values using the sine function. The plot_surface method generates the surface plot, and the 'viridis' colormap is applied for color coding.

Example 2: Wireframe Plot

Creating a Wireframe Plot:

We will now create a wireframe plot using the same mathematical function as before. Wireframe plots help in visualizing the structure of the surface without the solid fill.


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

ax.plot_wireframe(x, y, z, color='teal')

plt.show()
        

Explanation:

The plot_wireframe method is used instead of plot_surface to generate a wireframe plot. The 'teal' color is used to distinguish the wireframe from the surface plot.

Example 3: Customizing Surface Plot

Adding Color Maps and Labels:

In this example, we will customize the surface plot by adding labels and changing the colormap to enhance the visualization.


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

surf = ax.plot_surface(x, y, z, cmap='coolwarm', edgecolor='none')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)

plt.show()
        

Explanation:

The colormap 'coolwarm' is applied to the surface plot, and labels are added to each axis. A color bar is also included to provide a reference for the color coding of the surface heights.

Example 4: Contour Plot on Surface

Combining Surface and Contour Plots:

This example demonstrates how to overlay a contour plot on a surface plot, providing additional information about the surface's topography.


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

ax.plot_surface(x, y, z, alpha=0.7, rstride=100, cstride=100)
ax.contour(x, y, z, zdir='z', offset=-2, cmap='coolwarm')

plt.show()
        

Explanation:

Contours are added using the contour method, which projects the contour lines onto the surface. The zdir parameter specifies the direction of the contour projection, and the offset parameter sets the height at which contours are drawn.

Example 5: Parametric Surface Plot

Visualizing Parametric Equations:

Parametric surface plots are used to visualize surfaces defined by parametric equations. In this example, we will plot a torus using parametric equations.


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, 2 * np.pi, 100)
u, v = np.meshgrid(u, v)
x = (3 + np.cos(v)) * np.cos(u)
y = (3 + np.cos(v)) * np.sin(u)
z = np.sin(v)

ax.plot_surface(x, y, z, cmap='plasma')

plt.show()
        

Explanation:

The parametric equations define the x, y, and z coordinates of the torus. The plot_surface method visualizes the torus, and the 'plasma' colormap enhances the plot's appearance.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025