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 ( |
|---|---|
latest values, totals |
ranked bars ( |
values over years, up to six countries |
lines with direct labels ( |
values over years, more countries |
small multiples ( |
before-and-after dot plot ( |
|
|
scatter ( |
status chart: baseline, latest and 2030 projection ( |
|
two-group dot plot with ratios ( |
|
one row per indicator beside region and world ( |
|
year-of-latest-value heatmap ( |
|
any latest-value table of countries |
tile map, one equal square per country, no borders ( |
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),.svgor.pdf(editable text), or.htmlfor 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
topvalues.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:
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:
Example
>>> import undata as un >>> un.palette("sequential") ['#86B6EF', '#5598E7', '#256ABF', '#104281']