Fundamentals 10 min read

Create Stunning SVG Charts in Python with Pygal: A Step‑by‑Step Guide

Learn how to quickly generate beautiful SVG visualizations in Python using the Pygal library, covering installation, data loading with pandas, and creating line, bar, pie, radar, box, scatter, funnel, and gauge charts with various built‑in styles, all with minimal code.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Create Stunning SVG Charts in Python with Pygal: A Step‑by‑Step Guide

Sometimes data visualization does not require complex functions; you may only want to display simple data graphically.

Today we introduce a beginner‑friendly Python visualization library—pygal.

pygal is a niche library focused on SVG charts, offering interactivity and the ability to create beautiful graphics with very little code. It supports line, bar, histogram, pie, radar, funnel, gauge and other common chart types, and provides 16 elegant themes.

Installation is straightforward: pip install pygal Below we use a 2020 monthly living‑expense dataset from a dormitory as an example.

First, read the data with pandas:

import pandas as pd
data = pd.read_excel('生活费开销.xlsx')

To display pygal charts directly in Jupyter, create a basic HTML template:

import pygal
# Set pygal interaction with Jupyter notebook
from IPython.display import display, HTML
base_html = """<!DOCTYPE html>
<html>
  <head>
    <script type=\"text/javascript\" src=\"http://kozea.github.com/pygal.js/javascripts/svg.jquery.js\"></script>
    <script type=\"text/javascript\" src=\"https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js\"></script>
  </head>
  <body>
    <figure>
      {rendered_chart}
    </figure>
  </body>
</html>"""

1. Line chart (DefaultStyle)

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
line_chart = pygal.Line(style=DefaultStyle)
line_chart.title = '520寝室2020年生活费花销情况'
line_chart.x_labels = label
for i in people:
    line_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))

2. Bar charts (DarkStyle, NeonStyle)

Vertical bar chart:

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
bar_chart = pygal.Bar(style=DarkStyle)
bar_chart.title = '520寝室2020年生活费花销情况'
bar_chart.x_labels = label
for i in people:
    bar_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))

Horizontal bar chart:

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
hbar_chart = pygal.HorizontalBar(style=NeonStyle)
hbar_chart.title = '520寝室2020年生活费花销情况'
hbar_chart.x_labels = label
for i in people:
    hbar_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=hbar_chart.render(is_unicode=True)))

3. Pie charts (DarkSolarizedStyle)

Standard pie chart:

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
pie_chart = pygal.Pie(style=DarkSolarizedStyle)
pie_chart.title = '520寝室2020年1月生活费花销情况'
pie_chart.x_labels = label
for i in people:
    pie_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=pie_chart.render(is_unicode=True)))

Donut chart:

pie_chart = pygal.Pie(inner_radius=0.45, style=LightSolarizedStyle)
pie_chart.title = '520寝室2020年1月生活费花销情况'
for i in people:
    pie_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist()[0])
HTML(base_html.format(rendered_chart=pie_chart.render(is_unicode=True)))

4. Radar chart (LightStyle)

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
radar_chart = pygal.Radar(style=LightStyle)
radar_chart.title = '520寝室2020年生活费花销情况'
radar_chart.x_labels = label
for i in people:
    radar_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=radar_chart.render(is_unicode=True)))

5. Box plot (CleanStyle)

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
box_plot = pygal.Box(style=CleanStyle)
box_plot.title = '520寝室2020年生活费花销情况'
for i in people:
    box_plot.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=box_plot.render(is_unicode=True)))

6. Scatter (Dot) chart (RedBlueStyle)

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
dot_chart = pygal.Dot(x_label_rotation=30, style=RedBlueStyle)
dot_chart.title = '520寝室2020年生活费花销情况'
dot_chart.x_labels = label
for i in people:
    dot_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=dot_chart.render(is_unicode=True)))

7. Funnel chart (DarkColorizedStyle)

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
funnel_chart = pygal.Funnel(style=DarkColorizedStyle)
funnel_chart.title = '520寝室2020年生活费花销情况'
funnel_chart.x_labels = label
for i in people:
    funnel_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=funnel_chart.render(is_unicode=True)))

8. Gauge chart (LightColorizedStyle)

from pygal.style import *
people = data['人员'].unique()
label = data['月份'].unique()
gauge_chart = pygal.Gauge(human_readable=True, style=LightColorizedStyle)
gauge_chart.title = '520寝室2020年1月生活费花销情况'
gauge_chart.range = [0, 5000]
for i in people:
    gauge_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=gauge_chart.render(is_unicode=True)))

Attentive readers will notice that the pygal chart‑creation pattern is essentially the same, with core code of about five lines, making it a versatile tool for quick visualizations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonSVGData visualizationPygal
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.