How to show or hide labels in Pygal


The Python module named Pygal is a data visualization library that creates SVG graphs in various styles. This type of graph generally creates a highly interactive plot that users will easily understand. For example− line(), pie(), histogram(), etc. In Python, we have some built−in functions such as x_labels(), add(), and, render_to_file() will be used to show or hide labels in Pygal.

Syntax

The following syntax is used in the examples-

x_labels()

x_labels() follow the pyplot module of matplotlib that can be used to set the label on the horizontal axis.

add()

The add() is an in−built function in Python that can be used to insert the element into the set.

render_to_file()

The built−in function render_to_file() can render the image of the graph based on a specific program and load it into the file.

Installation Requirement

pip install pygal

This is the required command to install to your system while running the program based on Pygal module of Python.

Using Line Graph

The program uses the built−in function Line(), also known as the drawing function of Python and it will be used to plot the line based on specific integer or float parameters.

Example

In the following example, we will start the program by defining the built−in function Line() will help to draw the line based on the plotting point and store it in the variable A. Then set the horizontal axis in the variable A.x_labels. Next, it will use the built−in function add() to add the plotting plot to the line and create the visualization of graph. Finally, the built−in function render_to_file() sets the parameter to save the name of the file in SVG extension and allows for downloading the file.

A = pygal.Line()
A.x_labels = 'Yellow', 'Red', 'Purple'
A.add('line', [0.0006, 0.0003, 0.00028])
A.render_to_file('chart1.svg')

Output

The Bar Graph

The program uses a built−in function Bar() with the Pygal module to draw the bar graph based on Fibonacci series.

Example

In the following example, start the program by using the built−in function Bar() with Pygal module that will create the bar graph. Next, it will use the built−in function add() that accepts two parameters− label(to justify the representation of the graph) and list[to plot the point]. In the end, it will use the built−in function render_to_file() to generate the result.

bar_graph = pygal.Bar()
bar_graph.add('Graph of Fibonacci series', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_graph.render_to_file('chart2.svg')

Output

The Pie Graph

In this program, it will use some built−in function of Python such as Pie(), add(), and, render_to_file() will help to show or hide labels in Pygal.

Example

In the following example, Then it will add the name of the title of the graph and using add() it accepts the parameters based on region name and region percentage. The built−in function render_to_file() generate the result by rendering the template based on program.

pie_graph = pygal.Pie()
pie_graph.title = 'Color Percentage'
pie_graph.add('Red', 33.33)
pie_graph.add('Sea green', 33.33)
pie_graph.add('Purple', 33.33)
pie_graph.render_to_file('chart3.svg')

Output

The Dot Graph

The program uses dot graph to create a simple data visualization that helps to plot the data on both horizontal and vertical axis.

Example

In the following example, we will use the Pygal library to create a dot graph and save the file name as chart5.svg. This graph consists of two series− ‘normal’ and ‘With negative’ with some plotting values. The x_label_rotation represents the rotation of the horizontal label axis by 30 degree.

dot_graph = pygal.Dot(x_label_rotation=30)
dot_graph.add('Normal', [5, 10, 15, 20, 25])
dot_graph.add('With negatives', [0, -5, -10, -15, -20])
dot_graph.render_to_file('chart5.svg')

Output

Conclusion

We discussed the various ways to solve the problem statement. Pygal is known as an open−source data visualization library in Python that creates a high−level interactive plot according to given values. In comparison to matplotlib it doesn’t provide a flexible solution.

Updated on: 14-Aug-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements