Histogram in pygal

Pygal is a Python library for creating interactive charts and graphs. One of its supported chart types is the histogram, which provides a graphical representation of data distribution to help identify patterns, outliers, and trends in datasets.

What is a Histogram?

A histogram displays the frequency of data points within specific intervals called bins. The horizontal axis (x-axis) represents the range of values in the dataset, while the vertical axis (y-axis) shows the frequency of occurrences within each range.

Histograms are particularly useful for visualizing continuous data, such as test scores, heights, or temperatures. They reveal the shape of data distribution, including skewness and the presence of peaks or valleys.

Installing Pygal

First, install the Pygal library using pip ?

pip install pygal

Creating a Basic Histogram

Here's how to create a simple histogram in Pygal ?

import pygal

# Create a Histogram object
histogram = pygal.Histogram(title='Distribution of Scores', x_title='Score Range')

# Add data to the histogram (frequency, start, end)
histogram.add('Scores', [(6, 45, 55), (7, 55, 65), (8, 65, 75), (8, 75, 85), (7, 85, 95), (6, 95, 105)])

# Render the chart
print("Histogram created successfully")
print("Data points:", [(6, 45, 55), (7, 55, 65), (8, 65, 75), (8, 75, 85), (7, 85, 95), (6, 95, 105)])
Histogram created successfully
Data points: [(6, 45, 55), (7, 55, 65), (8, 65, 75), (8, 75, 85), (7, 85, 95), (6, 95, 105)]

Each tuple in the data represents (frequency, start_value, end_value) for each bin.

Customizing the Histogram

Pygal allows extensive customization of histogram appearance ?

import pygal

# Create a customized histogram
histogram = pygal.Histogram(
    title='Test Scores Distribution',
    x_title='Score Ranges', 
    y_title='Frequency',
    width=800,
    height=400
)

# Add multiple datasets with different colors
histogram.add('Class A', [(5, 60, 70), (8, 70, 80), (12, 80, 90), (6, 90, 100)])
histogram.add('Class B', [(3, 60, 70), (6, 70, 80), (10, 80, 90), (9, 90, 100)])

print("Customized histogram with multiple datasets created")
print("Class A data: [(5, 60, 70), (8, 70, 80), (12, 80, 90), (6, 90, 100)]")
print("Class B data: [(3, 60, 70), (6, 70, 80), (10, 80, 90), (9, 90, 100)]")
Customized histogram with multiple datasets created
Class A data: [(5, 60, 70), (8, 70, 80), (12, 80, 90), (6, 90, 100)]
Class B data: [(3, 60, 70), (6, 70, 80), (10, 80, 90), (9, 90, 100)]

Advanced Styling Options

You can apply custom styles and colors to enhance the histogram appearance ?

import pygal
from pygal.style import Style

# Define custom style
custom_style = Style(
    background='white',
    plot_background='white',
    colors=('#ff6384', '#36a2eb', '#4bc0c0')
)

# Create histogram with custom style
histogram = pygal.Histogram(
    title='Sales Data by Quarter',
    x_title='Sales Range ($)',
    style=custom_style,
    legend_at_bottom=True
)

# Add data
histogram.add('Q1 Sales', [(15, 1000, 2000), (25, 2000, 3000), (30, 3000, 4000)])
histogram.add('Q2 Sales', [(20, 1000, 2000), (28, 2000, 3000), (25, 3000, 4000)])

print("Styled histogram created with legend at bottom")
print("Custom colors applied: #ff6384, #36a2eb, #4bc0c0")
Styled histogram created with legend at bottom
Custom colors applied: #ff6384, #36a2eb, #4bc0c0

Key Features

Feature Parameter Description
Chart Size width, height Set chart dimensions
Axis Labels x_title, y_title Label chart axes
Legend Position legend_at_bottom Move legend to bottom
Custom Colors style Apply color themes

Conclusion

Pygal makes creating histograms straightforward with its intuitive API and extensive customization options. You can easily visualize data distributions, compare multiple datasets, and create professional-looking charts with custom styling and interactive features.

Updated on: 2026-03-27T06:57:51+05:30

393 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements