diff --git a/Plot/plot_feature_2/Cargo.toml b/Plot/plot_feature_2/Cargo.toml new file mode 100644 index 0000000..94b50e2 --- /dev/null +++ b/Plot/plot_feature_2/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "plot_feature_2" +version = "0.1.0" +authors = ["Axect ", "raprism (https://github.com/raprism)"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +peroxide = { version = "0.37.9", features = ["plot"] } diff --git a/Plot/plot_feature_2/README.md b/Plot/plot_feature_2/README.md new file mode 100644 index 0000000..bdd41aa --- /dev/null +++ b/Plot/plot_feature_2/README.md @@ -0,0 +1,33 @@ +# Peroxide Plot Feature 2 + +## Prerequisites + +* Python - `matplotlib`, `scienceplots` + +## Description + +This code extends Plot Feature to generate all supported style outputs with a single call. + +## Process + +Just run! + +```sh +cargo run --release +``` + +## Result + +***Due to issues (probably with PlotStyle.style.use) with specific context settings the Nature style generation is currently disabled in `main.rs`.*** + +- Default style + ![plot](plot.png) + +- Science style (Default style for scienceplots) + ![plot](plot_science.png) + +- Nature style + ![plot](plot_nature.png) + +- IEEE style + ![plot](plot_ieee.png) diff --git a/Plot/plot_feature_2/plot.png b/Plot/plot_feature_2/plot.png new file mode 100644 index 0000000..7d0a4e0 Binary files /dev/null and b/Plot/plot_feature_2/plot.png differ diff --git a/Plot/plot_feature_2/plot_ieee.png b/Plot/plot_feature_2/plot_ieee.png new file mode 100644 index 0000000..bf297ec Binary files /dev/null and b/Plot/plot_feature_2/plot_ieee.png differ diff --git a/Plot/plot_feature_2/plot_science.png b/Plot/plot_feature_2/plot_science.png new file mode 100644 index 0000000..b80d709 Binary files /dev/null and b/Plot/plot_feature_2/plot_science.png differ diff --git a/Plot/plot_feature_2/src/main.rs b/Plot/plot_feature_2/src/main.rs new file mode 100644 index 0000000..60e0e5f --- /dev/null +++ b/Plot/plot_feature_2/src/main.rs @@ -0,0 +1,76 @@ +extern crate peroxide; +use peroxide::fuga::*; + +use std::collections::BTreeMap; +// ## alternative (unordered creation) +//use std::collections::HashMap; + +trait PlotStyleExt { + fn from_str(s: &str) -> PlotStyle; +} + +impl PlotStyleExt for PlotStyle { + fn from_str(s: &str) -> PlotStyle { + match s { + //"Default" => PlotStyle::Default, + "IEEE" => PlotStyle::IEEE, + "Nature" => PlotStyle::Nature, + "Science" => PlotStyle::Science, + _ => PlotStyle::Default + } + } +} + +fn main() -> Result<(), Box> { + let x = seq(0, 1, 0.01); + let x2 = x.fmap(|t| t.powi(2)); + let x3 = x.fmap(|t| t.powi(3)); + + let mut styles: BTreeMap<&str, &str> = BTreeMap::new(); + // ## alternatively create an unordered mapping + //let mut styles: HashMap<&str, &str> = HashMap::new(); + // + // ## supported scienceplot styles (hard coded enum ...) + // ## no direct setting of e.g. "retro" possible + styles.insert("ieee", "IEEE"); + // ## unordered HashTable works sometimes for all styles + // ## orderer BTreeMap fails with some latex error for Nature style + //styles.insert("nature", "Nature"); + styles.insert("science", "Science"); + // needs to be (alphabetically) first entry, + // otherwise Default has properties of previous style! + styles.insert("0_default", "Default"); + + for (key, style_name) in styles.iter() { + + let mut plt = Plot2D::new(); + + let mut filename: String = "plot".to_owned(); + if (*key) != "0_default" { + filename = format!("{}_{}.png", filename, key); + } else { + filename = format!("{}.png", filename); + } + print!("{} ...", filename); + + plt + .set_domain(x.clone()) + .insert_image(x.clone()) + .insert_image(x2.clone()) + .insert_image(x3.clone()) + //.set_style(PlotStyle::Default) + .set_style(PlotStyle::from_str(style_name)) + .set_dpi(300) + .set_xlabel("$x$") + .set_ylabel("$y$") + .set_legend(vec!["$y=x$", "$y=x^2$", "$y=x^3$"]) + .grid(Grid::On) + .tight_layout() + .set_path(filename.as_str()) + .savefig()?; + + println!(" saved!"); + } + + Ok(()) +}