Skip to content

Added subplot example with multiple titles #166

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 3 commits into from
Jun 12, 2024
Merged
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
49 changes: 46 additions & 3 deletions examples/subplots/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![allow(dead_code)]

use plotly::common::{AxisSide, Font, Title};
use plotly::layout::{Axis, GridPattern, Layout, LayoutGrid, Legend, RowOrder, TraceOrder};
use plotly::common::{Anchor, AxisSide, Font, Title};
use plotly::layout::{
Annotation, Axis, GridPattern, Layout, LayoutGrid, Legend, RowOrder, TraceOrder,
};
use plotly::Configuration;
use plotly::{color::Rgb, Plot, Scatter};

// Subplots
fn simple_subplot() {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
Expand Down Expand Up @@ -242,6 +244,46 @@ fn multiple_axes() {
plot.show();
}

fn many_subplots_with_titles() {
let trace1 = Scatter::new(vec![1, 2], vec![4, 5]);

let number_of_plots = 10;

let mut plot = Plot::new();
let mut layout = Layout::new()
.grid(
LayoutGrid::new()
.rows(number_of_plots / 2)
.columns(2)
.pattern(GridPattern::Independent),
)
.height(number_of_plots * 200);

for i in 1..number_of_plots + 1 {
plot.add_trace(
trace1
.clone()
.y_axis(format!("y{}", i))
.x_axis(format!("x{}", i)),
);
layout.add_annotation(
Annotation::new()
.y_ref(format!("y{} domain", i))
.y_anchor(Anchor::Bottom)
.y(1)
.text(format!("Title {}", i))
.x_ref(format!("x{} domain", i))
.x_anchor(Anchor::Center)
.x(0.5)
.show_arrow(false),
)
}

plot.set_layout(layout);
plot.set_configuration(Configuration::new().responsive(true));
plot.show();
}

fn main() {
// Uncomment any of these lines to display the example.

Expand All @@ -252,6 +294,7 @@ fn main() {
// stacked_subplots();
// stacked_subplots_with_shared_x_axis();
// multiple_custom_sized_subplots();
// many_subplots_with_titles();

// Multiple Axes
// two_y_axes();
Expand Down
Loading