Public
Edited
Apr 9, 2024
Insert cell
Insert cell
Insert cell
Insert cell
census2020
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
addTooltips(
Plot.plot({
marginLeft: 60,
x: {
grid: true,
nice: true,
tickFormat: "s"
},
color: {scheme: "spectral", domain: genders, legend: true},
marks: [
Plot.axisY({tickFormat: p => formatProvince(p)}),
Plot.barX(provinceGenders2020, {
x: "population",
y: "province",
fill: "gender",
title: "population",
sort: {y: "x", reverse: true}
}),
Plot.ruleX([0])
]
})
)
Insert cell
addTooltips(
Plot.plot({
projection: {
type: "albers",
rotate: [-110, 0],
domain: provinces
},
marks: [
Plot.geo(provinces, {
fill: p => population2020.get(p.properties.NL_NAME_1),
title: p => `${p.properties.NL_NAME_1} \n ${population2020.get(p.properties.NL_NAME_1).toLocaleString()}`
})
],
margin: 20,
color: {
scheme: "reds", // Change color scheme
unknown: "#ddd", // Polygons with unknown values are gray
type: "linear", // Linear scale for color progression
legend: true, // Add the legend
nice: true,
tickFormat: "s",
label: "Province Population", // Update legend label
}
})
)
Insert cell
addTooltips(
Plot.plot({
width: census2020.length * 40,
y: {
grid: true,
tickFormat: d => d + 1,
label: "↑ Male/Female Ratio"
},
color: {
scheme: "PiYg"
},
marks: [
Plot.axisX({tickFormat: p => formatProvince(p)}),
Plot.barY(census2020, {
x: "地区",
y: d => d["男"]/d["女"] - 1,
fill: d => Math.sign(d["男"]/d["女"] - 1),
title: d => d["男"]/d["女"],
sort: {x: "y", reverse: true},
}),
Plot.ruleY([0])
]
})
)
Insert cell
Insert cell
Insert cell
addTooltips(
Plot.plot({
width: data.length * 40,
x: {axis: null, domain: years},
y: {
grid: true,
nice: true,
tickFormat: "s"
},
fx: {tickFormat: p => formatProvince(p)},
color: {scheme: "spectral", domain: years, legend: true},
facet: {data: provinceYears, x: "province"},
marks: [
Plot.barY(provinceYears, {
x: "year",
y: "population",
title: "population",
fill: "year",
sort: {fx: "y", reduce: "mean", reverse: true}
}),
Plot.ruleY([0])
]
})
)
Insert cell
Insert cell
addTooltips(
Plot.plot({
width: data.length * 40,
x: {axis: null, domain: periods},
y: {
grid: true,
nice: true,
tickFormat: "+s"
},
fx: {tickFormat: p => formatProvince(p)},
color: {scheme: "spectral", domain: periods, legend: true},
facet: {data: provincePopulationDifferences, x: "province"},
marks: [
Plot.barY(provincePopulationDifferences, {
x: "period",
y: "difference",
title: "difference",
fill: "period",
sort: {fx: "y", reduce: "sum", reverse: true}
}),
Plot.ruleY([0])
]
})
)
Insert cell
addTooltips(
Plot.plot({
projection: {
type: "albers",
rotate: [-110, 0],
domain: provinces
},
marks: [
Plot.geo(provinces, {
fill: p => populationDifference2000And2020.get(p.properties.NL_NAME_1),
title: p => `${p.properties.NL_NAME_1} \n ${populationDifference2000And2020.get(p.properties.NL_NAME_1).toLocaleString()}`
})
],
margin: 20,
color: {
scheme: "RdGy", // Change color scheme
unknown: "#ddd", // Polygons with unknown values are gray
type: "linear", // Linear scale for color progression
legend: true, // Add the legend
tickFormat: "s",
label: "Population Difference between 2000 and 2000", // Update legend label
domain: [-2e7, 2e7]
},
})
)
Insert cell
Insert cell
addTooltips(
Plot.plot({
width: data.length * 40,
x: {axis: null, domain: periods},
y: {
grid: true,
nice: true,
tickFormat: "+%"
},
fx: {tickFormat: p => formatProvince(p)},
color: {scheme: "spectral", domain: periods, legend: true},
facet: {data: provincePopulationDifferences, x: "province"},
marks: [
Plot.barY(provincePopulationDifferences, {
x: "period",
y: "percentage",
title: "percentage",
fill: "period",
sort: {fx: "y", reduce: "sum", reverse: true}
}),
Plot.ruleY([0])
]
})
)
Insert cell
Insert cell
Insert cell
workbook = FileAttachment("人口普查.xlsx").xlsx()
Insert cell
censuses = workbook.sheetNames.map(sheetName => workbook.sheet(sheetName, { headers: true }))
Insert cell
census2020 = censuses[2]
Insert cell
totalPopulation2020 = census2020.reduce((acc, cur) => acc + cur. + cur., 0)
Insert cell
genders = census2020.columns.slice(2)
Insert cell
provinceGenders2020 = genders.flatMap(gender => census2020.map(d => ({province: d.地区, gender, population: d[gender]})))
Insert cell
years = censuses.flatMap((census, index) => index * 10 + 2000)
Insert cell
provinceYears = censuses.flatMap((census, index) => census.map(d => ({year: index * 10 + 2000, province: d.地区, population: totalPopulation(d)})))
Insert cell
periods = ["2000~2010", "2010~2020"]
Insert cell
provincePopulationDifferences = periods.flatMap((n, i1) => censuses[i1 + 1].map((d, i2) => {
let totalNow = totalPopulation(d)
let totalLastTime = totalPopulation(censuses[i1][i2])
let difference = totalNow - totalLastTime
return {period: periods[i1], province: d.地区, difference, percentage: difference / totalLastTime}
}))
Insert cell
populationDifference2000And2020 = new Map(census2020.map((d, index) => ([formatProvince(d.地区), totalPopulation(d) - totalPopulation(censuses[0][index])])))
Insert cell
formatProvince = p => p.substring(0, (p === '黑龙江省' || p === '内蒙古自治区') ? 3 : 2)

Insert cell
totalPopulation = (d) => d. + d.
Insert cell
china = FileAttachment("china-provinces-simplified.json").json()
Insert cell
provinces = topojson.feature(china, china.objects.CHN_adm1)
Insert cell
population2020 = new Map(census2020.map((d) => [formatProvince(d.地区), totalPopulation(d)]))
Insert cell
import {addTooltips} from "@mkfreeman/plot-tooltip"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more