Creating a 3D Bubble Chart in Python with Plotly

Creating a 3D bubble chart in $Python$ using plotly can effectively visualize data in three dimensions, with the addition of varying bubble sizes to represent a fourth data dimension.

In this example, we’ll use plotly.graph_objects to create a Scatter3d plot where each point (or bubble) has x, y, and z coordinates along with a size attribute for the bubble’s radius.

Here’s how to create a detailed 3D bubble chart with plotly.


Step-by-Step Code Explanation

  1. Generate Data: We’ll generate synthetic data for x, y, and z coordinates.
    We’ll also create a size variable to determine the size of each bubble.
  2. Create a Scatter3d Plot: Using plotly.graph_objects, we’ll create a Scatter3d object to represent each bubble, where:
    • x, y, and z correspond to the spatial coordinates.
    • marker.size controls the radius of each bubble.
    • marker.color is set to vary based on another variable for an additional data layer.
  3. Customize Layout: We’ll customize the layout to make the chart more informative and visually appealing.

Code Example

Here’s how you can implement a 3D bubble chart using plotly.

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

# Generate synthetic data
np.random.seed(0)
num_points = 100
x = np.random.normal(loc=0, scale=1, size=num_points)
y = np.random.normal(loc=1, scale=2, size=num_points)
z = np.random.normal(loc=2, scale=1.5, size=num_points)
size = np.random.randint(5, 20, size=num_points) # Bubble sizes (can represent some other variable)

# Create a 3D scatter plot for the bubble chart
fig = go.Figure(data=[go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
marker=dict(
size=size, # Bubble sizes
color=z, # Color based on Z-axis values or any other metric
colorscale='Viridis', # Color scale for bubbles
opacity=0.8,
sizemode='diameter' # Control size mode (use 'diameter' for relative sizing)
)
)])

# Update layout for clarity
fig.update_layout(
title="3D Bubble Chart",
scene=dict(
xaxis_title="X Axis",
yaxis_title="Y Axis",
zaxis_title="Z Axis"
),
margin=dict(l=0, r=0, b=0, t=30) # Adjust margins for better view
)

fig.show()


Explanation of Key Components

  1. Data Generation: We generate random data for x, y, and z coordinates, along with a size array for bubble sizes.
    This size variable can represent any additional metric, such as population, magnitude, or frequency.

  2. 3D Scatter Plot with Scatter3d:

    • x, y, and z represent the spatial coordinates.
    • marker.size controls the size of each bubble.
    • marker.color uses z values (or another variable) to set bubble colors, adding a layer of information.
  3. Customization:

    • Color Scale: The Viridis color scale is used for a visually pleasing gradient effect based on z values.
    • Opacity: Setting opacity=0.8 allows overlapping bubbles to blend slightly, enhancing the 3D effect.
    • Layout: Axes are labeled, and margins are adjusted for an optimal view.

Summary

This 3D bubble chart effectively visualizes data across three spatial dimensions (x, y, and z), with the size of each bubble representing a fourth dimension.
This visualization is useful for showing multi-dimensional data, especially when analyzing clusters, correlations, or densities across multiple variables.