Grapher
owid.grapher.Chart ¶
Create interactive OWID charts from pandas DataFrames.
The Chart class provides a declarative API for building interactive visualizations using OWID's Grapher library. Charts are configured through method chaining and render directly in Jupyter notebooks.
Multiple chart types can be enabled by chaining mark_*() methods. The first one called becomes the default view, or use show() to set a specific default tab.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
A pandas DataFrame containing the data to visualize. The DataFrame should have columns for time/x-axis, values/y-axis, and optionally entities for grouping.
TYPE:
|
Example
import pandas as pd
from owid.grapher import Chart
df = pd.DataFrame({
'year': [2020, 2021, 2022],
'country': ['USA', 'China', 'India'],
'gdp': [21.4, 14.7, 2.9]
})
# Single chart type
Chart(df).mark_line().encode(
x='year',
y='gdp',
entity='country'
).label(
title='GDP by Country'
)
# Multiple chart types with bar as default
Chart(df).mark_line().mark_bar().show("discrete-bar").encode(...)
Source code in owid/grapher/__init__.py
encode ¶
encode(
x: Optional[str] = None,
y: Optional[str] = None,
entity: Optional[str] = None,
color: Optional[str] = None,
size: Optional[str] = None,
) -> Chart
Map DataFrame columns to visual properties.
This method establishes the visual encoding by mapping DataFrame columns to chart dimensions. The behavior varies by chart type:
- Line/Bar charts:
xis time,yis values,entitygroups lines/bars - Scatter plots:
xandyare both numeric values,entitygroups points
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Column name for x-axis. For line/bar charts, typically a time column ('year', 'date'). For scatter plots, a numeric value column.
TYPE:
|
y
|
Column name for y-axis values to plot. For bar charts, can be the entity column if you want entities on the y-axis.
TYPE:
|
entity
|
Column name for grouping data (e.g., 'country', 'region'). Each unique value becomes a separate line/series. Optional for single-series charts.
TYPE:
|
color
|
Column name for color encoding in scatter plots. Values map to colors.
TYPE:
|
size
|
Column name for size encoding in scatter plots. Values map to point sizes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
| RAISES | DESCRIPTION |
|---|---|
|
If a specified column name is not found in the DataFrame. |
Example
Source code in owid/grapher/__init__.py
label ¶
Add labels and text to the chart.
| PARAMETER | DESCRIPTION |
|---|---|
title
|
Chart title.
TYPE:
|
subtitle
|
Chart subtitle.
TYPE:
|
source_desc
|
Data source attribution.
TYPE:
|
note
|
Additional notes or footnotes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
xaxis ¶
xaxis(
label: Optional[str] = None,
unit: Optional[str] = None,
scale: Optional[Literal["linear", "log"]] = None,
scale_control: Optional[bool] = None,
) -> Chart
Configure the x-axis.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
Axis label text.
TYPE:
|
unit
|
Unit of measurement (e.g., '$', 'kg').
TYPE:
|
scale
|
Scale type ('linear' or 'log').
TYPE:
|
scale_control
|
Allow users to toggle scale.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
yaxis ¶
yaxis(
label: Optional[str] = None,
unit: Optional[str] = None,
scale: Optional[Literal["linear", "log"]] = None,
scale_control: Optional[bool] = None,
) -> Chart
Configure the y-axis.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
Axis label text.
TYPE:
|
unit
|
Unit of measurement (e.g., '$', 'kg').
TYPE:
|
scale
|
Scale type ('linear' or 'log').
TYPE:
|
scale_control
|
Allow users to toggle scale.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
axis ¶
axis(
x_label: Optional[str] = None,
y_label: Optional[str] = None,
x_unit: Optional[str] = None,
y_unit: Optional[str] = None,
x_scale: Optional[Literal["linear", "log"]] = None,
y_scale: Optional[Literal["linear", "log"]] = None,
x_scale_control: Optional[bool] = None,
y_scale_control: Optional[bool] = None,
) -> Chart
Configure both axes at once.
Convenience method for setting properties on both axes. For single-axis configuration, use xaxis() or yaxis() instead.
| PARAMETER | DESCRIPTION |
|---|---|
x_label
|
Label text for x-axis.
TYPE:
|
y_label
|
Label text for y-axis.
TYPE:
|
x_unit
|
Unit suffix for x-axis values (e.g., '$', '%', 'kg').
TYPE:
|
y_unit
|
Unit suffix for y-axis values.
TYPE:
|
x_scale
|
Scale type for x-axis ('linear' or 'log').
TYPE:
|
y_scale
|
Scale type for y-axis ('linear' or 'log').
TYPE:
|
x_scale_control
|
If True, adds UI control to toggle x-axis scale.
TYPE:
|
y_scale_control
|
If True, adds UI control to toggle y-axis scale.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
mark_scatter ¶
Add scatter plot to available chart types.
Scatter plots display individual data points with x and y positions. Use color
and size encodings for additional dimensions. Best for showing relationships
between two numeric variables.
Can be combined with other mark_*() methods to enable multiple chart types.
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
mark_line ¶
Add line chart to available chart types.
Line charts connect data points with lines, ideal for showing trends over time. Multiple entities create multiple lines.
Can be combined with other mark_*() methods to enable multiple chart types.
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
mark_bar ¶
Add bar chart to available chart types.
Bar charts display categorical data with rectangular bars. Bars can be shown side-by-side (default) or stacked on top of each other.
Can be combined with other mark_*() methods to enable multiple chart types.
| PARAMETER | DESCRIPTION |
|---|---|
stacked
|
If True, creates a stacked bar chart where bars for different entities are stacked vertically. If False (default), bars are shown side-by-side.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
mark_map ¶
mark_map(
time_tolerance: Optional[int] = None,
color_scheme: Optional[ColorSchemeName] = None,
binning_strategy: Optional[BinningStrategy] = None,
custom_numeric_values: Optional[List[float]] = None,
) -> Chart
Enable the map tab with optional configuration.
Adds a world map visualization showing data geographically. Can be combined with other mark_*() methods.
| PARAMETER | DESCRIPTION |
|---|---|
time_tolerance
|
How many years to look back/forward for data
TYPE:
|
color_scheme
|
Color scheme name (e.g., "Reds", "Blues", "YlOrRd")
TYPE:
|
binning_strategy
|
How to bin values ("auto", "manual", "equalInterval", "quantiles")
TYPE:
|
custom_numeric_values
|
Custom bin boundaries when using manual binning
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Example
Source code in owid/grapher/__init__.py
show ¶
show(
tab: Literal[
"line",
"discrete-bar",
"stacked-discrete-bar",
"scatter",
"stacked-area",
"slope",
"stacked-bar",
"marimekko",
"map",
"table",
],
) -> Chart
Set which tab to display by default.
Use this to control which visualization is shown when the chart first loads. The tab must correspond to an enabled chart type (via mark_*() methods).
| PARAMETER | DESCRIPTION |
|---|---|
tab
|
The tab to show by default. Options: - "line": Line chart - "discrete-bar": Bar chart - "stacked-discrete-bar": Stacked bar chart - "scatter": Scatter plot - "map": World map - "table": Data table
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Example
Source code in owid/grapher/__init__.py
interact ¶
interact(
allow_relative: Optional[bool] = None,
scale_control: Optional[bool] = None,
entity_control: Optional[bool] = None,
) -> Chart
Add interactive controls to the chart.
| PARAMETER | DESCRIPTION |
|---|---|
allow_relative
|
Show relative/absolute toggle.
TYPE:
|
scale_control
|
Show log/linear scale toggle.
TYPE:
|
entity_control
|
Show entity/country picker.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
select ¶
select(
entities: Optional[List[str]] = None,
timespan: Optional[Tuple[Any, Any]] = None,
) -> Chart
Pre-select entities and time range.
| PARAMETER | DESCRIPTION |
|---|---|
entities
|
List of entity names to display.
TYPE:
|
timespan
|
Tuple of (start, end) for time range.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
transform ¶
Transform data to relative or absolute values.
Display values as percentage change from a baseline (relative mode) or as absolute values (default). In relative mode, the first time period serves as the baseline (100%).
| PARAMETER | DESCRIPTION |
|---|---|
relative
|
If True, show values as percentage change from baseline. If False, show absolute values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
filter ¶
Filter entities to only show those with complete data.
When enabled, only entities that have data for all time periods and dimensions will be shown. Useful for ensuring fair comparisons by excluding entities with incomplete data.
| PARAMETER | DESCRIPTION |
|---|---|
matching_entities_only
|
If True, only show entities with complete data across all dimensions and time periods. If False, show all entities even with gaps.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Source code in owid/grapher/__init__.py
variable ¶
variable(
column: str,
name: Optional[str] = None,
description_short: Optional[str] = None,
description_from_producer: Optional[str] = None,
description_processing: Optional[str] = None,
description_key: Optional[List[str]] = None,
unit: Optional[str] = None,
short_unit: Optional[str] = None,
source_name: Optional[str] = None,
source_link: Optional[str] = None,
) -> Chart
Add rich metadata to a data column/variable.
Configures display properties and documentation for a column that will appear in tooltips, the data table, and source information.
| PARAMETER | DESCRIPTION |
|---|---|
column
|
Name of the DataFrame column to configure.
TYPE:
|
name
|
Display name (e.g., "Population" instead of "pop").
TYPE:
|
description_short
|
Brief description shown in tooltips.
TYPE:
|
description_from_producer
|
Original description from data source.
TYPE:
|
description_processing
|
How the data was processed/transformed.
TYPE:
|
description_key
|
List of key points about the variable.
TYPE:
|
unit
|
Full unit name (e.g., "million people").
TYPE:
|
short_unit
|
Abbreviated unit for compact display (e.g., "M").
TYPE:
|
source_name
|
Name of the data source.
TYPE:
|
source_link
|
URL to the data source.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self for method chaining. |
Note
The timespan is computed automatically from the data's time column.
Example
Source code in owid/grapher/__init__.py
export ¶
Export the chart configuration as a dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
include_data
|
If True, includes the data in the export.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Dictionary containing the full chart configuration. |
Source code in owid/grapher/__init__.py
owid.grapher.ChartConfig
dataclass
¶
ChartConfig(
title: str = "",
subtitle: str = "",
note: str = "",
source_desc: str = "",
hide_logo: bool = True,
is_published: bool = True,
hide_title_annotation: bool = True,
hide_legend: bool = False,
hide_entity_controls: bool = True,
hide_relative_toggle: bool = True,
has_map_tab: bool = False,
map_config: Optional[MapConfig] = None,
stack_mode: Literal[
"relative", "absolute"
] = "absolute",
matching_entities_only: bool = False,
x_axis: dict = dict(),
y_axis: dict = dict(),
)
Configuration for OWID chart display and behavior.
This dataclass holds all chart-level settings including title, subtitle, axis configuration, and UI control visibility. Properties use snake_case in Python but are automatically converted to camelCase for the JavaScript Grapher library.
Note: Chart types are now managed by the Chart class via chart_types list, not in this config class.
| ATTRIBUTE | DESCRIPTION |
|---|---|
|
Main chart title.
TYPE:
|
|
Additional context below title.
TYPE:
|
|
Footnote text displayed at bottom.
TYPE:
|
|
Data source attribution.
TYPE:
|
|
If True, hides OWID logo (default for embedded charts).
TYPE:
|
|
Publication status flag.
TYPE:
|
|
If True, hides the annotation arrow on title.
TYPE:
|
|
If True, hides the chart legend.
TYPE:
|
|
If True, hides the entity/country picker UI.
TYPE:
|
|
If True, hides relative/absolute toggle button.
TYPE:
|
|
If True, enables the map visualization tab.
TYPE:
|
|
For stacked charts, 'absolute' or 'relative' (percentage).
TYPE:
|
|
If True, only show entities with complete data.
TYPE:
|
|
Dictionary of x-axis configuration (label, scale, etc).
TYPE:
|
|
Dictionary of y-axis configuration (label, scale, etc).
TYPE:
|
owid.grapher.Dimension
dataclass
¶
Dimension(
property: Literal["y", "x", "color"],
variable_name: str,
display: Optional[dict] = dict(),
)
Maps a DataFrame column to a chart dimension.
Dimensions define how data columns are used in the chart visualization. Each dimension specifies a visual property (x, y, color) and the source column name, along with optional display settings.
| ATTRIBUTE | DESCRIPTION |
|---|---|
|
The visual property this dimension controls ('y' for y-axis, 'x' for x-axis, 'color' for color encoding).
TYPE:
|
|
Name of the column from the DataFrame to use for this dimension.
TYPE:
|
|
Optional dictionary of display settings (e.g., {'unit': '$', 'numDecimalPlaces': 2}).
TYPE:
|
Example
owid.grapher.TimeType ¶
Bases:
Enumeration for time dimension types.
Determines how time values are interpreted and displayed in charts.
| ATTRIBUTE | DESCRIPTION |
|---|---|
|
Daily or date-based data. Automatically detected when x='date'. Uses ISO date format (YYYY-MM-DD).
|
|
Annual data (default). Standard yearly time series.
|