Skip to main content

LinePlot

Builder for creating line plots that connect (x, y) data points with a continuous line. Supports multiple line styles, filled area regions, step interpolation, confidence bands, and error bars.

Constructor

LinePlot::new()
Self
Create a line plot with default settings.Defaults:
  • Color: "black"
  • Stroke width: 2.0 pixels
  • Line style: LineStyle::Solid
  • No fill
  • No step interpolation

Data Methods

with_data
Self
pub fn with_data<T, U, I>(self, points: I) -> Self
where
    I: IntoIterator<Item = (T, U)>,
    T: Into<f64>,
    U: Into<f64>
Set the (x, y) data points. Accepts any iterator of (T, U) pairs where T and U implement Into<f64>, so integer and float types all work without casting.Example:
let plot = LinePlot::new()
    .with_data(vec![(0_i32, 0_i32), (1, 2), (2, 1)]);
with_x_err
Self
pub fn with_x_err<T, I>(self, errors: I) -> Self
where
    I: IntoIterator<Item = T>,
    T: Into<f64> + Copy
Set symmetric X error bars. Each value is the half-width of the error bar. Must be called after with_data.
with_x_err_asymmetric
Self
pub fn with_x_err_asymmetric<T, U, I>(self, errors: I) -> Self
where
    I: IntoIterator<Item = (T, U)>,
    T: Into<f64>,
    U: Into<f64>
Set asymmetric X error bars. Each item is a (negative_arm, positive_arm) tuple. Must be called after with_data.
with_y_err
Self
pub fn with_y_err<T, I>(self, errors: I) -> Self
where
    I: IntoIterator<Item = T>,
    T: Into<f64> + Copy
Set symmetric Y error bars. Each value is the half-height of the error bar. Must be called after with_data.
with_y_err_asymmetric
Self
pub fn with_y_err_asymmetric<T, U, I>(self, errors: I) -> Self
where
    I: IntoIterator<Item = (T, U)>,
    T: Into<f64>,
    U: Into<f64>
Set asymmetric Y error bars. Each item is a (negative_arm, positive_arm) tuple. Must be called after with_data.

Styling Methods

with_color
Self
pub fn with_color<S: Into<String>>(self, color: S) -> Self
Set the line color. Accepts CSS color strings (e.g. "steelblue", "#4477aa").
with_stroke_width
Self
pub fn with_stroke_width(self, width: f64) -> Self
Set the stroke width in pixels (default 2.0).
with_line_style
Self
pub fn with_line_style(self, style: LineStyle) -> Self
Set the line style explicitly. See LineStyle for available options.
with_dashed
Self
pub fn with_dashed(self) -> Self
Set the line style to LineStyle::Dashed (dasharray 8 4).
with_dotted
Self
pub fn with_dotted(self) -> Self
Set the line style to LineStyle::Dotted (dasharray 2 4).
with_dashdot
Self
pub fn with_dashdot(self) -> Self
Set the line style to LineStyle::DashDot (dasharray 8 4 2 4).

Interpolation & Fill Methods

with_step
Self
pub fn with_step(self) -> Self
Use step interpolation between data points. Instead of a diagonal segment between (x₁, y₁) and (x₂, y₂), the renderer draws a horizontal segment at y₁ to x₂, then a vertical step to y₂. Useful for histograms and discrete-time series.
with_fill
Self
pub fn with_fill(self) -> Self
Fill the area between the line and the x-axis (area chart). The fill uses the line color at the opacity set by with_fill_opacity (default 0.3).
with_fill_opacity
Self
pub fn with_fill_opacity(self, opacity: f64) -> Self
Set the fill opacity for area charts (default 0.3). Has no effect unless with_fill is also called.

Band & Legend Methods

with_band
Self
pub fn with_band<T, U, I1, I2>(self, y_lower: I1, y_upper: I2) -> Self
where
    I1: IntoIterator<Item = T>,
    I2: IntoIterator<Item = U>,
    T: Into<f64>,
    U: Into<f64>
Attach a shaded confidence band aligned to the line’s x positions. y_lower and y_upper must have the same length as the data. The band color matches the line color at 30% opacity.Example:
let data: Vec<(f64, f64)> = (0..=5).map(|i| (i as f64, (i as f64).sin())).collect();
let lower: Vec<f64> = data.iter().map(|&(_, y)| y - 0.2).collect();
let upper: Vec<f64> = data.iter().map(|&(_, y)| y + 0.2).collect();

let plot = LinePlot::new()
    .with_data(data)
    .with_color("steelblue")
    .with_band(lower, upper);
with_legend
Self
pub fn with_legend<S: Into<String>>(self, label: S) -> Self
Attach a legend label to this series. A legend is rendered automatically when at least one series in the plot has a label.

Enums & Types

LineStyle

Stroke style for a line plot. Default is Solid.
  • Solid - Solid continuous line (default)
  • Dashed - Dashed line (dasharray 8 4)
  • Dotted - Dotted line (dasharray 2 4)
  • DashDot - Dash-dot line (dasharray 8 4 2 4)
  • Custom(String) - Arbitrary SVG stroke-dasharray value (e.g. "10 5 2 5")

Complete Example

use kuva::plot::LinePlot;
use kuva::backend::svg::SvgBackend;
use kuva::render::render::render_multiple;
use kuva::render::layout::Layout;
use kuva::render::plots::Plot;

let data: Vec<(f64, f64)> = (0..=100)
    .map(|i| { let x = i as f64 * 0.1; (x, x.sin()) })
    .collect();

let plot = LinePlot::new()
    .with_data(data)
    .with_color("steelblue")
    .with_stroke_width(2.0)
    .with_dashed();

let plots = vec![Plot::Line(plot)];
let layout = Layout::auto_from_plots(&plots)
    .with_title("Sine Wave")
    .with_x_label("X")
    .with_y_label("Y");

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

Build docs developers (and LLMs) love