This guide walks you through creating a scatter plot using both the Rust library API and the CLI. By the end, you’ll understand the basic workflow for generating publication-ready visualizations.
use kuva::prelude::*;fn main() { // Define your data as (x, y) tuples let data = vec![ (1.0_f64, 2.0), (3.0, 5.0), (5.0, 4.0), (7.0, 8.0), (9.0, 7.0), ]; // Build the plot using the builder pattern let plot = ScatterPlot::new() .with_data(data) .with_color("steelblue") .with_size(5.0) .with_legend("Sample Data"); // Convert to Plot enum and collect into Vec let plots: Vec<Plot> = vec![plot.into()]; // Create layout with auto-computed axis ranges let layout = Layout::auto_from_plots(&plots) .with_title("My First Plot") .with_x_label("X Axis") .with_y_label("Y Axis"); // Render to SVG let svg = render_to_svg(plots, layout); // Write to file std::fs::write("my_plot.svg", svg).unwrap(); println!("Plot saved to my_plot.svg");}
3
Run and view the output
cargo run
This creates my_plot.svg in your project directory. Open it in a web browser or any SVG viewer.
What just happened? You created a ScatterPlot with builder methods, converted it to the Plot enum, configured a Layout, and rendered to SVG in one line with render_to_svg().
Each plot type has a new() constructor and builder methods:
let plot = ScatterPlot::new() .with_data(data) // Required: your data points .with_color("steelblue") // Optional: point color .with_size(5.0) // Optional: point size .with_legend("Label"); // Optional: legend entry
Builder methods are chainable and return Self, allowing fluent configuration.
The CLI provides access to all plot types without writing code.
1
Prepare your data
Create a TSV or CSV file. For example, data.tsv:
x y1.0 2.03.0 5.05.0 4.07.0 8.09.0 7.0
Kuva auto-detects TSV vs CSV based on file extension and content. Use .tsv for tab-separated, .csv for comma-separated.
2
Generate a plot
kuva scatter data.tsv --x x --y y -o my_plot.svg
This creates a scatter plot by:
Reading data.tsv
Using column x for X-axis data
Using column y for Y-axis data
Writing output to my_plot.svg
3
Customize the plot
Add titles, labels, and styling:
kuva scatter data.tsv \ --x x --y y \ --title "My First Plot" \ --x-label "X Axis" \ --y-label "Y Axis" \ --color steelblue \ --size 5 \ -o my_plot.svg
kuva scatter data.tsv --x x --y y -o plot.svg # SVGkuva scatter data.tsv --x x --y y -o plot.png # PNG (requires --features png)kuva scatter data.tsv --x x --y y -o plot.pdf # PDF (requires --features pdf)
Omit -o to write SVG to stdout (useful for piping).