PyramidChart
Two series as horizontal bars mirrored around a shared category axis. The Pyramid Chart guide shows every feature on real data; this page is the contract: the function, the shape of its data, the keys style takes, and the constant each parameter accepts.
Function
datachart.charts.PyramidChart
PyramidChart(
data: list[list[BarDataPointAttrs]],
*,
title: str | None = None,
xlabel: str | None = None,
ylabel: str | None = None,
subtitle: str | list[str | None] | None = None,
figsize: FIG_SIZE | tuple[float, float] | None = None,
xmin: int | float | None = None,
xmax: int | float | None = None,
show_legend: bool | None = None,
legend: LegendSettingAttrs | None = None,
show_grid: SHOW_GRID | str | bool | None = None,
show_yerr: bool | None = None,
show_values: bool | None = None,
value_format: VALUE_FORMAT | str | None = None,
sort: SORT | str | None = None,
sort_by: str | None = None,
emphasis_rule: EmphasisRuleAttrs | None = None,
style: (
BarStyleAttrs | list[BarStyleAttrs | None] | None
) = None,
xticks: list[int | float] | None = None,
xticklabels: list[str] | None = None,
xtickrotate: int | None = None,
yticks: list[int | float] | None = None,
yticklabels: list[str] | None = None,
ytickrotate: int | None = None,
xticks_format: (
VALUE_FORMAT | DATE_FORMAT | str | None
) = None,
yticks_format: (
VALUE_FORMAT | DATE_FORMAT | str | None
) = None,
vlines: (
VLineSettingAttrs | list[VLineSettingAttrs] | None
) = None,
hlines: (
HLineSettingAttrs | list[HLineSettingAttrs] | None
) = None,
vspans: (
VSpanSettingAttrs | list[VSpanSettingAttrs] | None
) = None,
hspans: (
HSpanSettingAttrs | list[HSpanSettingAttrs] | None
) = None,
texts: (
TextSettingAttrs | list[TextSettingAttrs] | None
) = None,
label: str | list[str | None] | None = None,
y: str | list[str | None] | None = None,
yerr: str | list[str | None] | None = None
) -> plt.Figure
Creates the pyramid chart.
A pyramid chart draws exactly two series as horizontal bars mirrored around a shared category axis, the first series to the left and the second to the right: the classic age-sex population pyramid. Use it to compare the distribution of two groups over the same ordered categories, such as age bands, where the symmetry (or lack of it) is the message.
Both series are supplied as positive values; value ticks and labels show
absolute values. Unlike the other chart fronts, the axis parameters are
spatial: xlabel, xticks, and xmax address the horizontal value axis,
and ylabel the vertical category axis.
Examples:
>>> from datachart.charts import PyramidChart
>>> figure = PyramidChart(
... data=[
... [
... {"label": "0-14", "y": 12},
... {"label": "15-29", "y": 18},
... {"label": "30-44", "y": 22},
... ],
... [
... {"label": "0-14", "y": 11},
... {"label": "15-29", "y": 19},
... {"label": "30-44", "y": 24},
... ],
... ],
... subtitle=["Group A", "Group B"],
... title="Basic Pyramid Chart",
... show_legend=True,
... )
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Exactly two lists of data points — the first is the left side, the second the right. Values are positive for both sides; the chart mirrors the left side itself.
TYPE:
|
title
|
The title of the chart.
TYPE:
|
xlabel
|
The label of the horizontal value axis.
TYPE:
|
ylabel
|
The label of the vertical category axis.
TYPE:
|
subtitle
|
The names of the two sides. Used as legend labels.
TYPE:
|
figsize
|
The size of the figure as (width, height) in inches. See
TYPE:
|
xmin
|
Not supported; the value axis is always symmetric around zero. Raises when passed.
TYPE:
|
xmax
|
The maximum per-side value; the value axis spans (-xmax, xmax).
TYPE:
|
show_legend
|
Whether to show the legend.
TYPE:
|
legend
|
The per-figure legend setting: title, location, column count
and alignment; each field falls back to the theme. See
TYPE:
|
show_grid
|
Which grid lines to show ("both", "x", "y");
TYPE:
|
show_yerr
|
Whether to show error bars on the bars.
TYPE:
|
show_values
|
Whether to show bar value labels at the edge of each bar.
TYPE:
|
value_format
|
Format string for bar value labels: a
TYPE:
|
sort
|
The order the categories are drawn in: None (input order),
"ascending", or "descending" by value. One order serves both
sides, keyed by the total of the two; ties keep input order.
See
TYPE:
|
sort_by
|
The subtitle of the one side whose values key the sort instead of the total. A category that side lacks sorts last.
TYPE:
|
emphasis_rule
|
A one-key dict that highlights the bars matching it
and mutes the rest:
TYPE:
|
style
|
Style configuration(s) for the bars, per side.
TYPE:
|
xticks
|
Custom value-axis tick positions, as positive values; each is mirrored to both halves.
TYPE:
|
xticklabels
|
Custom value-axis tick labels (same length as
TYPE:
|
xtickrotate
|
Rotation angle for value-axis tick labels.
TYPE:
|
yticks
|
Custom category-axis tick positions.
TYPE:
|
yticklabels
|
Custom category-axis tick labels.
TYPE:
|
ytickrotate
|
Rotation angle for category-axis tick labels.
TYPE:
|
xticks_format
|
The x-axis tick label format: a
TYPE:
|
yticks_format
|
The y-axis tick label format, as
TYPE:
|
vlines
|
Vertical line(s) to plot.
TYPE:
|
hlines
|
Horizontal line(s) to plot.
TYPE:
|
vspans
|
Vertical reference band(s) to shade, between two x positions.
TYPE:
|
hspans
|
Horizontal reference band(s) to shade, between two y positions.
TYPE:
|
texts
|
Text annotation(s) to draw.
TYPE:
|
label
|
The key name in data for label values (default: "label").
TYPE:
|
y
|
The key name in data for the bar values (default: "y").
TYPE:
|
yerr
|
The key name in data for the bar error values (default: "yerr").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
plt.Figure
|
The figure containing the pyramid chart. |
Data
Each record in data is a BarDataPointAttrs; the label, y and yerr parameters rename its keys.
Style
style takes the keys of BarStyleAttrs. The chart also reads the shared groups it draws: value labels (ValueLabelStyleAttrs), reference lines (VLineStyleAttrs and HLineStyleAttrs), reference bands (VSpanStyleAttrs and HSpanStyleAttrs) and text annotations (TextStyleAttrs). Every key falls back to the theme, so the same keys set the default look through config.
Constants
The parameters that accept a constant, with the class in datachart.constants that lists its values.
| Parameter | Constant |
|---|---|
figsize |
FIG_SIZE |
legend={"location": ..., "alignment": ...} |
LEGEND_LOCATION, LEGEND_ALIGN |
show_grid |
SHOW_GRID |
value_format |
VALUE_FORMAT |
sort |
SORT |
xticks_format |
VALUE_FORMAT, DATE_FORMAT |
yticks_format |
VALUE_FORMAT, DATE_FORMAT |