Draw Capped Error Bars Anywhere on a Scatter Plot
▶ Allele-frequency trajectory (reversed axis + segment error bars)
Some of the most information-dense figures in science are also the simplest to describe: a dot marking a value, and a vertical whisker with flat caps showing the uncertainty around it. Allele-frequency panels, measurement-with-confidence plots, before-and-after comparisons — they all lean on the same visual primitive, a capped segment anchored to a specific point in the plot.
CanvasXpress now draws that primitive directly, with a new decoration type: decorations.segment. It renders a free two-point line between any (x, y) and (x2, y2) coordinates you choose, with optional end caps and an optional anchor dot — and it is aware of reversed axes, so it lands correctly even when your axis runs high-to-low.
Why a new decoration was needed
CanvasXpress already has a rich decoration system for annotating plots, and it is worth being precise about what existed before, because the gap was specific:
- Range decorations draw a band that spans the whole plot between two values on a single axis — a highlighted zone, not a point-anchored segment.
- Error decorations draw capped error bars, but they are sample-anchored: they belong to the bar and dot-plot world, keyed to a category, not to free scatter coordinates.
- Linear/fit decorations draw a single fitted line through your data, not independent per-point segments.
None of those draws an arbitrary two-point segment sitting at coordinates you specify. The (x, y, x2, y2) convention itself was already familiar from the fit decorations, so segment adopts it and composes the missing piece: each entry is its own independent line, optionally capped into an error bar, optionally crowned with a dot.
The shape of a segment
A segment entry is a small object. At minimum it has four coordinates:
decorations: {
segment: [
{ x: 1, y: 2, x2: 1, y2: 4, cap: true, point: true, color: "firebrick" }
]
}
That single entry draws a vertical whisker from (1, 2) to (1, 4), puts flat perpendicular caps at both ends, and places an anchor dot at the base — the complete "mean plus capped error bar" mark in one declaration.
The options you can set per entry:
x, y, x2, y2— the two endpoints, in data space. Required.color, width, lineType— the stroke styling.cap— turn on perpendicular end caps (this is what makes it read as an error bar), withcapSizecontrolling their length.pointandpointSize— draw an anchor dot at the start, or at both ends.labelandalign— an optional text label.tooltip— hover text; by default it reports the two endpoints.scope— confine the segment to a specific facet when your plot is laid out in panels.
Because segment takes an array, you can draw as many independent whiskers as your data needs, each with its own coordinates and styling — one per observation, if that is what the figure calls for.
Reversed axes handled correctly
A detail that sounds small but matters a great deal in practice: segment maps its coordinates through the engine's axis-aware scaling. If you have set an axis to run in reverse — common in genomics and in any plot where a conventional ordering is flipped — the segment and its caps still land on the correct data values. You do not have to pre-flip your numbers or reason about pixel space; you give data coordinates and the decoration places them honestly, whichever way the axis points.
This was in fact the motivating case. Recreating a reversed-axis allele-frequency trajectory, where ancestral-source points are drawn as capped error bars, is exactly the kind of figure that needs both the segment primitive and correct reversed-axis placement at once.
A real example in the gallery
The feature ships with a worked Scatter2D example: a reversed-axis trajectory whose points are rendered as segment capped error bars. It is there to show the full composition — dot, whisker, caps, reversed axis — in a single real figure you can inspect and adapt.
Why this belongs in a charting library
You could, in principle, draw error bars by hand with low-level canvas calls. But then they are not part of the chart: they do not move when you zoom, they do not participate in hover, they do not respect facets, and they certainly do not know about your reversed axis. By expressing the whisker as a decoration, it becomes a first-class citizen of the figure. It carries a tooltip, it confines itself to the right panel under a layout, it is reproducible as part of the chart's configuration, and it travels with the figure when you save or share it.
When to reach for segment
Use it whenever you need a mark that lives between two specific points rather than spanning the plot or hanging off a category:
- Per-point error bars or confidence intervals on a scatter plot.
- Connectors between paired observations — a before and an after, linked.
- Any annotation that is fundamentally "a line from here to there," with or without caps.
It is a compact addition that fills a genuine gap, and it does so in the idiom CanvasXpress users already know: a decoration array, data-space coordinates, and sensible defaults that produce a publication-ready mark with a single short declaration.