Skip to content

Commit

Permalink
Impl set_cross
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmech committed May 18, 2024
1 parent 4e3f7e4 commit d6da126
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,14 @@ impl Plot {
self.set_frame_border(show_all, show_all, show_all, show_all)
}

/// Draws infinite lines at x=0 and y=0
pub fn set_cross(&mut self, color: &str, line_style: &str, line_width: f64) -> &mut Self {
let opt = format!(",color='{}',linestyle='{}',linewidth={}", color, line_style, line_width);
self.buffer
.push_str(&format!("plt.axhline(0{})\nplt.axvline(0{})\n", &opt, &opt));
self
}

/// Writes extra python commands
pub fn extra(&mut self, commands: &str) -> &mut Self {
self.buffer.write_str(commands).unwrap();
Expand Down Expand Up @@ -1259,4 +1267,13 @@ mod tests {
plt.gca().set_zlabel(r'Z')\n";
assert_eq!(plot.buffer, b);
}

#[test]
fn extra_functionality_works() {
let mut plot = Plot::new();
plot.set_cross("red", "--", 3.0);
let b: &str = "plt.axhline(0,color='red',linestyle='--',linewidth=3)\n\
plt.axvline(0,color='red',linestyle='--',linewidth=3)\n";
assert_eq!(plot.buffer, b);
}
}
19 changes: 19 additions & 0 deletions tests/test_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,22 @@ fn test_plot_multiple_of_pi() -> Result<(), StrError> {
assert!(lines_iter.count() > 1060);
Ok(())
}

#[test]
fn test_plot_extra_functionality() -> Result<(), StrError> {
// plot
let mut plot = Plot::new();
plot.set_cross("red", "--", 3.0).set_range(-1.0, 1.0, -1.0, 1.0);

// save figure
let path = Path::new(OUT_DIR).join("integ_plot_extra_functionality.svg");
plot.set_show_errors(true).save(&path)?;

// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 480 && n < 520);
Ok(())
}

0 comments on commit d6da126

Please sign in to comment.