Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added looping over PlotStyle items as plot_feature_2 example #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Plot/plot_feature_2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "plot_feature_2"
version = "0.1.0"
authors = ["Axect <[email protected]>", "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"] }
33 changes: 33 additions & 0 deletions Plot/plot_feature_2/README.md
Original file line number Diff line number Diff line change
@@ -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)
Binary file added Plot/plot_feature_2/plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Plot/plot_feature_2/plot_ieee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Plot/plot_feature_2/plot_science.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions Plot/plot_feature_2/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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(())
}