Skip to content
Commits on Source (3)
......@@ -3,3 +3,4 @@
**/*.rs.bk
.idea
*.iml
Cargo.lock
# changelog
* 0.1.7
* Add line segments
* 0.1.6
* Add vertical and horizontal lines
......
[package]
name = "matplotrust"
version = "0.1.6"
version = "0.1.7"
authors = ["Rui Vieira <ruidevieira@googlemail.com>"]
exclude = ["examples/figures/**/*", ".idea/**/*", "binder/**/*"]
description = "A simple wrap around Python's matplotlib."
......
......@@ -22,6 +22,7 @@ Python (3.x) and `matplotlib` must also be installed.
* Line plots
* Histograms
* Vertical, horizontal lines
* Line segments
* Plots as Base64
## examples
......
......@@ -102,4 +102,22 @@ figure.add_plot(mean_line);
figure.save("./examples/figures/horizontal_line.png", None);
```
![plot](figures/horizontal_line.png)
\ No newline at end of file
![plot](figures/horizontal_line.png)
## line segments
```rust
let x = vec![1, 2, 3, 4];
let y = vec![0.1, 0.2, 0.5, 0.3];
let mut options = ScatterPlotOptions::new();
options.marker = Some(Markers::Diamond);
let lp = scatter_plot::<i32, f64>(x, y, Some(options));
let mut figure = Figure::new();
figure.add_plot(lp);
let mut line_opt = LinePlotOptions::new();
line_opt.colour = Some("red".to_string());
line_opt.lineStyle = Some(LineStyle::Dash);
figure.add_plot(line::<i32, f64>((1, 0.1), (4, 0.3), Some(line_opt)));
```
![plot](figures/line_segment.png)
\ No newline at end of file
......@@ -108,7 +108,7 @@ impl Figure {
pub fn new() -> Figure {
return Figure {
script: "import matplotlib\nmatplotlib.use('agg')\nimport matplotlib.pyplot as plt\n\n".to_string()
script: "import matplotlib\nmatplotlib.use('agg')\nimport matplotlib.pyplot as plt\nfrom matplotlib.lines import Line2D\n\n".to_string()
}
}
......@@ -276,6 +276,17 @@ pub fn vertical_line<U>(x: U, options: Option<LinePlotOptions>) -> String where
}
}
pub fn line<U, T>(start : (U, T), end : (U, T), options : Option<LinePlotOptions>) -> String where U : ToString, T : ToString {
match options {
Some(opt) => {
return format!("ax=plt.gca()\nax.add_line(Line2D([{},{}],[{},{}],{}))\n", start.0.to_string(), end.0.to_string(), start.1.to_string(), end.1.to_string(), opt);
},
None => {
return format!("ax=plt.gca()\nax.add_line(Line2D([{},{}],[{},{}]))\n", start.0.to_string(), end.0.to_string(), start.1.to_string(), end.1.to_string());
}
}
}
#[cfg(test)]
mod lineplot {
use super::*;
......@@ -434,9 +445,22 @@ mod lineplot {
print!("{:?}", figure.save("./examples/figures/horizontal_line.png", None));
}
#[test]
fn line_segment() {
let x = vec![1, 2, 3, 4];
let y = vec![0.1, 0.2, 0.5, 0.3];
let mut options = ScatterPlotOptions::new();
options.marker = Some(Markers::Diamond);
let lp = scatter_plot::<i32, f64>(x, y, Some(options));
let mut figure = Figure::new();
figure.add_plot(lp);
let mut line_opt = LinePlotOptions::new();
line_opt.colour = Some("red".to_string());
line_opt.lineStyle = Some(LineStyle::Dash);
figure.add_plot(line::<i32, f64>((1, 0.1), (4, 0.3), Some(line_opt)));
figure.save("./examples/figures/line_segment.png", None);
print!("{:?}", figure.script);
}
}
fn main() {
println!("Hello, world!");
}
\ No newline at end of file