PlotIron - Rust Plotting Library
A Rust plotting library that integrates DOT graph rendering with traditional matplotlib plotting capabilities
Features
- 🎨 Multiple Chart Types: Line plots, scatter plots, bar charts, and more
- 🎯 Easy to Use: matplotlib-like API design
- 📊 SVG Output: Generate high-quality vector graphics
- 🎪 Custom Styling: Support for colors, markers, line styles, and more
- 📈 Multiple Subplots: Create multiple subplots in a single figure
- 🔧 Flexible Configuration: Configurable axes, grids, legends, and more
Quick Start
Installation
Add to your Cargo.toml:
[]
= { = "." }
Basic Usage
use *;
Examples
Line Plot
use Array;
// Create x values from -10 to 10
let x: = .map.collect;
let x = from;
// Calculate sin and cos values
let y = x.sin;
let z = x.cos;
let mut fig = figure;
fig.add_subplot
.plot // Plot sin(x)
.plot // Plot cos(x)
.set_title;
write.unwrap;
Scatter Plot
let x = vec!;
let y = vec!;
let mut fig = figure;
fig.add_subplot
.scatter
.set_title;
write.unwrap;
Bar Chart
let categories = vec!;
let values = vec!;
let mut fig = figure;
fig.add_subplot
.bar
.set_title;
write.unwrap;
Histogram
// Generate sample data
let data: = .map.collect;
let mut fig = figure;
fig.add_subplot
.histogram // 20 bins
.set_title
.set_xlabel
.set_ylabel;
write.unwrap;
Pie Chart
use *;
let values = vec!;
let labels = vec!;
let mut fig = figure;
fig.add_subplot
.pie
.set_title;
write.expect;
Box Plot
use *;
let data = vec!;
let mut fig = figure;
fig.add_subplot
.boxplot
.set_title
.set_ylabel;
write.expect;
Violin Plot
use *;
let data = vec!;
let mut fig = figure;
fig.add_subplot
.violin
.set_title
.set_ylabel
.grid;
write.expect;
Contour Plot
// Create sample data for contour plot
let x: = .map.collect;
let y: = .map.collect;
// Create a 2D function: z = sin(x) * cos(y)
let mut z: = Vecnew;
for &yi in &y
let mut fig = figure;
fig.add_subplot
.contour
.set_title
.set_xlabel
.set_ylabel;
write.expect;
Multiple Line Plot
let x: = .map.collect;
let y1: = x.iter.map.collect;
let y2: = x.iter.map.collect;
let mut fig = figure;
fig.add_subplot
.plot
.plot
.set_title
.legend;
write.unwrap;
Multiple Subplots
let x: = .map.collect;
let y1: = x.iter.map.collect;
let y2: = x.iter.map.collect;
let scatter_x: = .map.collect;
let scatter_y: = scatter_x.iter.map.collect;
let mut fig = figure_with_size;
// First subplot: sine wave
fig.add_subplot
.plot
.set_title
.set_xlabel
.set_ylabel;
// Second subplot: cosine wave
fig.add_subplot
.plot
.set_title
.set_xlabel
.set_ylabel;
// Third subplot: scatter plot
fig.add_subplot
.scatter
.set_title
.set_xlabel
.set_ylabel;
write.unwrap;
DOT Graph Example
let dot_content = r#"
digraph G {
rankdir=TB;
A [label="Start"];
B [label="Process"];
C [label="Decision"];
D [label="End"];
A -> B;
B -> C;
C -> D [label="Yes"];
C -> B [label="No"];
}
"#;
let mut fig = figure;
fig.add_dot_subplot.unwrap
.set_title;
write.unwrap;
Mixed Subplots: Scatter Plot and DOT Graph
// Create scatter plot data
let scatter_x: = .map.collect;
let scatter_y: = scatter_x.iter.map.collect;
// DOT graph content
let dot_content = r#"
digraph Network {
rankdir=LR;
node [shape=circle];
A -> B -> C;
A -> D -> C;
B -> E;
D -> E;
E -> F;
C -> F;
}
"#;
let mut fig = figure_with_size;
// First subplot: scatter plot
fig.add_subplot
.scatter
.set_title
.set_xlabel
.set_ylabel
.grid;
// Second subplot: DOT graph
fig.add_dot_subplot.unwrap
.set_title
.show_x_axis
.show_y_axis;
write.unwrap;
API Reference
Figure
figure()- Create a figure with default sizefigure_with_size(width, height)- Create a figure with specified sizeadd_subplot()- Add a subplotadd_dot_subplot(dot_content)- Add a subplot with DOT graphadd_dot_subplot_with_layout(dot_content, layout)- Add DOT subplot with specific layoutto_svg()- Generate SVG stringshow()- Display figure (print SVG to console)
Axes
plot(x, y)- Add line plotscatter(x, y)- Add scatter plotbar(x, y)- Add bar chartset_title(title)- Set titleset_xlabel(label)- Set X-axis labelset_ylabel(label)- Set Y-axis labelset_xlim(min, max)- Set X-axis rangeset_ylim(min, max)- Set Y-axis rangegrid(enable)- Enable/disable gridlegend(enable)- Enable/disable legendshow_x_axis(enable)- Show/hide X-axisshow_y_axis(enable)- Show/hide Y-axis
DOT Layout Algorithms
LayoutAlgorithm::Circular- Arrange nodes in a circleLayoutAlgorithm::Hierarchical- Hierarchical top-down layoutLayoutAlgorithm::ForceDirected- Force-directed layoutLayoutAlgorithm::Grid- Grid-based layout
Colors
Supported predefined colors:
Color::RED,Color::BLUE,Color::GREEN, etc.- Hex colors:
Color::from_hex("#FF0000") - String colors:
Color::from("red")
Marker Styles
Marker::Circle- CircleMarker::Square- SquareMarker::TriangleUp- Upward triangleMarker::Diamond- DiamondMarker::Plus- Plus signMarker::Cross- CrossMarker::Star- Star
Saving Plots
To save plots as SVG files, use the to_svg() method combined with std::fs::write():
let mut fig = figure;
fig.add_subplot
.plot
.set_title;
// Save to file
write.unwrap;
Running Examples
This will generate several example SVG files in the output/ directory:
sine_plot.svg- Sine function plotmulti_line_plot.svg- Multi-line plotscatter_plot.svg- Scatter plotbar_plot.svg- Bar chart