11#![ allow( dead_code) ]
22
3- use plotly:: common:: { AxisSide , Font , Title } ;
4- use plotly:: layout:: { Axis , GridPattern , Layout , LayoutGrid , Legend , RowOrder , TraceOrder } ;
3+ use plotly:: common:: { Anchor , AxisSide , Font , Title } ;
4+ use plotly:: layout:: {
5+ Annotation , Axis , GridPattern , Layout , LayoutGrid , Legend , RowOrder , TraceOrder ,
6+ } ;
7+ use plotly:: Configuration ;
58use plotly:: { color:: Rgb , Plot , Scatter } ;
6-
79// Subplots
810fn simple_subplot ( ) {
911 let trace1 = Scatter :: new ( vec ! [ 1 , 2 , 3 ] , vec ! [ 4 , 5 , 6 ] ) . name ( "trace1" ) ;
@@ -242,6 +244,46 @@ fn multiple_axes() {
242244 plot. show ( ) ;
243245}
244246
247+ fn many_subplots_with_titles ( ) {
248+ let trace1 = Scatter :: new ( vec ! [ 1 , 2 ] , vec ! [ 4 , 5 ] ) ;
249+
250+ let number_of_plots = 10 ;
251+
252+ let mut plot = Plot :: new ( ) ;
253+ let mut layout = Layout :: new ( )
254+ . grid (
255+ LayoutGrid :: new ( )
256+ . rows ( number_of_plots / 2 )
257+ . columns ( 2 )
258+ . pattern ( GridPattern :: Independent ) ,
259+ )
260+ . height ( number_of_plots * 200 ) ;
261+
262+ for i in 1 ..number_of_plots + 1 {
263+ plot. add_trace (
264+ trace1
265+ . clone ( )
266+ . y_axis ( format ! ( "y{}" , i) )
267+ . x_axis ( format ! ( "x{}" , i) ) ,
268+ ) ;
269+ layout. add_annotation (
270+ Annotation :: new ( )
271+ . y_ref ( format ! ( "y{} domain" , i) )
272+ . y_anchor ( Anchor :: Bottom )
273+ . y ( 1 )
274+ . text ( format ! ( "Title {}" , i) )
275+ . x_ref ( format ! ( "x{} domain" , i) )
276+ . x_anchor ( Anchor :: Center )
277+ . x ( 0.5 )
278+ . show_arrow ( false ) ,
279+ )
280+ }
281+
282+ plot. set_layout ( layout) ;
283+ plot. set_configuration ( Configuration :: new ( ) . responsive ( true ) ) ;
284+ plot. show ( ) ;
285+ }
286+
245287fn main ( ) {
246288 // Uncomment any of these lines to display the example.
247289
@@ -252,6 +294,7 @@ fn main() {
252294 // stacked_subplots();
253295 // stacked_subplots_with_shared_x_axis();
254296 // multiple_custom_sized_subplots();
297+ // many_subplots_with_titles();
255298
256299 // Multiple Axes
257300 // two_y_axes();
0 commit comments