Skip to content

Commit 086d012

Browse files
Traces for Mesh3d, Image, ScatterMapbox (#88)
* Add basic Mesh3D trace functionality * Add basic Image trace functionality * Add basic ScatterMapbox functionality * Fix compilation errors due to merge * Copy some setters * Fill in some more Mesh3D setters * Complete the Mesh3D setters * Complete the Image setters * Sketch idea of ImageData trait * Complete the ScatterMapbox setters * Add tests for Mesh3D * Fix cargo warnings * Make greater use of IntoIterator trait * Add tests for Image * Add tests for ScatterMapbox * Fix compilation errors * Insert _ in setter names * Run rustfmt and add line breaks to documentation * Add jupyter lab examples for Mesh3D, Image, ScatterMapbox * Update CHANGELOG * Remove setter assertions * refactoring * fix tests * formatting * add ImageData trait * add Image trace examples * add mesh3d example * rustfmt * add map example * tweak zoom data type * rustfmt * clippy * update workflows Co-authored-by: Michael Freeborn <michaelfreeborn1@gmail.com>
1 parent a3770c8 commit 086d012

File tree

34 files changed

+2672
-702
lines changed

34 files changed

+2672
-702
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ jobs:
6060
basic_charts,
6161
custom_controls,
6262
financial_charts,
63+
images,
6364
kaleido,
65+
maps,
6466
ndarray,
6567
scientific_charts,
6668
shapes,

.github/workflows/release_ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ jobs:
6060
basic_charts,
6161
custom_controls,
6262
financial_charts,
63+
images,
6364
kaleido,
65+
maps,
6466
ndarray,
6567
scientific_charts,
6668
shapes,

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [0.8.2] - 2022-11-xx
88
### Added
99
- [[#110](https://github.com/igiagkiozis/plotly/pull/110)] `LegendGroupTitle` to existing traces.
10+
- [[#88](https://github.com/igiagkiozis/plotly/pull/88)] `Mesh3D`, `Image`, `ScatterMapbox` traces.
1011

1112
### Changed
1213
- [[#113](https://github.com/igiagkiozis/plotly/pull/113)] Refactored the structure of the examples to make them more accessible, whilst adding more examples e.g. for `wasm`.

examples/3d_charts/src/main.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ndarray::Array;
44
use plotly::{
55
common::{ColorScale, ColorScalePalette, Marker, MarkerSymbol, Mode, Title},
66
layout::{Axis, Layout},
7-
Plot, Scatter3D, Surface,
7+
Mesh3D, Plot, Scatter3D, Surface,
88
};
99

1010
// 3D Scatter Plots
@@ -101,12 +101,35 @@ fn surface_plot() {
101101
plot.show();
102102
}
103103

104+
fn mesh_3d_plot() {
105+
let trace = Mesh3D::new(
106+
vec![0, 1, 2, 0],
107+
vec![0, 0, 1, 2],
108+
vec![0, 2, 0, 1],
109+
vec![0, 0, 0, 1],
110+
vec![1, 2, 3, 2],
111+
vec![2, 3, 1, 3],
112+
)
113+
.intensity(vec![0.0, 0.33, 0.66, 1.0])
114+
.color_scale(ColorScale::Palette(ColorScalePalette::Rainbow));
115+
116+
let mut plot = Plot::new();
117+
plot.add_trace(trace);
118+
119+
plot.show();
120+
}
121+
104122
fn main() {
105123
// Uncomment any of these lines to display the example.
106124

107125
// Scatter3D Plots
108-
simple_scatter3d_plot();
109-
simple_line3d_plot();
110-
customized_scatter3d_plot();
111-
surface_plot();
126+
// simple_scatter3d_plot();
127+
// simple_line3d_plot();
128+
// customized_scatter3d_plot();
129+
130+
// Surface Plots
131+
// surface_plot();
132+
133+
// Mesh Plots
134+
// mesh_3d_plot();
112135
}

examples/custom_controls/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use plotly::{
1010
Bar, HeatMap, Layout, Plot,
1111
};
1212

13-
/// Display a bar chart with an associated dropdown selector to show different data.
13+
/// Display a bar chart with an associated dropdown selector to show different
14+
/// data.
1415
fn bar_plot_with_dropdown_for_different_data() {
1516
type BarType = Bar<&'static str, i32>;
1617
let mut plot = Plot::new();
@@ -40,7 +41,8 @@ fn bar_plot_with_dropdown_for_different_data() {
4041
plot.show();
4142
}
4243

43-
/// Display a heat map, with buttons to allow for toggling of different colorscales.
44+
/// Display a heat map, with buttons to allow for toggling of different
45+
/// colorscales.
4446
fn heat_map_with_modifiable_colorscale() {
4547
type HeatMapType = HeatMap<f64, f64, Vec<f64>>;
4648
let gauss = |v: i32| (-v as f64 * v as f64 / 200.0).exp();
@@ -72,7 +74,8 @@ fn heat_map_with_modifiable_colorscale() {
7274
plot.show();
7375
}
7476

75-
/// Display a bar chart, with buttons to toggle between stacked or grouped display maodes.
77+
/// Display a bar chart, with buttons to toggle between stacked or grouped
78+
/// display maodes.
7679
fn bar_chart_with_modifiable_bar_mode() {
7780
type BarType = Bar<&'static str, i32>;
7881
let mut plot = Plot::new();

examples/images/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "images"
3+
version = "0.1.0"
4+
authors = ["Michael Freeborn <michaelfreeborn1@gmail.com>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
image = "0.24.4"
9+
ndarray = "0.15.6"
10+
plotly = { path = "../../plotly", features = ["plotly_image", "plotly_ndarray"] }

examples/images/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Images
2+
3+
## How to Run
4+
5+
1. Configure which example(s) you want to run by commenting/uncommenting lines in the `main` function, located in `src/main.rs`.
6+
2. Run `cargo run`.

examples/images/assets/mario.png

119 KB
Loading

examples/images/src/main.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#![allow(dead_code)]
2+
3+
use ndarray::arr2;
4+
use plotly::{color::Rgb, image::ColorModel, Image, Plot};
5+
6+
fn basic_image() {
7+
let w = Rgb::new(255, 255, 255);
8+
let b = Rgb::new(0, 0, 0);
9+
let r = Rgb::new(240, 8, 5);
10+
let db = Rgb::new(145, 67, 7);
11+
let lb = Rgb::new(251, 200, 129);
12+
let s = Rgb::new(153, 75, 10);
13+
let bl = Rgb::new(3, 111, 191);
14+
let y = Rgb::new(251, 250, 15);
15+
16+
let pixels = vec![
17+
vec![w, w, w, w, r, r, r, r, r, w, w, w, w, w, w],
18+
vec![w, w, w, r, r, r, r, r, r, r, r, r, w, w, w],
19+
vec![w, w, w, db, db, db, lb, lb, b, lb, w, w, w, w, w],
20+
vec![w, w, db, lb, db, lb, lb, lb, b, lb, lb, lb, w, w, w],
21+
vec![w, w, db, lb, db, db, lb, lb, lb, db, lb, lb, lb, w, w],
22+
vec![w, w, db, db, lb, lb, lb, lb, db, db, db, db, w, w, w],
23+
vec![w, w, w, w, lb, lb, lb, lb, lb, lb, lb, w, w, w, w],
24+
vec![w, w, w, r, r, bl, r, r, r, w, w, w, w, w, w],
25+
vec![w, w, r, r, r, bl, r, r, bl, r, r, r, w, w, w],
26+
vec![w, r, r, r, r, bl, bl, bl, bl, r, r, r, r, w, w],
27+
vec![w, lb, lb, r, bl, y, bl, bl, y, bl, r, lb, lb, w, w],
28+
vec![w, lb, lb, lb, bl, bl, bl, bl, bl, bl, lb, lb, lb, w, w],
29+
vec![w, lb, lb, bl, bl, bl, bl, bl, bl, bl, bl, lb, lb, w, w],
30+
vec![w, w, w, bl, bl, bl, w, w, bl, bl, bl, w, w, w, w],
31+
vec![w, w, s, s, s, w, w, w, w, s, s, s, w, w, w],
32+
vec![w, s, s, s, s, w, w, w, w, w, s, s, s, s, w],
33+
];
34+
let trace = Image::new(pixels).color_model(ColorModel::RGB);
35+
36+
let mut plot = Plot::new();
37+
plot.add_trace(trace);
38+
39+
plot.show()
40+
}
41+
42+
fn trace_from_image_crate_rgb() {
43+
let im = image::open("assets/mario.png").unwrap().into_rgb8();
44+
let trace = Image::new(im).color_model(ColorModel::RGB);
45+
46+
let mut plot = Plot::new();
47+
plot.add_trace(trace);
48+
49+
plot.show()
50+
}
51+
52+
fn trace_from_image_crate_rgba() {
53+
let im = image::open("assets/mario.png").unwrap().into_rgba8();
54+
let trace = Image::new(im).color_model(ColorModel::RGBA);
55+
56+
let mut plot = Plot::new();
57+
plot.add_trace(trace);
58+
59+
plot.show()
60+
}
61+
62+
fn trace_from_ndarray_rgb() {
63+
let pixels = arr2(&[
64+
[(255, 255, 255), (0, 0, 0)],
65+
[(0, 0, 0), (255, 255, 255)],
66+
[(255, 255, 255), (0, 0, 0)],
67+
]);
68+
let trace = Image::new(pixels).color_model(ColorModel::RGB);
69+
70+
let mut plot = Plot::new();
71+
plot.add_trace(trace);
72+
73+
plot.show()
74+
}
75+
76+
fn trace_from_ndarray_rgba() {
77+
let pixels = arr2(&[
78+
[(255, 255, 255, 1.), (0, 0, 0, 0.25)],
79+
[(0, 0, 0, 0.5), (255, 255, 255, 1.)],
80+
[(255, 255, 255, 1.), (0, 0, 0, 0.75)],
81+
]);
82+
let trace = Image::new(pixels).color_model(ColorModel::RGBA);
83+
84+
let mut plot = Plot::new();
85+
plot.add_trace(trace);
86+
87+
plot.show()
88+
}
89+
90+
fn main() {
91+
// Uncomment any of these lines to display the example.
92+
93+
// basic_image();
94+
// trace_from_image_crate_rgb();
95+
// trace_from_image_crate_rgba();
96+
// trace_from_ndarray_rgb();
97+
// trace_from_ndarray_rgba();
98+
}

examples/kaleido/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ fn main() {
55
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
66
plot.add_trace(trace);
77

8-
// Adjust these arguments to set the image format, width and height of the output image.
8+
// Adjust these arguments to set the image format, width and height of the
9+
// output image.
910
let filename = "out";
1011
let image_format = ImageFormat::PNG;
1112
let width = 800;
1213
let height = 600;
1314
let scale = 1.0;
1415

15-
// The image will be saved to format!("{filename}.{image_format}") relative to the current working directory.
16+
// The image will be saved to format!("{filename}.{image_format}") relative to
17+
// the current working directory.
1618
plot.write_image(filename, image_format, width, height, scale);
1719
}

0 commit comments

Comments
 (0)