Skip to content

Commit

Permalink
Added subplot example with multiple titles (#166)
Browse files Browse the repository at this point in the history
* Added example with multiple titles

Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>

---------

Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
Co-authored-by: Andrei <8067229+andrei-ng@users.noreply.github.com>
  • Loading branch information
prokie and andrei-ng authored Jun 12, 2024
1 parent e14193c commit c642253
Showing 1 changed file with 46 additions and 3 deletions.
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

0 comments on commit c642253

Please sign in to comment.