Skip to content

Commit 78e9f84

Browse files
committed
add another example of subplots with multiple traces per subplot
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 9bf35c2 commit 78e9f84

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

docs/book/src/recipes/subplots/subplots.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ The `to_inline_html` method is used to produce the html plot displayed in this p
1919
{{#include ../../../../../examples/subplots/output/inline_simple_subplot.html}}
2020

2121

22+
## Subplots with Multiple Traces
23+
```rust,no_run
24+
{{#include ../../../../../examples/subplots/src/main.rs:subplots_with_multiple_traces}}
25+
```
26+
27+
{{#include ../../../../../examples/subplots/output/inline_subplot_with_multiple_traces.html}}
28+
29+
2230
## Custom Sized Subplot
2331
```rust,no_run
2432
{{#include ../../../../../examples/subplots/src/main.rs:custom_sized_subplot}}

examples/subplots/src/main.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,60 @@ fn many_subplots_with_titles(show: bool, file_name: &str) {
386386
}
387387
// ANCHOR_END: many_subplots_with_titles
388388

389+
// ANCHOR: subplots_with_multiple_traces
390+
fn subplots_with_multiple_traces(show: bool, file_name: &str) {
391+
// Create multiple traces for the first subplot (left side)
392+
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 11, 12, 13])
393+
.name("Line 1")
394+
.mode(plotly::common::Mode::LinesMarkers);
395+
396+
let trace2 = Scatter::new(vec![1, 2, 3, 4], vec![8, 9, 10, 11])
397+
.name("Line 2")
398+
.mode(plotly::common::Mode::LinesMarkers);
399+
400+
let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 13, 14, 15])
401+
.name("Line 3")
402+
.mode(plotly::common::Mode::LinesMarkers);
403+
404+
// Create traces for the second subplot (right side)
405+
let trace4 = Scatter::new(vec![1, 2, 3, 4], vec![20, 25, 30, 35])
406+
.name("Dots 1")
407+
.x_axis("x2")
408+
.y_axis("y2")
409+
.mode(plotly::common::Mode::Markers);
410+
411+
let trace5 = Scatter::new(vec![1, 2, 3, 4], vec![15, 20, 25, 30])
412+
.name("Dots 2")
413+
.x_axis("x2")
414+
.y_axis("y2")
415+
.mode(plotly::common::Mode::Markers);
416+
417+
let mut plot = Plot::new();
418+
// Add traces to first subplot (default axes)
419+
plot.add_trace(trace1);
420+
plot.add_trace(trace2);
421+
plot.add_trace(trace3);
422+
// Add traces to second subplot (x2, y2 axes)
423+
plot.add_trace(trace4);
424+
plot.add_trace(trace5);
425+
426+
let layout = Layout::new()
427+
.title("Subplots with Multiple Traces")
428+
.grid(
429+
LayoutGrid::new()
430+
.rows(1)
431+
.columns(2)
432+
.pattern(GridPattern::Independent),
433+
);
434+
plot.set_layout(layout);
435+
436+
let path = write_example_to_html(&plot, file_name);
437+
if show {
438+
plot.show_html(path);
439+
}
440+
}
441+
// ANCHOR_END: subplots_with_multiple_traces
442+
389443
fn main() {
390444
// Change false to true on any of these lines to display the example.
391445
// Subplots
@@ -404,6 +458,9 @@ fn main() {
404458

405459
many_subplots_with_titles(false, "many_subplots_with_titles");
406460

461+
// Multiple traces in subplots
462+
subplots_with_multiple_traces(false, "subplots_with_multiple_traces");
463+
407464
// Multiple Axes
408465
two_y_axes(false, "two_y_axes");
409466
multiple_axes(false, "multiple_axes");

0 commit comments

Comments
 (0)