singleManhattanPath = [
{x: 0, y: 0}, // Start at origin
{x: sv1, y: 0}, // Move horizontally first
{x: sv1, y: sv2} // Then move vertically to vector endpoint
]
// Norm visualization plot (single vector)
Plot.plot({
style: "overflow: visible; display: block; margin: 0 auto;",
width: 700,
height: 600,
grid: true,
x: {label: "x_1", domain: [-10, 10]},
y: {label: "x_2", domain: [-10, 10]},
marks: [
// Grid axes
Plot.ruleY([0], {stroke: "#ddd"}),
Plot.ruleX([0], {stroke: "#ddd"}),
// Vector v (blue arrow)
Plot.arrow([{x1: 0, y1: 0, x2: sv1, y2: sv2}], {
x1: "x1", y1: "y1", x2: "x2", y2: "y2",
stroke: "steelblue", strokeWidth: 4, fill: "steelblue"
}),
// L2-norm: Direct line from origin to vector endpoint (green)
Plot.line([{x: 0, y: 0}, {x: sv1, y: sv2}], {
x: "x", y: "y",
stroke: "green", strokeWidth: 3, strokeOpacity: 0.6, strokeDasharray: "5,5"
}),
// L1-norm: Manhattan path from origin to vector endpoint (orange)
Plot.line(singleManhattanPath, {
x: "x", y: "y",
stroke: "orange", strokeWidth: 4, strokeOpacity: 0.8
}),
// Origin point
Plot.dot([{x: 0, y: 0}], {
x: "x", y: "y",
r: 4,
fill: "black",
stroke: "white", strokeWidth: 2
}),
// Vector endpoint
Plot.dot([{x: sv1, y: sv2}], {
x: "x", y: "y",
r: 6,
fill: "steelblue",
stroke: "white", strokeWidth: 2
}),
// Corner point for Manhattan distance
Plot.dot([{x: sv1, y: 0}], {
x: "x", y: "y", r: 3, fill: "orange", stroke: "white"
}),
// Vector label
Plot.text([
{x: sv1/2 + 0.3, y: sv2/2 + 0.3, text: "v"}
], {
x: "x", y: "y", text: "text",
fontSize: 16, fontWeight: "bold", fill: "steelblue"
}),
// Norm labels
Plot.text([
{x: sv1/2 - 0.5, y: sv2/2 - 0.5, text: `L₂ = ${singleNorms.l2_norm.toFixed(2)}`},
{x: sv1/2, y: -0.8, text: `L₁ = ${singleNorms.l1_norm.toFixed(2)}`}
], {
x: "x", y: "y", text: "text",
fontSize: 12, fontWeight: "bold",
fill: d => d.text.includes("L₂") ? "green" : "orange"
}),
// Component labels
Plot.text([
{x: sv1/2, y: -0.3, text: `|${sv1}|`},
{x: sv1 + 0.3, y: sv2/2, text: `|${sv2}|`}
], {
x: "x", y: "y", text: "text",
fontSize: 11, fill: "orange", fontStyle: "italic"
})
]
})