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

Add examples to readme #72

Merged
merged 5 commits into from
Sep 15, 2024
Merged
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
257 changes: 254 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
- [Setting Cargo.toml](#setting-cargotoml)
- [Use of Jupyter via evcxr](#use-of-jupyter-via-evcxr)
- [Examples](#examples)
- [Barplot](#barplot)
- [Canvas](#canvas)
- [Contour](#contour)
- [Superquadric](#superquadric)
- [Curve](#curve)
- [Histogram](#histogram)
- [Image](#image)
- [Surface](#surface)
- [Text](#text)


## Introduction
Expand Down Expand Up @@ -93,8 +99,96 @@ plot.set_python_exe(python)

## Examples



### Barplot

[See the documentation](https://docs.rs/plotpy/latest/plotpy/struct.Barplot.html)

```rust
use plotpy::{Barplot, Plot, StrError};

fn main() -> Result<(), StrError> {
// data
let fruits = ["Apple", "Banana", "Orange"];
let prices = [10.0, 20.0, 30.0];
let errors = [3.0, 2.0, 1.0];

// barplot object and options
let mut bar = Barplot::new();
bar.set_x_errors(&errors)
.set_horizontal(true)
.set_with_text("edge")
.draw_with_str(&fruits, &prices);

// save figure
let mut plot = Plot::new();
plot.set_inv_y()
.add(&bar)
.set_title("Fruits")
.set_label_x("price")
.save("/tmp/plotpy/doc_tests/doc_barplot_3.svg")?;
Ok(())
}
```

![barplot.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_barplot_3.svg)



### Canvas

[See the documentation](https://docs.rs/plotpy/latest/plotpy/struct.Canvas.html)

```rust
use plotpy::{Canvas, Plot, PolyCode, StrError};

fn main() -> Result<(), StrError> {
// codes
let data = [
(3.0, 0.0, PolyCode::MoveTo),
(1.0, 1.5, PolyCode::Curve4),
(0.0, 4.0, PolyCode::Curve4),
(2.5, 3.9, PolyCode::Curve4),
(3.0, 3.8, PolyCode::LineTo),
(3.5, 3.9, PolyCode::LineTo),
(6.0, 4.0, PolyCode::Curve4),
(5.0, 1.5, PolyCode::Curve4),
(3.0, 0.0, PolyCode::Curve4),
];

// polycurve
let mut canvas = Canvas::new();
canvas.set_face_color("#f88989").set_edge_color("red");
canvas.polycurve_begin();
for (x, y, code) in data {
canvas.polycurve_add(x, y, code);
}
canvas.polycurve_end(true);

// add canvas to plot
let mut plot = Plot::new();
plot.add(&canvas);

// save figure
plot.set_range(1.0, 5.0, 0.0, 4.0)
.set_frame_borders(false)
.set_hide_axes(true)
.set_equal_axes(true)
.set_show_errors(true);
plot.save("/tmp/plotpy/doc_tests/doc_canvas_polycurve.svg")?;
Ok(())
}
```

![canvas.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_canvas_polycurve.svg)



### Contour

[See the documentation](https://docs.rs/plotpy/latest/plotpy/struct.Contour.html)

```rust
use plotpy::{generate3d, Contour, Plot, StrError};

Expand Down Expand Up @@ -124,9 +218,130 @@ fn main() -> Result<(), StrError> {
}
```

![readme_contour.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/readme_contour.svg)
![contour.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/readme_contour.svg)



### Superquadric
### Curve

[See the documentation](https://docs.rs/plotpy/latest/plotpy/struct.Curve.html)

```rust
use plotpy::{linspace, Curve, Plot, StrError};

fn main() -> Result<(), StrError> {
// generate (x,y) points
let x = linspace(-1.0, 1.0, 21);
let y: Vec<_> = x.iter().map(|v| 1.0 / (1.0 + f64::exp(-5.0 * *v))).collect();

// configure curve
let mut curve = Curve::new();
curve
.set_label("logistic function")
.set_line_alpha(0.8)
.set_line_color("#5f9cd8")
.set_line_style("-")
.set_line_width(5.0)
.set_marker_color("#eeea83")
.set_marker_every(5)
.set_marker_line_color("#da98d1")
.set_marker_line_width(2.5)
.set_marker_size(20.0)
.set_marker_style("*");

// draw curve
curve.draw(&x, &y);

// add curve to plot
let mut plot = Plot::new();
plot.add(&curve).set_num_ticks_y(11).grid_labels_legend("x", "y");

// save figure
plot.save("/tmp/plotpy/doc_tests/doc_curve.svg")?;
Ok(())
}
```

![curve.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_curve_vector.svg)


### Histogram

[See the documentation](https://docs.rs/plotpy/latest/plotpy/struct.Histogram.html)

```rust
use plotpy::{Histogram, Plot, StrError};

fn main() -> Result<(), StrError> {
// set values
let values = vec![
vec![1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6], // first series
vec![-1, -1, 0, 1, 2, 3], // second series
vec![5, 6, 7, 8], // third series
];

// set labels
let labels = ["first", "second", "third"];

// configure and draw histogram
let mut histogram = Histogram::new();
histogram.set_colors(&["#9de19a", "#e7eca3", "#98a7f2"])
.set_line_width(10.0)
.set_stacked(true)
.set_style("step");
histogram.draw(&values, &labels);

// add histogram to plot
let mut plot = Plot::new();
plot.add(&histogram)
.set_frame_border(true, false, true, false)
.grid_labels_legend("values", "count");

// save figure
plot.save("/tmp/plotpy/doc_tests/doc_histogram.svg")?;
Ok(())
}
```

![histogram](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_histogram.svg)


### Image

```rust
use plotpy::{Image, Plot, StrError};

fn main() -> Result<(), StrError> {
// set values
let data = [
[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3],
];

// image plot and options
let mut img = Image::new();
img.set_colormap_name("hsv").draw(&data);

// save figure
let mut plot = Plot::new();
plot.add(&img);
plot.save("/tmp/plotpy/doc_tests/doc_image_1.svg")?;
Ok(())
}
```

![image](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_image_1.svg)



### Surface

[See the documentation](https://docs.rs/plotpy/latest/plotpy/struct.Surface.html)

```rust
use plotpy::{Plot, StrError, Surface};
Expand Down Expand Up @@ -184,3 +399,39 @@ fn main() -> Result<(), StrError> {
```

![readme_superquadric.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/readme_superquadric.svg)


### Text

```rust
use plotpy::{Plot, Text, StrError};
use std::path::Path;

fn main() -> Result<(), StrError> {
// configure text
let mut text = Text::new();
text.set_color("purple")
.set_align_horizontal("center")
.set_align_vertical("center")
.set_fontsize(30.0)
.set_rotation(45.0)
.set_bbox(true)
.set_bbox_facecolor("pink")
.set_bbox_edgecolor("black")
.set_bbox_alpha(0.3)
.set_bbox_style("roundtooth,pad=0.3,tooth_size=0.2");

// draw text
text.draw_3d(0.5, 0.5, 0.5, "Hello World!");

// add text to plot
let mut plot = Plot::new();
plot.add(&text);

// save figure
plot.save("/tmp/plotpy/doc_tests/doc_text.svg")?;
Ok(())
}
```

![text](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/doc_text.svg)
37 changes: 37 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ Please check out [the documentation](https://docs.rs/plotpy) and the integration

Some output of integration tests are shown below.

## Barplot

[test_barplot.rs](https://github.com/cpmech/plotpy/tree/main/tests/test_barplot.rs)

![barplot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_barplot_1.svg)
![barplot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_barplot_2.svg)
![barplot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_barplot_3.svg)
![barplot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_barplot_4.svg)

## Canvas

[test_canvas.rs](https://github.com/cpmech/plotpy/tree/main/tests/test_canvas.rs)
Expand All @@ -15,12 +24,27 @@ Some output of integration tests are shown below.
![canvas](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_canvas_polycurve_quadratic.svg)
![canvas](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_canvas_polyline_3d_methods.svg)
![canvas](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_canvas_polyline_3d.svg)
![canvas](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_canvas_rectangle_and_text.svg)
![canvas](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_canvas_rectangle.svg)
![canvas](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_canvas.svg)

## Contour

[test_contour.rs](https://github.com/cpmech/plotpy/tree/main/tests/test_contour.rs)

![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_0.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_1.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_2.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_3.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_4.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_5.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_6.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_7.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_8.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_9.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_gnuplot2.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_Pastel1.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colormap_tab20c.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour_colors.svg)
![contour](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour.svg)

Expand All @@ -31,6 +55,7 @@ Some output of integration tests are shown below.
![curve](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_curve_3d.svg)
![curve](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_curve_points_3d_methods.svg)
![curve](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_curve_points_methods.svg)
![curve](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_curve_twinx.svg)
![curve](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_curve.svg)

## Histogram
Expand All @@ -40,6 +65,12 @@ Some output of integration tests are shown below.
![histogram](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_histogram_1.svg)
![histogram](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_histogram_2.svg)

## Image

[test_image.rs](https://github.com/cpmech/plotpy/tree/main/tests/test_image.rs)

![image](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_image_1.svg)

## Legend

[test_legend.rs](https://github.com/cpmech/plotpy/tree/main/tests/test_legend.rs)
Expand All @@ -51,9 +82,15 @@ Some output of integration tests are shown below.

[test_plot.rs](https://github.com/cpmech/plotpy/tree/main/tests/test_plot.rs)

![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_extra_funcionality.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_fontsize_and_color_2d.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_fontsize_and_color_3d.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_handles_quotes.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_log.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_multiple_of_pi.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_subplots.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_tick_labels.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_title_handles_tex.svg)
![plot](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot.svg)
![plot_3d](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_plot_3d.svg)

Expand Down
Loading
Loading