public
Created

TRing Hist* in Plot

  • Download Gist
gistfile1.rkt
Racket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#lang typed/racket/base
 
(require
(only-in racket/math
sqr)
(only-in typed/mred/mred
Snip%
Color%))
 
;; Use of mred/mred/Snip% fails :(
(define-type Image-Snip% Any)
 
(define-type Format
(U 'auto 'png 'jpeg
'xmb 'xpm 'bmp
'ps 'pdf 'svg))
 
(define-type Anchor
(U 'top-left 'top 'top-right
'left 'center 'right
'bottom-left 'bottom 'bottom-right))
 
(define-type Labels
(U (Listof (Option String))
((Listof Any) -> (Listof (Option String)))))
 
(define-type Alphas
(U (Listof Real)
((Listof Any) -> (Listof Real))))
 
(define-type Plot-Brush-Style
(U Integer 'transparent 'solid
'bdiagonal-hatch 'fdiagonal-hatch 'crossdiag-hatch
'horizontal-hatch 'vertical-hatch 'cross-hatch))
 
(define-type Plot-Brush-Styles
(U (Listof Plot-Pen-Style)
((Listof Any) -> (Listof Plot-Pen-Style))))
 
(define-type Plot-Pen-Style
(U Integer 'transparent 'solid
'bdiagnol-hatch 'fdiagonal-hatch
'horizontal-hath 'veritcal-hatch
'crossdisg-hatch 'cross-hatch))
 
(define-type Plot-Pen-Styles
(U (Listof Plot-Pen-Style)
((Listof Any) -> (Listof Plot-Pen-Style))))
 
(define-type Pen-Widths (U (Listof Plot-Pen-Style)
((Listof Any) -> (Listof Plot-Pen-Style))))
 
(define-type Plot-Color (U Integer (List Real Real Real)
String Symbol Color%))
 
(define-type Plot-Colors (U (Listof Plot-Color)
((Listof Any) -> (Listof Plot-Color))))
 
(define-type Discrete-Histogram-Data (Listof (Vector Any (Option (U Real Interval)))))
 
(define-type Stacked-Histogram-Data (Listof (Vector Any (Listof Real))))
 
(require/typed plot/utils
[opaque Renderer2d renderer2d?]
[opaque Nonrenderer nonrenderer?]
[linear-seq (Real Real Exact-Nonnegative-Integer -> (Listof Real))])
 
(define-type Renderer-Tree (Rec RTree (U (U Renderer2d Nonrenderer) (Listof RTree))))
 
(require/typed plot/main
[opaque Interval ivl?]
[ivl (Real Real -> Interval)]
[plot (Renderer-Tree
[#:x-min (Option Exact-Rational)]
[#:x-max (Option Exact-Rational)]
[#:y-min (Option Exact-Rational)]
[#:y-max (Option Exact-Rational)]
[#:width Natural]
[#:height Natural]
[#:title (Option String)]
[#:x-label (Option String)]
[#:y-label (Option String)]
[#:legend-anchor Anchor]
[#:out-file (Option (U Path Output-Port))]
[#:out-kind Format]
-> (U Image-Snip% Void))]
[rectangles ((Listof (Vector Interval Interval))
[#:x-min (Option Exact-Rational)]
[#:x-max (Option Exact-Rational)]
[#:y-min (Option Exact-Rational)]
[#:y-max (Option Exact-Rational)]
[#:color Plot-Color]
[#:style Plot-Brush-Style]
[#:line-color Plot-Color]
[#:line-width Plot-Brush-Style]
[#:line-style Plot-Pen-Style]
[#:alpha Real]
[#:label (Option String)]
-> Renderer2d)]
;; TR does not support KW and optional args.
[function ((Real -> Real)
(Option Exact-Rational)
(Option Exact-Rational)
[#:y-min Exact-Rational]
[#:y-max Exact-Rational]
[#:samples Integer]
[#:color Plot-Color]
[#:width Real]
[#:style Plot-Pen-Style]
[#:alpha Real]
[#:label (Option String)]
-> Renderer2d)]
[area-histogram ((Real -> Real)
(Listof Real)
[#:x-min (Option Exact-Rational)]
[#:x-max (Option Exact-Rational)]
[#:y-min (Option Exact-Rational)]
[#:y-max (Option Exact-Rational)]
[#:samples Integer]
[#:color Plot-Color]
[#:style Plot-Brush-Style]
[#:line-color Plot-Color]
[#:line-width Plot-Brush-Style]
[#:line-style Plot-Pen-Style]
[#:alpha Real]
[#:label (Option String)]
-> Renderer2d)]
[discrete-histogram (Discrete-Histogram-Data
[#:x-min (Option Exact-Rational)]
[#:x-max (Option Exact-Rational)]
[#:y-min (Option Exact-Rational)]
[#:y-max (Option Exact-Rational)]
[#:gap Real]
[#:skip Real]
[#:invert? Boolean]
[#:color Plot-Color]
[#:style Plot-Brush-Style]
[#:line-color Plot-Color]
[#:line-width Plot-Brush-Style]
[#:line-style Plot-Pen-Style]
[#:alpha Real]
[#:label (Option String)]
[#:add-ticks? Boolean]
[#:far-ticks? Boolean]
-> Renderer2d)]
[stacked-histogram (Stacked-Histogram-Data
[#:x-min (Option Exact-Rational)]
[#:x-max (Option Exact-Rational)]
[#:y-min (Option Exact-Rational)]
[#:y-max (Option Exact-Rational)]
[#:gap Real]
[#:skip Real]
[#:invert? Boolean]
[#:colors Plot-Colors]
[#:styles Plot-Brush-Styles]
[#:line-colors Plot-Colors]
[#:line-widths Pen-Widths]
[#:line-styles Plot-Pen-Styles]
[#:alphas (U Alphas Natural)]
[#:labels (U Labels Natural)]
[#:add-ticks? Boolean]
[#:far-ticks? Boolean]
-> (Listof Renderer2d))])
 
(define (test)
(: f (Real -> Real))
(define (f x) (exp (* -1/2 (sqr x))))
(plot (list (area-histogram f (linear-seq -4 4 10)
#:label "Root 2")
(function f -4 4))))
 
(define (test2)
(let: ((data : Discrete-Histogram-Data
(list #(A 1) #(B 2) #(B 3)
(vector 'C (ivl 0.5 1.5)))))
(plot (discrete-histogram data #:label "Hello"))))
 
(define (test3)
(let: ((h1-data : Discrete-Histogram-Data '(#(Eggs 1.5) #(Bacon 2.5) #(Pancakes 3.5)))
(h2-data : Discrete-Histogram-Data '(#(Eggs 1.4) #(Bacon 2.3) #(Pancakes 3.1))))
(plot (list (discrete-histogram
h1-data
#:skip 2.5 #:x-min 0
#:label "AMD")
(discrete-histogram
h2-data
#:skip 2.5 #:x-min 1
#:label "Intel" #:color 2 #:line-color 2))
#:x-label "Breakfast Food" #:y-label "Cooking Time (minutes)"
#:title "Cooking Times For Breakfast Food, Per Processor")))
(define (test4)
(let: ((data : Stacked-Histogram-Data
(list #(a (1 1 1)) #(b (1.5 3))
#(c ()) #(d (1/2)))))
(plot (stacked-histogram data
#:invert? #t
#:labels '("Red" #f "Blue"))
#:legend-anchor 'top-right)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.