How to show legend and label axes in 3D scatter plots in Python Plotly?

Plotly is an open-source Python library for creating interactive charts and visualizations. In 3D scatter plots, properly showing legends and labeling axes is crucial for data interpretation and presentation clarity.

This tutorial demonstrates how to create 3D scatter plots with customized legends and axis labels using plotly.express and pandas.

Basic 3D Scatter Plot Setup

First, let's create a simple 3D scatter plot with our sample data ?

import plotly.express as px
import pandas as pd

# Create sample data
data = {
    'gadget': ['mobile', 'tablet', 'ipad', 'laptop'],
    'rating': [3, 4, 2, 1],
    'price': [20000, 50000, 30000, 90000],
    'category': ['phone', 'tablet', 'tablet', 'computer']
}

df = pd.DataFrame(data)
print(df)
   gadget  rating  price   category
0  mobile       3  20000      phone
1  tablet       4  50000     tablet
2    ipad       2  30000     tablet
3  laptop       1  90000   computer

Creating 3D Scatter Plot with Legend

Use the color parameter to create a legend based on categorical data ?

import plotly.express as px
import pandas as pd

data = {
    'gadget': ['mobile', 'tablet', 'ipad', 'laptop'],
    'rating': [3, 4, 2, 1],
    'price': [20000, 50000, 30000, 90000],
    'category': ['phone', 'tablet', 'tablet', 'computer']
}

df = pd.DataFrame(data)

# Create 3D scatter plot with legend
fig = px.scatter_3d(
    df, 
    x='rating', 
    y='price', 
    z='gadget',
    color='category',  # Creates legend
    title="3D Scatter Plot with Legend"
)

fig.show()

Customizing Axis Labels

Use the labels parameter to customize axis titles ?

import plotly.express as px
import pandas as pd

data = {
    'gadget': ['mobile', 'tablet', 'ipad', 'laptop'],
    'rating': [3, 4, 2, 1],
    'price': [20000, 50000, 30000, 90000],
    'category': ['phone', 'tablet', 'tablet', 'computer']
}

df = pd.DataFrame(data)

fig = px.scatter_3d(
    df,
    x='rating',
    y='price', 
    z='gadget',
    color='category',
    title="Gadget Analysis: Rating vs Price",
    labels={
        'rating': 'User Rating (1-5)',
        'price': 'Price (INR)',
        'gadget': 'Gadget Type',
        'category': 'Device Category'
    }
)

fig.show()

Advanced Customization

Combine legend positioning, axis labels, and layout styling for professional visualization ?

import plotly.express as px
import pandas as pd

data = {
    'gadget': ['mobile', 'tablet', 'ipad', 'laptop', 'smartwatch'],
    'rating': [3, 4, 2, 1, 5],
    'price': [20000, 50000, 30000, 90000, 15000],
    'category': ['phone', 'tablet', 'tablet', 'computer', 'wearable']
}

df = pd.DataFrame(data)

fig = px.scatter_3d(
    df,
    x='rating',
    y='price',
    z='gadget',
    color='category',
    size=[10, 15, 12, 20, 8],  # Point sizes
    title="Complete 3D Scatter Plot Analysis",
    labels={
        'rating': 'User Rating (?)',
        'price': 'Price (?)',
        'gadget': 'Product Name',
        'category': 'Product Category'
    }
)

# Customize layout and legend
fig.update_layout(
    font_family="Arial",
    font_color="black",
    title_font_family="Times New Roman",
    title_font_color="darkblue",
    title_font_size=18,
    legend=dict(
        orientation="v",
        yanchor="top",
        y=1,
        xanchor="left",
        x=1.01
    ),
    scene=dict(
        xaxis_title="User Rating (?)",
        yaxis_title="Price (?)",
        zaxis_title="Product Name"
    )
)

fig.show()

Key Parameters Summary

Parameter Purpose Example
color Creates legend color='category'
labels Custom axis titles labels={'x': 'Custom X'}
scene 3D axis formatting scene=dict(xaxis_title='X')
legend Legend positioning legend=dict(x=1.01)

Conclusion

Use the color parameter to generate legends and labels dictionary for custom axis titles. The update_layout() method provides fine-grained control over legend positioning and 3D scene formatting for professional visualizations.

Updated on: 2026-03-26T22:27:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements