Skip to content

add example of subplots with multiple traces per subplot #313

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

Merged
merged 1 commit into from
Jun 20, 2025
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
8 changes: 8 additions & 0 deletions docs/book/src/recipes/subplots/subplots.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ The `to_inline_html` method is used to produce the html plot displayed in this p
{{#include ../../../../../examples/subplots/output/inline_simple_subplot.html}}


## Subplots with Multiple Traces
```rust,no_run
{{#include ../../../../../examples/subplots/src/main.rs:subplots_with_multiple_traces}}
```

{{#include ../../../../../examples/subplots/output/inline_subplot_with_multiple_traces.html}}


## Custom Sized Subplot
```rust,no_run
{{#include ../../../../../examples/subplots/src/main.rs:custom_sized_subplot}}
Expand Down
55 changes: 55 additions & 0 deletions examples/subplots/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,58 @@ fn many_subplots_with_titles(show: bool, file_name: &str) {
}
// ANCHOR_END: many_subplots_with_titles

// ANCHOR: subplots_with_multiple_traces
fn subplots_with_multiple_traces(show: bool, file_name: &str) {
// Create multiple traces for the first subplot (left side)
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 11, 12, 13])
.name("Line 1")
.mode(plotly::common::Mode::LinesMarkers);

let trace2 = Scatter::new(vec![1, 2, 3, 4], vec![8, 9, 10, 11])
.name("Line 2")
.mode(plotly::common::Mode::LinesMarkers);

let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 13, 14, 15])
.name("Line 3")
.mode(plotly::common::Mode::LinesMarkers);

// Create traces for the second subplot (right side)
let trace4 = Scatter::new(vec![1, 2, 3, 4], vec![20, 25, 30, 35])
.name("Dots 1")
.x_axis("x2")
.y_axis("y2")
.mode(plotly::common::Mode::Markers);

let trace5 = Scatter::new(vec![1, 2, 3, 4], vec![15, 20, 25, 30])
.name("Dots 2")
.x_axis("x2")
.y_axis("y2")
.mode(plotly::common::Mode::Markers);

let mut plot = Plot::new();
// Add traces to first subplot (default axes)
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
// Add traces to second subplot (x2, y2 axes)
plot.add_trace(trace4);
plot.add_trace(trace5);

let layout = Layout::new().title("Subplots with Multiple Traces").grid(
LayoutGrid::new()
.rows(1)
.columns(2)
.pattern(GridPattern::Independent),
);
plot.set_layout(layout);

let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: subplots_with_multiple_traces

fn main() {
// Change false to true on any of these lines to display the example.
// Subplots
Expand All @@ -404,6 +456,9 @@ fn main() {

many_subplots_with_titles(false, "many_subplots_with_titles");

// Multiple traces in subplots
subplots_with_multiple_traces(false, "subplots_with_multiple_traces");

// Multiple Axes
two_y_axes(false, "two_y_axes");
multiple_axes(false, "multiple_axes");
Expand Down
Loading