Plotters
A pure-Rust drawing library for rendering data plots and charts to bitmap, SVG, and WebAssembly canvas backends.
Repository Health
Technical Analysis
Plotters is a Rust drawing library purpose-built for rendering figures, plots, and charts without relying on external C libraries like Cairo or GTK. It ships with a pluggable backend system: bitmap output via plotters-bitmap, vector SVG output via plotters-svg, and a WebAssembly canvas target for rendering directly to an HTML5 <canvas> in the browser, all sharing the same charting API.
The crate provides a ChartBuilder/ChartContext API for composing axes, meshes, legends, and series (line, area, point, histogram, candlestick, boxplot, error bars) on top of a DrawingArea abstraction that supports nested and split layouts. It is widely used for scientific visualization, financial charting, and generating static or animated plots from Rust programs, including interactive use inside Jupyter via the evcxr kernel.
What You Get
- A
ChartBuilder/ChartContextAPI for composing axes, mesh grids, legends, and labeled series on a canvas - Pluggable backends:
BitMapBackend(PNG/JPEG/BMP via theimagecrate),SVGBackendfor vector output, and native WASM canvas support - Built-in series and element types: line, area, point, histogram, candlestick, boxplot, error bars, and pie charts
- A
DrawingAreaabstraction supporting nested/split layouts, coordinate mapping (linear, logarithmic, date/time, 3D), and full-color-palette styling - Optional TrueType font rendering (
ttffeature) and colormap support for continuous-value visualizations - Jupyter integration via the
evcxrRust kernel for interactive, inline chart rendering in notebooks
Common Use Cases
- Generating static PNG or SVG charts from data-processing pipelines and CLI tools written in Rust
- Rendering financial candlestick and time-series charts for trading or analytics dashboards
- Producing interactive plots inside Jupyter notebooks via the evcxr kernel for data exploration
- Compiling charting code to WebAssembly to draw directly on an HTML5 canvas in web applications
- Visualizing scientific or numerical simulation output (e.g. Mandelbrot sets, 3D surfaces) without a C toolchain dependency
Under The Hood
Architecture
Plotters separates charting logic from rendering through a DrawingBackend trait (defined in the sibling plotters-backend crate) that exposes low-level primitives like draw_pixel, draw_line, and estimate_text_size; a default CPU rasterizer fills in shape drawing for any backend that only implements pixel output, while richer backends can override methods directly. On top of that sits DrawingArea, a recursively splittable canvas abstraction, and ChartBuilder/ChartContext, which compose axes, mesh grids, legends, and series onto a DrawingArea using ranged coordinate types (linear, logarithmic, date/time, 3D) defined under coord/. This layering means adding a new output target only requires implementing the backend trait, not touching chart or series logic, and the project has in fact split each backend (plotters-bitmap, plotters-svg) into its own crate around this seam.
Tech Stack
The core plotters crate targets Rust edition 2018 with an MSRV of 1.56, structured as a Cargo workspace with plotters-backend, plotters-bitmap, and plotters-svg as path dependencies alongside the main crate. Optional dependencies are feature-gated: chrono for date/time coordinates, serde for serialization, image for bitmap encoding, and a TrueType stack (font-kit, ttf-parser, ab_glyph) for text rendering, with a separate WASM target pulling in wasm-bindgen and web-sys for direct HTML5 canvas access. This feature-gating lets consumers strip unused backends and shrink the dependency tree for size-sensitive builds like WASM.
Code Quality
Tests are embedded inline as #[cfg(test)] mod tests blocks across roughly two-thirds of the source files, covering series, coordinate mapping, and element rendering logic, and a GitHub Actions matrix runs cargo test across Ubuntu, Windows, and macOS plus a dedicated all-features test job, a clippy lint workflow, and a WASM build/test workflow. The crate enables #![warn(missing_docs)] at the crate root, enforcing doc comments on public items, and error handling is typed through a DrawingErrorKind<E> enum wrapping backend-specific and font-loading errors rather than swallowing failures. No test files were found isolated in a top-level tests/ directory; all coverage lives alongside the implementation.
API Design
The public API centers on a fluent ChartBuilder::on(&root).caption(...).build_cartesian_2d(...) chain that reads close to plain English, and the crate re-exports its most common types through a prelude module so a typical program needs only use plotters::prelude::*; to get started. Getting a first chart on screen takes roughly a dozen lines including backend setup, mesh configuration, and one series draw call, and the crate’s own module-level documentation walks through this quick-start path directly in lib.rs, which also renders as the crate’s docs.rs landing page.