How to manually add a legend color and legend font size on a plotly figure in Python?

This tutorial explains how to manually customize legend text color and font size on a Plotly figure using Python. Plotly is a powerful data visualization library that creates interactive charts and graphs. While Plotly provides default legend settings, you may need to customize the legend appearance to match your specific design requirements.

Syntax

Use Plotly's update_layout() method with the legend_font_color and legend_font_size parameters to customize legend appearance ?

fig = px.scatter(df, x="x_column", y="y_column", color="category_column")

# Set legend font color
fig.update_layout(legend_font_color='red')

# Set legend font size  
fig.update_layout(legend_font_size=14)

# Or combine both parameters
fig.update_layout(legend_font_color='blue', legend_font_size=16)

Example 1: Custom Dataset with Exam Scores

This example creates a scatter plot using a custom dataset with exam scores categorized by gender ?

import plotly.express as px
import pandas as pd
import numpy as np

# Create sample dataset
data = {
    'Exam_1_Score': np.random.randint(50, 101, 50),
    'Exam_2_Score': np.random.randint(50, 101, 50), 
    'Gender': np.random.choice(['Male', 'Female'], 50)
}
df = pd.DataFrame(data)

# Create scatter plot with custom colors
fig = px.scatter(df, 
                x='Exam_1_Score', 
                y='Exam_2_Score', 
                color='Gender',
                color_discrete_map={'Male': 'blue', 'Female': 'pink'},
                title='Exam Scores by Gender')

# Customize legend appearance
fig.update_layout(
    legend_font_color='red',
    legend_font_size=14
)

fig.show()

The scatter plot displays exam scores with color-coded markers for gender. The legend text appears in red with a font size of 14 pixels.

Example 2: Tips Dataset

This example uses Plotly's built-in tips dataset to demonstrate legend customization ?

import plotly.express as px

# Load built-in tips dataset
df = px.data.tips()

# Create scatter plot with size and color mapping
fig = px.scatter(df, 
                x='total_bill', 
                y='tip', 
                size='size', 
                color='sex',
                title='Restaurant Tips Analysis')

# Customize legend with green color and larger font
fig.update_layout(
    legend_font_color='green',
    legend_font_size=16
)

fig.show()

This creates a scatter plot showing the relationship between total bill and tip amount, with markers sized by party size and colored by gender. The legend displays in green with a 16-pixel font size.

Additional Legend Customization

You can further customize the legend appearance with additional parameters ?

import plotly.express as px

df = px.data.iris()

fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')

# Comprehensive legend customization
fig.update_layout(
    legend=dict(
        font=dict(
            color='darkblue',
            size=18
        ),
        bgcolor='lightgray',
        bordercolor='black',
        borderwidth=1
    )
)

fig.show()

This example demonstrates additional legend styling including background color, border, and more detailed font settings.

Parameter Summary

Parameter Description Example Values
legend_font_color Sets legend text color 'red', 'blue', '#FF5733'
legend_font_size Sets legend text size in pixels 12, 14, 16, 18
legend.bgcolor Sets legend background color 'white', 'lightgray'

Conclusion

Use update_layout() with legend_font_color and legend_font_size parameters to customize Plotly legend appearance. These simple modifications can significantly improve your visualization's readability and visual appeal.

Updated on: 2026-03-27T06:34:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements