Skip to content

Commit

Permalink
Merge pull request #43 from cpmech/add-functionality-to-plot
Browse files Browse the repository at this point in the history
Add functionality to plot
  • Loading branch information
cpmech authored Jun 3, 2024
2 parents 4e3f7e4 + a17c787 commit 02f500e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,28 @@ impl Plot {
self.set_frame_border(show_all, show_all, show_all, show_all)
}

/// Draws an infinite horizontal line at y
pub fn set_horiz_line(&mut self, y: f64, 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({}{})\n", y, &opt));
self
}

/// Draws an infinite vertical line at x
pub fn set_vert_line(&mut self, x: f64, 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.axvline({}{})\n", x, &opt));
self
}

/// Draws infinite horizontal and vertical lines at (x, y)
pub fn set_cross(&mut self, x: f64, y: f64, 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({}{})\nplt.axvline({}{})\n", y, &opt, x, &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 +1281,17 @@ 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_horiz_line(-1.0, "blue", "-", 1.1)
.set_vert_line(-2.0, "green", ":", 1.2)
.set_cross(0.25, 0.75, "red", "--", 3.0);
let b: &str = "plt.axhline(-1,color='blue',linestyle='-',linewidth=1.1)\n\
plt.axvline(-2,color='green',linestyle=':',linewidth=1.2)\n\
plt.axhline(0.75,color='red',linestyle='--',linewidth=3)\n\
plt.axvline(0.25,color='red',linestyle='--',linewidth=3)\n";
assert_eq!(plot.buffer, b);
}
}
22 changes: 22 additions & 0 deletions tests/test_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,25 @@ 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_horiz_line(-0.5, "green", "-", 1.0)
.set_vert_line(-0.75, "gold", ":", 10.0)
.set_cross(0.25, 0.75, "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 > 490 && n < 530);
Ok(())
}

0 comments on commit 02f500e

Please sign in to comment.