How to change the position of legend in Pygal?

Pygal is a Python data visualization library designed to create interactive charts and graphs. Built with advanced SVG (Scalable Vector Graphics) features, it produces high-quality graphics that can be easily embedded into web pages and applications.

Installing Pygal

You can install Pygal using pip ?

pip install pygal

Understanding Legends in Pygal

The legend adds context and clarity to charts by displaying the names of each data series and their corresponding colors. By default, legends appear at the top of Pygal charts.

Changing Legend Position

Syntax

To change the legend position, use the legend_at_bottom property ?

chart.legend_at_bottom = True  # Move legend to bottom
chart.legend_at_bottom = False # Keep legend at top (default)

Example: Legend at Top (Default)

Here's a bar chart with the legend at the default top position ?

import pygal

# Create bar chart
bar_chart = pygal.Bar(width=600, height=400)
bar_chart.title = 'Cricket Match Scores'
bar_chart.x_labels = ['1-10 Overs', '11-20 Overs', '21-30 Overs', '31-40 Overs', '41-50 Overs']
bar_chart.add('Runs Scored', [35, 40, 25, 50, 90])

# Keep legend at top (default behavior)
bar_chart.legend_at_bottom = False

# Display chart
print("Chart created with legend at top")
Chart created with legend at top

Example: Legend at Bottom

Move the legend to the bottom and organize it in columns ?

import pygal

# Create line chart
line_chart = pygal.Line(width=600, height=400)
line_chart.title = 'Weekly Temperature'
line_chart.x_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

# Add multiple data series
line_chart.add('City A', [23, 34, 25, 37, 32, 40, 24])
line_chart.add('City B', [28, 31, 29, 35, 30, 38, 26])

# Move legend to bottom
line_chart.legend_at_bottom = True
line_chart.legend_at_bottom_columns = 2  # Organize in 2 columns

print("Chart created with legend at bottom")
Chart created with legend at bottom

Legend Configuration Options

Additional properties for customizing legend placement ?

import pygal

chart = pygal.Bar()
chart.title = 'Sales Data'

# Legend positioning options
chart.legend_at_bottom = True           # Move to bottom
chart.legend_at_bottom_columns = 3     # Number of columns
chart.legend_box_size = 18             # Size of legend boxes

# Add sample data
chart.add('Q1', [100, 120, 80])
chart.add('Q2', [110, 130, 90])
chart.add('Q3', [105, 125, 85])

print("Legend configured with custom options")
Legend configured with custom options

Comparison

Property Description Default Value
legend_at_bottom Position legend at bottom False
legend_at_bottom_columns Number of columns for bottom legend None
legend_box_size Size of legend color boxes 12

Conclusion

Use legend_at_bottom = True to move legends to the bottom of Pygal charts. Combine with legend_at_bottom_columns to organize multiple legend entries efficiently.

Updated on: 2026-03-27T11:29:43+05:30

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements