Python Plotly – How to change variable/label names for the legend in a line chart?

Plotly is an open-source, interactive, and browser-based charting library for Python. Python users can use Plotly to generate different types of charts including scientific charts, 3D graphs, statistical charts, financial charts, etc.

In this tutorial, we will show how you can use Plotly to change the variable and label names for the legend in a line chart. The name parameter in scatter traces controls legend labels, while legend_title customizes the legend title.

Basic Setup

First, import the required module and create a figure object ?

import plotly.graph_objs as go

# Create a figure object
fig = go.Figure()
print("Figure created successfully")
Figure created successfully

Creating Sample Data

Let's create a dictionary with sample data for our line chart ?

# Sample data
data = {
    'id': [1, 2, 3, 4, 5],
    'salary': [10000, 12000, 11000, 12500, 13000],
    'age': [21, 22, 23, 24, 25]
}

print("Data:", data)
Data: {'id': [1, 2, 3, 4, 5], 'salary': [10000, 12000, 11000, 12500, 13000], 'age': [21, 22, 23, 24, 25]}

Adding Traces with Custom Legend Labels

Use the add_trace() method with the name parameter to set custom legend labels ?

import plotly.graph_objs as go

# Create figure and data
fig = go.Figure()
data = {
    'id': [1, 2, 3, 4, 5],
    'salary': [10000, 12000, 11000, 12500, 13000],
    'age': [21, 22, 23, 24, 25]
}

# Add first trace with custom legend label
fig.add_trace(go.Scatter(
    x=data['id'],
    y=data['salary'],
    mode='lines+markers',
    name="Employee Salary"  # Custom legend label
))

# Add second trace with custom legend label
fig.add_trace(go.Scatter(
    x=data['id'],
    y=data['age'],
    mode='lines+markers',
    name="Employee Age",  # Custom legend label
    yaxis="y2"  # Use secondary y-axis for different scale
))

print("Traces added with custom legend labels")
Traces added with custom legend labels

Customizing Legend Title and Layout

Use update_layout() to customize the legend title and chart appearance ?

import plotly.graph_objs as go

# Complete example with customized legend
fig = go.Figure()
data = {
    'id': [1, 2, 3, 4, 5],
    'salary': [10000, 12000, 11000, 12500, 13000],
    'age': [21, 22, 23, 24, 25]
}

# Add traces
fig.add_trace(go.Scatter(
    x=data['id'],
    y=data['salary'],
    mode='lines+markers',
    name="Salary (USD)"
))

fig.add_trace(go.Scatter(
    x=data['id'],
    y=data['age'],
    mode='lines+markers',
    name="Age (Years)"
))

# Customize layout and legend
fig.update_layout(
    title="Employee Data Analysis",
    xaxis_title="Employee ID",
    yaxis_title="Values",
    legend_title="Metrics",  # Custom legend title
    font=dict(
        family="Arial",
        size=14,
        color="black"
    ),
    legend=dict(
        x=0.02,
        y=0.98,
        bgcolor="rgba(255, 255, 255, 0.8)",
        bordercolor="rgba(0, 0, 0, 0.2)",
        borderwidth=1
    )
)

# Display the chart (would open in browser)
print("Chart configured with custom legend labels and styling")
Chart configured with custom legend labels and styling

Key Parameters for Legend Customization

Parameter Purpose Example
name Sets individual trace legend labels name="Sales Data"
legend_title Sets the main legend title legend_title="Categories"
legend Controls legend position and styling legend=dict(x=0, y=1)

Conclusion

Use the name parameter in go.Scatter() to set custom legend labels for each trace. Customize the legend title using legend_title in update_layout() for better chart readability.

Updated on: 2026-03-26T22:28:37+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements