Skip to main content
A shaded area between two y-curves over a shared x-axis — typically used to display confidence intervals, prediction bands, or IQR envelopes.

Usage Modes

Standalone — create with BandPlot::new and add as Plot::Band. Pair with a Plot::Line or Plot::Scatter in the same plots vector to draw the band behind the data series. Attached — use LinePlot::with_band or ScatterPlot::with_band as a one-call shorthand. The band inherits the series color automatically and is rendered behind the line or points.

Constructor

new()

Create a band from parallel x, lower-bound, and upper-bound iterables.
x
I1: IntoIterator<Item = T>
X coordinates shared by both boundary curves
y_lower
I2: IntoIterator<Item = U>
Lower boundary y values. Must have the same length as x
y_upper
I3: IntoIterator<Item = V>
Upper boundary y values. Must have the same length as x
Returns: BandPlot All three iterables must have the same length. Accepts any type implementing Into<f64>. Default fill: "steelblue" at opacity 0.2.
use kuva::plot::BandPlot;

let x = vec![0.0_f64, 1.0, 2.0, 3.0];
let lower = vec![-0.3_f64, 0.7, 1.7, 2.7];
let upper = vec![ 0.3_f64, 1.3, 2.3, 3.3];

let band = BandPlot::new(x, lower, upper);

Styling

with_color()

Set the fill color.
color
S: Into<String>
CSS color string (default: "steelblue")
Returns: Self Accepts any CSS color string. When using the standalone mode, set this to match the paired line or scatter color for a cohesive look.
let band = BandPlot::new(x, lower, upper)
    .with_color("firebrick");

with_opacity()

Set the fill opacity.
opacity
f64
Fill opacity in [0.0, 1.0] (default: 0.2)
Returns: Self Lower values make the band more transparent; 1.0 gives a fully opaque filled area.
let band = BandPlot::new(x, lower, upper)
    .with_opacity(0.3);

Legend

with_legend()

Enable a legend entry with the given label.
label
S: Into<String>
Legend label text
Returns: Self The legend swatch is a filled rectangle in the band color.
let band = BandPlot::new(x, lower, upper)
    .with_legend("95% CI");

Complete Example

use kuva::plot::{BandPlot, LinePlot};
use kuva::backend::svg::SvgBackend;
use kuva::render::render::render_multiple;
use kuva::render::layout::Layout;
use kuva::render::plots::Plot;

let x: Vec<f64> = (0..50).map(|i| i as f64 * 0.2).collect();
let y: Vec<f64> = x.iter().map(|&v| v.sin()).collect();
let lower: Vec<f64> = y.iter().map(|&v| v - 0.3).collect();
let upper: Vec<f64> = y.iter().map(|&v| v + 0.3).collect();

let band = BandPlot::new(x.clone(), lower, upper)
    .with_color("steelblue")
    .with_opacity(0.25);

let line = LinePlot::new()
    .with_data(x.iter().copied().zip(y.iter().copied()))
    .with_color("steelblue");

let plots = vec![Plot::Band(band), Plot::Line(line)];
let layout = Layout::auto_from_plots(&plots)
    .with_title("Confidence Band")
    .with_x_label("x")
    .with_y_label("y");

let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
std::fs::write("band.svg", svg).unwrap();

Public Fields

x
Vec<f64>
X coordinates shared by both boundary curves
y_lower
Vec<f64>
Lower boundary y values. Must have the same length as x
y_upper
Vec<f64>
Upper boundary y values. Must have the same length as x
color
String
Fill color as a CSS color string. Default: "steelblue"
opacity
f64
Fill opacity in [0.0, 1.0]. Default: 0.2
legend_label
Option<String>
When Some, a legend entry is shown with a filled rectangle swatch

Build docs developers (and LLMs) love