Charts

pip install "undata[charts]" (matplotlib, and seaborn for undata.style()); pip install "undata[interactive]" for Plotly charts.

undata.chart() reads the table and picks the chart that fits it:

Table

Chart (kind=)

latest values, totals

ranked bars ("bar"); add benchmarks=["World"] for reference lines

values over years, up to six countries

lines with direct labels ("line")

values over years, more countries

small multiples ("facets")

undata.change(), undata.trend(), undata.benchmark()

before-and-after dot plot ("dumbbell") or "slope"

undata.compare() with two indicators

scatter ("scatter")

undata.progress()

status chart: baseline, latest and 2030 projection ("progress")

undata.gap()

two-group dot plot with ratios ("gap")

undata.profile()

one row per indicator beside region and world ("profile")

undata.availability()

year-of-latest-value heatmap ("heatmap")

any latest-value table of countries

tile map, one equal square per country, no borders (kind="map")

Every chart carries its title, unit and source line, labels its marks, and uses palettes checked for colour blindness. Save as PNG (300 dpi), SVG or PDF (text stays editable).

chart(table, path=None, *, kind='auto', highlight=None, title=None, note=None, benchmarks=None, top=None, context='report', interactive=False)[source]

Draw a production-quality chart of a table from undata, chosen from its shape.

Parameters:
  • table (DataFrame) – A table from any undata function. The module help lists which chart each draws.

  • path (str | Path | None) – Save the chart too: .png (300 dpi), .svg or .pdf (editable text), or .html for an interactive chart.

  • kind (Literal['auto', 'bar', 'line', 'facets', 'dumbbell', 'slope', 'progress', 'gap', 'heatmap', 'profile', 'scatter', 'map']) – Override the choice: "bar", "line", "facets", "dumbbell", "slope", "progress", "gap", "heatmap", "profile", "scatter" or "map".

  • highlight (str | Sequence[str] | None) – Countries to draw in colour, with the rest in grey.

  • title (str | None) – The title. Defaults to the indicator’s name.

  • note (str | None) – A line added under the source, such as a definition.

  • benchmarks (Sequence[str] | None) – For bars: regions or groups whose published figure is drawn as a reference line, such as ["World", "Western Africa"].

  • top (int | None) – For bars: keep only the highest top values.

  • context (Literal['report', 'slides']) – "report" for documents, "slides" for presentations.

  • interactive (bool) – Draw an interactive Plotly chart instead (pip install plotly).

Returns:

A matplotlib figure (it displays itself in a notebook), or a Plotly figure.

Return type:

Any

Example

>>> import undata as un
>>> table = un.latest("undata/sdg/EG_ACS_ELEC", "Western Africa")
>>> un.chart(table, "electricity.svg", benchmarks=["World"])
style(context='report')[source]

Give matplotlib and seaborn charts undata’s clean, report-ready defaults.

Call it once at the top of a notebook. Charts you draw with matplotlib, pandas’ .plot() or seaborn then share undata’s fonts, colours, light grid and export settings (300 dpi PNG, editable SVG and PDF text).

Parameters:

context (Literal['report', 'slides']) – "report" for documents, "slides" for presentations (larger text).

Example

>>> import undata as un
>>> un.style()
>>> import seaborn as sns
>>> sns.barplot(table, x="value", y="country")
palette(name='categorical')[source]

One of undata’s checked palettes, as hex colours.

Parameters:

name (Literal['categorical', 'sequential', 'status']) – "categorical" for categories (in a fixed order), "sequential" for magnitudes, light to dark, or "status" for the progress categories, best first.

Returns:

Hex colour strings.

Return type:

list[str]

Example

>>> import undata as un
>>> un.palette("sequential")
['#86B6EF', '#5598E7', '#256ABF', '#104281']