How to change the position of legend in Pygal?


Pygal is abbreviated as Python Data Visualization Library, which is designed to create the interactive charts and graphs. This is library is built with the advanced features than the SVG(Scalar Vector Graphics) format, to work with the high quality graphics that can be easily embedded into the web pages and applications.

Pygal is abbreviated as Python Data Visualization Library, which is designed to create the interactive charts and graphs. This is library is built with the advanced features than the SVG(Scalar Vector Graphics) format, to work with the high quality graphics that can be easily embedded into the web pages and applications.

Installing Pygal

You can install pygal using the following command –

>pip install pygal
Defaulting to user installation because normal site-packages is not writeable
Collecting pygal
  Downloading pygal-3.0.0-py2.py3-none-any.whl (129 kB)
     ------------------------------------- 129.4/129.4 kB 80.3 kB/s eta 0:00:00
Installing collected packages: pygal
Successfully installed pygal-3.0.0

Changing the position of legend

The legend is used to add the context and clarity to the data, which displays the names of each data series in our chart and the corresponding colors too. By default, the legend in the pygal graphs is at the top of the graph. You can change its position using the property legend_at_bottom.

Syntax

Following is the syntax for changing the position of the legend in the pygal –

pygal.chart_name.legend_at_bottom = True

Where,

  • pygal is the library of the python language.

  • chart_name is the chart that we want to use.

  • legend_at_bottom is the legend.

Example

In the following example, we will set the value of the property legend_at_bottom as False.

import pygal
line_chart = pygal.Bar(width=690, height=300, explicit_size=True)
line_chart.title = 'Scores'
line_chart.x_labels = ['1-10 Overs', '11-20 Overs', '21-30 Overs', '31-40 Overs', '41-50 Overs']
line_chart.add('Temperature', [35,40,25,50,90])
line_chart.legend_at_bottom = False
line_chart.render_in_browser()

Output

When we run the above code, the output of the legend will be shown as below. When we use render() to display the chart, the chart will not be display but the data will be returned in the xml format.

Example

Let’s see another example to display the legend at the bottom by assigning the legend_at_bottom as True and then the output will be an image with the legend at the bottom left which is displayed in the web browser as we used render_in_browser().

import pygal
line_chart = pygal.Line()
line_chart.title = 'Temperature'
line_chart.x_labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
line_chart.add('Temperature', [23,34,25,37,32,40,24])
line_chart.legend_at_bottom = True
line_chart.legend_at_bottom_columns = 2
line_chart.render_in_browser()

Output

When we execute the above code, following output will be displayed -

Updated on: 09-Aug-2023

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements