@@ -6,8 +6,9 @@ use plotly::{
66 color:: { NamedColor , Rgb , Rgba } ,
77 common:: { ErrorData , ErrorType , Line , Marker , Mode , Orientation } ,
88 histogram:: { Bins , Cumulative , HistFunc , HistNorm } ,
9- layout:: { Axis , BarMode , BoxMode , Layout , Margin } ,
10- Bar , BoxPlot , Histogram , Plot , Scatter ,
9+ layout:: { Axis , BarMode , BoxMode , Layout , Margin , ViolinMode } ,
10+ violin:: { MeanLine , ViolinBox , ViolinPoints , ViolinSide } ,
11+ Bar , BoxPlot , Histogram , Plot , Scatter , Violin ,
1112} ;
1213use plotly_utils:: write_example_to_html;
1314use rand_distr:: { Distribution , Normal , Uniform } ;
@@ -477,6 +478,98 @@ fn fully_styled_box_plot(show: bool, file_name: &str) {
477478}
478479// ANCHOR_END: fully_styled_box_plot
479480
481+ // Violin Plots
482+ // ANCHOR: basic_violin_plot
483+ fn basic_violin_plot ( show : bool , file_name : & str ) {
484+ let y = vec ! [
485+ 0.2 , 0.2 , 0.6 , 1.0 , 0.5 , 0.4 , 0.2 , 0.7 , 0.9 , 0.1 , 0.5 , 0.3 , 0.8 , 0.4 , 0.6 ,
486+ ] ;
487+
488+ let trace = Violin :: new ( y)
489+ . box_plot ( ViolinBox :: new ( ) . visible ( true ) )
490+ . mean_line ( MeanLine :: new ( ) . visible ( true ) )
491+ . name ( "Total" ) ;
492+
493+ let layout = Layout :: new ( ) . title ( "Basic Violin Plot" ) ;
494+
495+ let mut plot = Plot :: new ( ) ;
496+ plot. set_layout ( layout) ;
497+ plot. add_trace ( trace) ;
498+
499+ let path = write_example_to_html ( & plot, file_name) ;
500+ if show {
501+ plot. show_html ( path) ;
502+ }
503+ }
504+ // ANCHOR_END: basic_violin_plot
505+
506+ // ANCHOR: horizontal_violin_plot
507+ fn horizontal_violin_plot ( show : bool , file_name : & str ) {
508+ let x = vec ! [ 1.4 , 2.1 , 1.9 , 3.2 , 2.7 , 2.2 , 1.8 , 2.5 , 3.1 , 2.0 , 2.6 , 1.7 ] ;
509+
510+ let trace = Violin :: < f64 , f64 > :: default ( )
511+ . x ( x)
512+ . points ( ViolinPoints :: All )
513+ . box_plot ( ViolinBox :: new ( ) . visible ( true ) )
514+ . mean_line ( MeanLine :: new ( ) . visible ( true ) )
515+ . orientation ( Orientation :: Horizontal )
516+ . name ( "Score" ) ;
517+
518+ let layout = Layout :: new ( ) . title ( "Horizontal Violin Plot" ) ;
519+
520+ let mut plot = Plot :: new ( ) ;
521+ plot. set_layout ( layout) ;
522+ plot. add_trace ( trace) ;
523+
524+ let path = write_example_to_html ( & plot, file_name) ;
525+ if show {
526+ plot. show_html ( path) ;
527+ }
528+ }
529+ // ANCHOR_END: horizontal_violin_plot
530+
531+ // ANCHOR: split_violin_plot
532+ fn split_violin_plot ( show : bool , file_name : & str ) {
533+ let x = vec ! [
534+ "Mon" , "Mon" , "Mon" , "Mon" , "Tue" , "Tue" , "Tue" , "Tue" , "Wed" , "Wed" , "Wed" , "Wed" ,
535+ ] ;
536+
537+ let trace1 = Violin :: new_xy (
538+ x. clone ( ) ,
539+ vec ! [ 0.6 , 0.9 , 0.4 , 0.7 , 0.8 , 1.1 , 0.6 , 0.9 , 1.0 , 1.3 , 0.8 , 1.1 ] ,
540+ )
541+ . legend_group ( "Yes" )
542+ . scale_group ( "Yes" )
543+ . name ( "Yes" )
544+ . side ( ViolinSide :: Negative )
545+ . line ( Line :: new ( ) . color ( NamedColor :: Blue ) ) ;
546+
547+ let trace2 = Violin :: new_xy (
548+ x,
549+ vec ! [ 0.4 , 0.7 , 0.3 , 0.5 , 0.6 , 0.9 , 0.4 , 0.7 , 0.8 , 1.1 , 0.6 , 0.9 ] ,
550+ )
551+ . legend_group ( "No" )
552+ . scale_group ( "No" )
553+ . name ( "No" )
554+ . side ( ViolinSide :: Positive )
555+ . line ( Line :: new ( ) . color ( NamedColor :: Green ) ) ;
556+
557+ let layout = Layout :: new ( )
558+ . title ( "Split Violin Plot" )
559+ . violin_mode ( ViolinMode :: Overlay ) ;
560+
561+ let mut plot = Plot :: new ( ) ;
562+ plot. set_layout ( layout) ;
563+ plot. add_trace ( trace1) ;
564+ plot. add_trace ( trace2) ;
565+
566+ let path = write_example_to_html ( & plot, file_name) ;
567+ if show {
568+ plot. show_html ( path) ;
569+ }
570+ }
571+ // ANCHOR_END: split_violin_plot
572+
480573// Histograms
481574fn sample_normal_distribution ( n : usize , mean : f64 , std_dev : f64 ) -> Vec < f64 > {
482575 let mut rng = rand:: rng ( ) ;
@@ -729,6 +822,11 @@ fn main() {
729822 grouped_horizontal_box_plot ( false , "grouped_horizontal_box_plot" ) ;
730823 fully_styled_box_plot ( false , "fully_styled_box_plot" ) ;
731824
825+ // Violin Plots
826+ basic_violin_plot ( false , "basic_violin_plot" ) ;
827+ horizontal_violin_plot ( false , "horizontal_violin_plot" ) ;
828+ split_violin_plot ( false , "split_violin_plot" ) ;
829+
732830 // Histograms
733831 basic_histogram ( false , "basic_histogram" ) ;
734832 horizontal_histogram ( false , "horizontal_histogram" ) ;
0 commit comments