PasteRack.org
Paste # 88833
2022-11-16 23:09:26

Forked from paste # 30113.

Fork as a new paste.

Paste viewed 1019 times.


Embed:

violin plots

  1. #lang racket/base
  2.  
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4. ;; require
  5.  
  6. (require math/statistics
  7.          plot/pict
  8.          plot/utils
  9.          racket/dict
  10.          racket/function
  11.          racket/list)
  12.  
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ;; violin
  15.  
  16. (define (violin vals
  17.                 #:bandwidth [bandwidth (silverman (second vals))]
  18.                 #:x-min [x-min #f]
  19.                 #:x-max [x-max #f]
  20.                 #:y-min [y-min #f]
  21.                 #:y-max [y-max #f]
  22.                 #:color [color (interval-color)]
  23.                 #:style [style (interval-style)]
  24.                 #:line1-color [line1-color (interval-line1-color)]
  25.                 #:line1-width [line1-width (interval-line1-width)]
  26.                 #:line1-style [line1-style (interval-line1-style)]
  27.                 #:line2-color [line2-color (interval-line2-color)]
  28.                 #:line2-width [line2-width (interval-line2-width)]
  29.                 #:line2-style [line2-style (interval-line2-style)]
  30.                 #:alpha [alpha (interval-alpha)]
  31.                 #:label [label #f])
  32.   (define y-shift (first vals))
  33.   (define-values (f low high)
  34.     (kde (second vals) bandwidth))
  35.   (define x-axis (const 0))
  36.   (define x-min* (or x-min low))
  37.   (define x-max* (or x-max high))
  38.   (define settings
  39.     `([#:y-min . ,y-min]
  40.       [#:y-max . ,y-max]
  41.       [#:color . ,color]
  42.       [#:style . ,style]
  43.       [#:line1-color . ,line1-color]
  44.       [#:line1-width . ,line1-width]
  45.       [#:line1-style . ,line1-style]
  46.       [#:line2-color . ,line2-color]
  47.       [#:line2-width . ,line2-width]
  48.       [#:line2-style . ,line2-style]
  49.       [#:alpha . ,alpha]
  50.       [#:label . ,label]))
  51.   (list (keyword-apply/dict function-interval settings
  52.                             (shift-up (invert f) y-shift)
  53.                             (shift-up f y-shift)
  54.                             x-min* x-max* null)))
  55.  
  56. (define (shift-up f shift)
  57.   (λ (x)
  58.     (+ (f x) shift)))
  59.  
  60. (define ((invert f) x)
  61.   (- (f x)))
  62.  
  63. (define (silverman vals)
  64.   (define iqr (interquartile-range vals))
  65.   (define n (length vals))
  66.   (* 0.9
  67.      (min (stddev vals) (/ iqr 1.34))
  68.      (expt n -0.2)))
  69.  
  70. (define (interquartile-range vals)
  71.   (- (quantile 3/4 < vals)
  72.      (quantile 1/4 < vals)))
  73.  
  74. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75. ;; example
  76. ;;
  77. ;; `(violin (list y-center x-data))`
  78. ;;
  79.  
  80. (parameterize ([plot-y-ticks no-ticks]
  81.                [plot-y-label #f]
  82.                [plot-x-far-ticks no-ticks]
  83.                [plot-x-label "Time (sec)"])
  84.   (plot (list (violin `[0.00 (0 1 1 2 3 4 4 4 5 6 7 9 10 10 10 11 13)])
  85.               (violin `[0.30 (15 16 17 18 19 20 20 21 23 30)]))))

=>

image