Population pyramid
Draw two groups back to back with mirrored bars, such as men and women by age, then play the pyramid through the years.
A population pyramid puts two groups back to back: men extend left from a center column of age groups and women extend right. Its shape shows a population’s history at a glance, a broad base for a young country and a top-heavy column for an aging one. The same mirrored bars suit any two-group comparison over shared categories, such as imports and exports by sector.
{
"mark": {
"type": "bar",
"mirror": true
},
"data": [
{
"year": 2025,
"age": "0-4",
"sex": "Male",
"pop": 2006000
},
{
"year": 2025,
"age": "0-4",
"sex": "Female",
"pop": 1914000
},
{
"year": 2025,
"age": "5-9",
"sex": "Male",
"pop": 2387000
},
{
"year": 2025,
"age": "5-9",
"sex": "Female",
"pop": 2268000
},
{
"year": 2025,
"age": "10-14",
"sex": "Male",
"pop": 2703000
},
{
"year": 2025,
"age": "10-14",
"sex": "Female",
"pop": 2557000
},
// … 36 more rows; Copy includes them
],
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "Japan's largest age group is 50 to 54",
"subtitle": "Population of Japan by age and sex, 2025"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "Japan's largest age group is 50 to 54",
"subtitle": "Population of Japan by age and sex, 2025"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "Japan's largest age group is 50 to 54",
"subtitle": "Population of Japan by age and sex, 2025"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "Japan's largest age group is 50 to 54",
"subtitle": "Population of Japan by age and sex, 2025"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "Japan's largest age group is 50 to 54",
"subtitle": "Population of Japan by age and sex, 2025"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Mirrored bars
mark: { type: "bar", mirror: true } turns horizontal bars into a pyramid. Put the categories on y, the values on x, and a field with exactly two values on color.
{
"mark": { "type": "bar", "mirror": true },
"data": [
{ "age": "0-4", "sex": "Male", "pop": 2006000 },
{ "age": "0-4", "sex": "Female", "pop": 1914000 }
],
"encoding": {
"y": { "field": "age", "type": "ordinal", "sort": ["100+", "...", "10-14", "5-9", "0-4"] },
"x": { "field": "pop", "type": "quantitative", "format": ".2~s" },
"color": { "field": "sex", "type": "nominal", "sort": ["Male", "Female"] }
}
}The first value in the color channel’s sort list goes on the left, or the first one in the data when there is no sort. An ordinal y axis runs from top to bottom, so listing the age groups from oldest to youngest puts 100+ on top and 0-4 at the base.
What mirror changes
Both halves share one value scale that starts at zero on each side of the center column, so equal values draw equal lengths on either side. The ticks read as positive values in both directions, here 0 to 6M.
- The category labels sit in the center column, which is as wide as the longest label. There is no separate y axis.
- The two color values label the halves above the plot, and the chart has no legend. Hovering a half label highlights that half, as hovering a legend entry would.
- There is no crosshair.
crosshair: trueis ignored with a warning. - Value labels sit past the outer end of each bar, in the
xformat unlesslabels.formatsets another.labels.placementis ignored, andlabels.density: "none"turns the labels off.
The spec fails validation when color is missing or has more or fewer than two values in the data, when y is quantitative (which would draw vertical bars), and when x has a stack of any value, null included, since mirrored bars never stack. A negative value draws as a zero-length bar.
{
"mark": {
"type": "bar",
"mirror": true
},
"data": [
{
"year": 1950,
"age": "0-4",
"sex": "Male",
"pop": 6002000
},
{
"year": 1950,
"age": "0-4",
"sex": "Female",
"pop": 5944000
},
{
"year": 1950,
"age": "5-9",
"sex": "Male",
"pop": 4747000
},
{
"year": 1950,
"age": "5-9",
"sex": "Female",
"pop": 4788000
},
{
"year": 1950,
"age": "10-14",
"sex": "Male",
"pop": 4450000
},
{
"year": 1950,
"age": "10-14",
"sex": "Female",
"pop": 4517000
},
// … 36 more rows; Copy includes them
],
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "In 1950 every age group was smaller than the one below it",
"subtitle": "Population of Japan by age and sex, 1950"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "In 1950 every age group was smaller than the one below it",
"subtitle": "Population of Japan by age and sex, 1950"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "In 1950 every age group was smaller than the one below it",
"subtitle": "Population of Japan by age and sex, 1950"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "In 1950 every age group was smaller than the one below it",
"subtitle": "Population of Japan by age and sex, 1950"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 42 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
}
},
"chrome": {
"title": "In 1950 every age group was smaller than the one below it",
"subtitle": "Population of Japan by age and sex, 1950"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
In 1950 every age group in Japan was smaller than the one below it, the shape that gives the chart its name. By 2025 the base had narrowed to fewer than four million children under five, and the widest bars were the 50-54 and 75-79 groups.
Play it through the years
{
"mark": {
"type": "bar",
"mirror": true
},
"data": [
{
"year": 1950,
"age": "0-4",
"sex": "Male",
"pop": 6002000
},
{
"year": 1950,
"age": "0-4",
"sex": "Female",
"pop": 5944000
},
{
"year": 1955,
"age": "0-4",
"sex": "Male",
"pop": 4852000
},
{
"year": 1955,
"age": "0-4",
"sex": "Female",
"pop": 4687000
},
{
"year": 1960,
"age": "0-4",
"sex": "Male",
"pop": 4085000
},
{
"year": 1960,
"age": "0-4",
"sex": "Female",
"pop": 3949000
},
// … 1,296 more rows; Copy includes them
],
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
},
"time": {
"field": "year",
"type": "temporal"
}
},
"playback": {
"duration": 15000
},
"chrome": {
"title": "Japan's population ages as the base narrows and the bulge moves up",
"subtitle": "Population of Japan by age and sex, every fifth year from 1950 to 2100; 2025 onward projected"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 1,302 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
},
"time": {
"field": "year",
"type": "temporal"
}
},
"playback": {
"duration": 15000
},
"chrome": {
"title": "Japan's population ages as the base narrows and the bulge moves up",
"subtitle": "Population of Japan by age and sex, every fifth year from 1950 to 2100; 2025 onward projected"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 1,302 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
},
"time": {
"field": "year",
"type": "temporal"
}
},
"playback": {
"duration": 15000
},
"chrome": {
"title": "Japan's population ages as the base narrows and the bulge moves up",
"subtitle": "Population of Japan by age and sex, every fifth year from 1950 to 2100; 2025 onward projected"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 1,302 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
},
"time": {
"field": "year",
"type": "temporal"
}
},
"playback": {
"duration": 15000
},
"chrome": {
"title": "Japan's population ages as the base narrows and the bulge moves up",
"subtitle": "Population of Japan by age and sex, every fifth year from 1950 to 2100; 2025 onward projected"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 1,302 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"mirror": true
},
"encoding": {
"y": {
"field": "age",
"type": "ordinal",
"sort": [
"100+",
"95-99",
"90-94",
"85-89",
"80-84",
"75-79",
"70-74",
"65-69",
"60-64",
"55-59",
"50-54",
"45-49",
"40-44",
"35-39",
"30-34",
"25-29",
"20-24",
"15-19",
"10-14",
"5-9",
"0-4"
]
},
"x": {
"field": "pop",
"type": "quantitative",
"format": ".2~s"
},
"color": {
"field": "sex",
"type": "nominal",
"sort": [
"Male",
"Female"
]
},
"time": {
"field": "year",
"type": "temporal"
}
},
"playback": {
"duration": 15000
},
"chrome": {
"title": "Japan's population ages as the base narrows and the bulge moves up",
"subtitle": "Population of Japan by age and sex, every fifth year from 1950 to 2100; 2025 onward projected"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Add encoding.time and one row per age group, sex and year. Each bar keeps its identity from its category and color value, the same pair that identifies it without time, so a pyramid needs no key. Two rows with the same age, sex and year fail validation and ask you to aggregate them.
"encoding": {
"y": { "field": "age", "type": "ordinal", "sort": ["100+", "...", "5-9", "0-4"] },
"x": { "field": "pop", "type": "quantitative", "format": ".2~s" },
"color": { "field": "sex", "type": "nominal", "sort": ["Male", "Female"] },
"time": { "field": "year", "type": "temporal" }
},
"playback": { "duration": 15000 }The value axis is computed once over every period and stays still for the run. This one runs to 8M on each side, enough for the 6 million boys under five in 1950, while a static 2025 pyramid stops at 6M. Between two periods each bar grows or shrinks in a straight line.
The large faint year sits in the upper right on a pyramid, where the oldest age groups leave room. When a bar or value label in any period would cover it, the label shrinks, to 20 px at the smallest. playback.period changes its format or position, and the playback page lists every timing and control option.
Sorting by value
A pyramid keeps its categories in a fixed order, and so does any bar chart with time. A sort by value, such as "-x" on y, ranks the categories once over every period, so the bars never overtake one another, and the engine warns. For bars that re-rank each period, use a race.
Fields that matter
| Field | What it does |
|---|---|
mark.mirror |
true draws the two color values back to back. Horizontal bars only. |
encoding.y |
The categories, in the center column. A sort list sets their order from top to bottom. |
encoding.x |
The values. format applies to the ticks and value labels. Never takes stack. |
encoding.color |
A field with exactly two values. Its sort puts the first one on the left. |
encoding.time |
The period field. Each bar is identified by its category and color value. |
labels |
density: "none" hides the value labels; format overrides the x format. |
Related
- Bar: the same categories with one value each.
- Playback: timing, controls, keyboard and the
chart.playbackAPI. - Encoding channels: how bars keep their identity across periods.
- Marks reference lists the options each mark type takes.