The same plot in 14 other libraries — Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Seaborn; R: ggplot2; Julia: Makie.jl; JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts, MUI X Charts. Compare all 15 side by side: Basic Gauge Chart in Python, R, Julia and JavaScript.
A gauge chart (also known as a speedometer chart) displays a single value within a defined range using a semi-circular or circular dial. It is ideal for showing progress toward a goal, performance metrics, or any KPI that needs to be evaluated against minimum and maximum bounds. The visual metaphor of a speedometer makes it intuitive to quickly assess whether a value is in an acceptable range.

""" anyplot.ai
gauge-basic: Basic Gauge Chart
Library: pygal 3.1.3 | Python 3.14.10
Quality: 87/100 | Updated: 2026-12-04
"""
import math
import os
import re
import sys
from pathlib import Path
# Remove script directory from path to avoid name collision with the pygal package
_script_dir = str(Path(__file__).parent)
sys.path = [p for p in sys.path if p != _script_dir]
import pandas as pd
import pygal
from pygal.style import Style
# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
# Imprint palette: all eight positions in canonical order
IMPRINT_PALETTE = ["#009E73", "#C475FD", "#4467A3", "#BD8233", "#AE3030", "#2ABCCD", "#954477", "#99B314"]
# Constants
# Semantic zone colors, low to high: bad → warn → good (Imprint traffic-light anchors)
ZONE_COLORS = ["#AE3030", "#DDCC77", "#009E73"]
ZONE_NAMES = ["Poor", "Fair", "Good"]
# Columns: set these to your table's column names
VALUE = "% of annual target" # role: value
MIN_VALUE = "Minimum" # role: min_value
MAX_VALUE = "Maximum" # role: max_value
THRESHOLDS = ["Threshold 1", "Threshold 2"] # role: threshold_
# Data: replace this block with your own table, for example df = pd.read_csv("kpi.csv")
# Quarterly sales performance against an annual target (Q3 2024)
df = pd.DataFrame({VALUE: [72], MIN_VALUE: [0], MAX_VALUE: [100], THRESHOLDS[0]: [30], THRESHOLDS[1]: [70]})
# Plot: reads only df and the constants above
value = float(df[VALUE].iloc[0])
min_value = float(df[MIN_VALUE].iloc[0])
max_value = float(df[MAX_VALUE].iloc[0])
thresholds = sorted(float(t) for t in df[THRESHOLDS].iloc[0].dropna())
def fmt(x):
return f"{x:g}"
# Zone boundaries and the zone the value falls in
edges = [min_value, *thresholds, max_value]
n_zones = len(edges) - 1
zone_colors = [
ZONE_COLORS[i] if i < len(ZONE_COLORS) else IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)] for i in range(n_zones)
]
zone_names = [ZONE_NAMES[i] if i < len(ZONE_NAMES) else f"Zone {i + 1}" for i in range(n_zones)]
zone_idx = sum(value >= t for t in thresholds)
# Square canvas: pygal.Gauge draws a 300-degree dial and needs a square plot area to stay circular
_title = "Quarterly Sales Performance · gauge-basic · python · pygal · anyplot.ai"
_title_fs = max(36, min(60, round(1950 / (0.62 * len(_title)))))
custom_style = Style(
background=PAGE_BG,
plot_background=PAGE_BG,
foreground=INK,
foreground_strong=INK,
foreground_subtle=INK_MUTED,
guide_stroke_color=INK_MUTED,
major_guide_stroke_color=INK_MUTED,
colors=(INK,), # the needle
title_font_size=_title_fs,
label_font_size=56,
major_label_font_size=56,
legend_font_size=44,
value_font_size=36,
stroke_width=2.5,
opacity="1",
opacity_hover="1",
)
# The value goes in through chart.add: pygal draws the needle on its polar scale
chart = pygal.Gauge(
width=2400,
height=2400,
title=_title,
style=custom_style,
show_legend=False,
margin=60,
value_formatter=lambda x: f"{fmt(x)} of {fmt(max_value)}",
)
chart.range = (min_value, max_value)
chart.y_labels = [{"value": e, "label": fmt(e)} for e in edges]
chart.add("Sales", value)
svg_str = chart.render().decode("utf-8")
# Dial geometry as pygal laid it out: centre circle of the polar axis, tick spoke reaching 0.95 of the radius
CX, CY = map(float, re.search(r'<circle cx="([\d.]+)" cy="([\d.]+)"', svg_str).groups())
_tip_x, _tip_y = map(float, re.search(r'L ?([\d.]+) ([\d.]+) Z" class="guide line major', svg_str).groups())
R = math.hypot(_tip_x - CX, _tip_y - CY) / 0.95
def dial_point(radius, v):
"""Pixel position at a radius for a scale value: 300-degree sweep from lower right, counterclockwise (pygal's own direction)."""
angle = math.radians(300 + 300 * (v - min_value) / (max_value - min_value))
return CX + radius * math.cos(angle), CY - radius * math.sin(angle)
# Colored zone band behind the needle, one sector per zone
BAND_OUTER, BAND_INNER = 0.83 * R, 0.68 * R
zone_paths = ""
for color, lo, hi in zip(zone_colors, edges[:-1], edges[1:], strict=True):
large = int(300 * (hi - lo) / (max_value - min_value) > 180)
ox0, oy0 = dial_point(BAND_OUTER, lo)
ox1, oy1 = dial_point(BAND_OUTER, hi)
ix1, iy1 = dial_point(BAND_INNER, hi)
ix0, iy0 = dial_point(BAND_INNER, lo)
zone_paths += (
f'<path d="M {ox0:.1f} {oy0:.1f} A {BAND_OUTER:.1f} {BAND_OUTER:.1f} 0 {large} 0 {ox1:.1f} {oy1:.1f} '
f'L {ix1:.1f} {iy1:.1f} A {BAND_INNER:.1f} {BAND_INNER:.1f} 0 {large} 1 {ix0:.1f} {iy0:.1f} Z" '
f'fill="{color}" stroke="{PAGE_BG}" stroke-width="6"/>'
)
# Prominent value and verdict below the pivot, in the color of the zone the value falls in
readout = (
f'<text x="{CX:.1f}" y="{CY + 0.52 * R:.1f}" text-anchor="middle" font-size="{0.34 * R:.0f}"'
f' font-weight="bold" fill="{zone_colors[zone_idx]}" font-family="sans-serif">{fmt(value)}%</text>'
f'<text x="{CX:.1f}" y="{CY + 0.70 * R:.1f}" text-anchor="middle" font-size="{0.12 * R:.0f}"'
f' fill="{INK_SOFT}" font-family="sans-serif">{zone_names[zone_idx]}</text>'
f'<text x="{CX:.1f}" y="{CY + 0.86 * R:.1f}" text-anchor="middle" font-size="{0.07 * R:.0f}"'
f' fill="{INK_SOFT}" font-family="sans-serif">{VALUE.removeprefix("% ")}</text>'
)
svg_str = svg_str.replace('<g class="axis x gauge">', zone_paths + '<g class="axis x gauge">', 1)
svg_str = svg_str.replace('<g class="axis y gauge">', readout + '<g class="axis y gauge">', 1)
import cairosvg
cairosvg.svg2png(bytestring=svg_str.encode("utf-8"), write_to=f"plot-{THEME}.png")
# HTML: the SVG keeps pygal's JS tooltips
with open(f"plot-{THEME}.html", "w", encoding="utf-8") as f:
f.write(svg_str)
Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/gauge-basic/pygal/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.
{
"spec_id": "gauge-basic",
"language": "python",
"library": "pygal",
"page": "https://anyplot.ai/gauge-basic/python/pygal",
"hub": "https://anyplot.ai/gauge-basic",
"code_json": "https://api.anyplot.ai/specs/gauge-basic/pygal/code",
"spec_json": "https://api.anyplot.ai/specs/gauge-basic",
"render_light_png": "https://storage.googleapis.com/anyplot-images/plots/gauge-basic/python/pygal/plot-light.png",
"render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/gauge-basic/python/pygal/plot-dark.png",
"interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/gauge-basic/python/pygal/plot-light.html",
"interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/gauge-basic/python/pygal/plot-dark.html",
"quality_score": 87.0,
"license": "MIT",
"guide": "https://anyplot.ai/llms.txt"
}Part of Basic Gauge Chart on anyplot.ai.