Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to show or hide labels in Pygal
Pygal is a Python data visualization library that creates interactive SVG graphs. It provides various chart types like line charts, bar charts, pie charts, and more. This article demonstrates how to work with labels in Pygal charts using functions like x_labels, add(), and render_to_file().
Installation
First, install Pygal using pip ?
pip install pygal
Key Functions for Labels
x_labels
Sets labels for the horizontal axis of the chart.
add()
Adds data series to the chart with optional labels for each series.
render_to_file()
Saves the chart as an SVG file.
Line Chart with Labels
Create a line chart with custom xaxis labels ?
import pygal
# Create line chart
line_chart = pygal.Line()
line_chart.x_labels = ['Yellow', 'Red', 'Purple']
line_chart.add('Values', [0.0006, 0.0003, 0.00028])
line_chart.render_to_file('line_chart.svg')
print("Line chart saved as line_chart.svg")
Line chart saved as line_chart.svg
Bar Chart with Series Labels
Create a bar chart showing Fibonacci series with a descriptive label ?
import pygal
# Create bar chart
bar_chart = pygal.Bar()
bar_chart.add('Fibonacci Series', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.render_to_file('bar_chart.svg')
print("Bar chart saved as bar_chart.svg")
Bar chart saved as bar_chart.svg
Pie Chart with Title and Labels
Create a pie chart with a title and labeled segments ?
import pygal
# Create pie chart
pie_chart = pygal.Pie()
pie_chart.title = 'Color Distribution'
pie_chart.add('Red', 33.33)
pie_chart.add('Sea Green', 33.33)
pie_chart.add('Purple', 33.33)
pie_chart.render_to_file('pie_chart.svg')
print("Pie chart saved as pie_chart.svg")
Pie chart saved as pie_chart.svg
Dot Chart with Rotated Labels
Create a dot chart with rotated xaxis labels ?
import pygal
# Create dot chart with rotated labels
dot_chart = pygal.Dot(x_label_rotation=30)
dot_chart.add('Positive Values', [5, 10, 15, 20, 25])
dot_chart.add('With Negatives', [0, -5, -10, -15, -20])
dot_chart.render_to_file('dot_chart.svg')
print("Dot chart saved as dot_chart.svg")
Dot chart saved as dot_chart.svg
Hiding Labels
You can hide labels by setting them to None or empty values ?
import pygal
# Chart without x-labels
chart_no_labels = pygal.Line()
chart_no_labels.x_labels = [] # Empty list hides x-labels
chart_no_labels.add('Data', [1, 3, 2, 4, 5])
chart_no_labels.render_to_file('no_labels.svg')
# Chart without legend
chart_no_legend = pygal.Line(legend_at_bottom=True, show_legend=False)
chart_no_legend.add('Hidden Series', [1, 3, 2, 4, 5])
chart_no_legend.render_to_file('no_legend.svg')
print("Charts with hidden labels created")
Charts with hidden labels created
Label Customization Options
| Property | Purpose | Example |
|---|---|---|
x_labels |
Set horizontal axis labels | chart.x_labels = ['A', 'B', 'C'] |
title |
Set chart title | chart.title = 'My Chart' |
show_legend |
Show/hide legend | pygal.Line(show_legend=False) |
x_label_rotation |
Rotate xaxis labels | pygal.Bar(x_label_rotation=45) |
Conclusion
Pygal provides flexible options for showing and hiding labels in charts. Use x_labels for axis labels, add() for series labels, and various configuration options to customize label visibility and appearance.
