PlotIron - Rust Plotting Library
A Rust plotting library inspired by matplotlib for creating 2D charts and visualizations.
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
let x = vec!;
let y = vec!;
let mut fig = figure;
fig.add_subplot
.plot
.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;
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
Architecture
src/
├── lib.rs # Main library entry point
├── main.rs # Example program
├── figure.rs # Figure management
├── axes.rs # Axes functionality
├── plot.rs # Plot types
├── colors.rs # Color definitions
├── markers.rs # Marker styles
└── utils.rs # Utility functions
Dependencies
svg- SVG generationnum-traits- Numeric traitsrand- Random number generation (examples only)
License
MIT License
Contributing
Issues and Pull Requests are welcome!
TODO
- More chart types (histograms, pie charts, etc.)
- Animation support
- Interactive charts
- 3D chart support
- Better error handling
- Performance optimization