Plot.plot({
title: "地震震级与深度关系图",
subtitle: "分析深度与震级的相关性",
width: 800,
height: 500,
grid: true,
x: {
label: "深度 (km)",
domain: [0, d3.max(earthquakes, d => d.depth) + 10],
},
y: {
label: "震级",
domain: [0, d3.max(earthquakes, d => d.mag) + 0.5],
},
color: {
scheme: "turbo",
legend: true,
label: "震级"
},
marks: [
Plot.dot(earthquakes, {
x: "depth",
y: "mag",
fill: "mag",
r: 4,
opacity: 0.7,
title: d => `${d.place}\n震级: ${d.mag}\n深度: ${d.depth.toFixed(1)}km`
}),
Plot.lineY(
d3.rollups(
earthquakes,
v => d3.mean(v, d => d.mag),
d => Math.floor(d.depth / 20) * 20
).map(([depth, mag]) => ({depth, mag}))
.sort((a, b) => a.depth - b.depth),
{
x: "depth",
y: "mag",
stroke: "red",
strokeWidth: 2,
curve: "catmull-rom"
}
)
]
})