diff --git a/examples/subplots/src/main.rs b/examples/subplots/src/main.rs index 88af0dcb..18aee5ac 100644 --- a/examples/subplots/src/main.rs +++ b/examples/subplots/src/main.rs @@ -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"); @@ -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. @@ -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();