Last modified: Jun 05, 2025 By Alexander Williams
How to Install Pygal in Python
Pygal is a Python library for creating interactive SVG charts. It is simple to use and produces beautiful visualizations. This guide will show you how to install Pygal in Python.
Prerequisites
Before installing Pygal, ensure you have Python installed. You can check this by running python --version
in your terminal.
python --version
If Python is not installed, download it from the official website. Pygal works with Python 3.6 and above.
Install Pygal Using pip
The easiest way to install Pygal is using pip, Python's package manager. Open your terminal and run the following command.
pip install pygal
This will download and install Pygal along with its dependencies. Wait for the installation to complete.
Verify the Installation
To ensure Pygal is installed correctly, run the following Python code in your terminal or IDE.
import pygal
print("Pygal version:", pygal.__version__)
If installed properly, it will display the Pygal version.
Pygal version: 2.4.0
Create a Simple Chart
Let's create a simple bar chart to test Pygal. Copy the following code into a Python file.
import pygal
# Create a bar chart
bar_chart = pygal.Bar()
bar_chart.title = "Sample Bar Chart"
bar_chart.add('Data', [1, 3, 5, 7, 9])
# Save the chart
bar_chart.render_to_file('bar_chart.svg')
Run the script. It will generate an SVG file named bar_chart.svg. Open it in a browser to view the chart.
Common Installation Issues
If you encounter errors during installation, try these solutions.
1. Permission Denied: Use pip install --user pygal
to install locally.
2. Outdated pip: Update pip with pip install --upgrade pip
before installing Pygal.
3. Missing Dependencies: Ensure all dependencies are installed. Pygal requires lxml and cairosvg for some features.
Advanced Usage
Pygal supports various chart types like line, pie, and radar. For more advanced features, check the Pygal documentation.
You can also customize colors, labels, and animations. Here's an example of a pie chart.
pie_chart = pygal.Pie()
pie_chart.title = "Sample Pie Chart"
pie_chart.add('A', 30)
pie_chart.add('B', 50)
pie_chart.add('C', 20)
pie_chart.render_to_file('pie_chart.svg')
Conclusion
Installing Pygal in Python is straightforward with pip. It is a powerful tool for creating SVG charts. For more libraries, see this guide.
Now you can start visualizing data with Pygal. Happy coding!