Creating a Complex 3D Contour Plot in Python with Plotly

Visualizing complex mathematical surfaces in 3D can be incredibly insightful, especially when using interactive plotting libraries like Plotly.

In this tutorial, we’ll build an intricate 3D contour plot using plotly.graph_objects and Python’s numpy.

Although Plotly doesn’t directly support 3D contour plots, we can achieve a similar effect using the go.Surface plot type.

Let’s walk through the steps to create this visualization.


Step 1: Define a Complex Mathematical Function

To create a compelling 3D plot, we need a mathematical function that has varied, interesting features.

For this example, let’s combine trigonometric functions with an exponential decay factor.

This will produce wave-like patterns that decay outward, creating a complex and visually engaging surface.

Here’s the function we’ll use:

1
2
3
4
import numpy as np

def complex_function(x, y):
return np.sin(x**2 + y**2) * np.exp(-0.1 * (x**2 + y**2)) + 0.5 * np.sin(3 * x) * np.cos(3 * y)

This function combines sine waves with exponential decay, resulting in a surface that has oscillations and a natural fade-out effect, adding to the complexity.


Step 2: Create a Grid of Points

We need to evaluate our function across a grid of $(x)$ and $(y)$ values.

Using numpy.linspace, we can create a finely spaced grid.

This allows us to generate smooth contours and ensures that the details of our surface are captured.

1
2
3
4
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = complex_function(X, Y)

Here, X and Y form a 2D grid, and Z is the output of our function across this grid, representing height.


Step 3: Plot the Surface with Plotly’s go.Surface

To plot in 3D, we use Plotly’s go.Surface plot type.

go.Surface is flexible, allowing us to visualize a 3D surface with customizable color maps and contour lines.

We’ll set up contour lines along the z axis to give the appearance of a 3D contour plot.

1
2
3
4
5
6
7
8
9
10
11
import plotly.graph_objects as go

fig = go.Figure(data=go.Surface(
x=X,
y=Y,
z=Z,
colorscale='Viridis',
contours={
"z": {"show": True, "size": 0.1, "start": -1, "end": 1} # Define contour levels
}
))

In this code:

  • colorscale='Viridis' applies a perceptually uniform color scale, enhancing depth perception.
  • contours settings add contour lines along the z axis, creating a layered effect that resembles contour lines on a topographic map.

Step 4: Customize the Layout

To make the plot more informative and visually appealing, we’ll add labels and adjust the camera view to highlight the surface’s details.

1
2
3
4
5
6
7
8
9
10
11
12
fig.update_layout(
title="Complex 3D Contour Plot using Surface",
scene=dict(
xaxis_title="X Axis",
yaxis_title="Y Axis",
zaxis_title="Z Axis",
camera=dict(
eye=dict(x=1.25, y=1.25, z=0.75) # Adjust camera position for better view
)
)
)
fig.show()

With these settings:

  • xaxis_title, yaxis_title, and zaxis_title label each axis.
  • camera positions the viewpoint, giving a balanced, 3D perspective on the plot.

Full Code

Here’s the complete code to generate this 3D contour-style plot:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import plotly.graph_objects as go

# Define the function for complex surface
def complex_function(x, y):
return np.sin(x**2 + y**2) * np.exp(-0.1 * (x**2 + y**2)) + 0.5 * np.sin(3 * x) * np.cos(3 * y)

# Generate a grid of points
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = complex_function(X, Y)

# Create 3D surface plot with contours on z-axis
fig = go.Figure(data=go.Surface(
x=X,
y=Y,
z=Z,
colorscale='Viridis',
contours={
"z": {"show": True, "size": 0.1, "start": -1, "end": 1}
}
))

# Customize plot
fig.update_layout(
title="Complex 3D Contour Plot using Surface",
scene=dict(
xaxis_title="X Axis",
yaxis_title="Y Axis",
zaxis_title="Z Axis",
camera=dict(
eye=dict(x=1.25, y=1.25, z=0.75)
)
)
)

fig.show()

Output


Explanation of Key Components

  • Complex Function: By combining trigonometric and exponential functions, the surface has both oscillations and decay, providing an intricate structure.
  • Surface Plot and Contours: Although Plotly lacks a dedicated 3D contour plot type, adding contours on the z axis of a go.Surface plot effectively mimics this visualization style.
  • Interactive Plotting: Plotly’s interactive nature allows users to explore the plot by rotating and zooming, revealing intricate details from different perspectives.

This plot is highly customizable.

Try experimenting with different functions, contour settings, and color scales to create unique 3D visualizations!