Generic plot utility

Assumes that xs and ys are lists.


In [1]:
(require gamble
         "c3_helpers.rkt")

Scatter plot

The default type.


In [2]:
(scatter-c3 '(1 2 3 4 5 5.5 6) '(8.2 7 5 4 3 2.5 0.9) 
            #:xlabel "cans of spinach consumed" #:ylabel "fights lost to Bluto")


Out[2]:
(c3-data . #hasheq((data . #hasheq((type . scatter) (xs . #hasheq((ys1 . xs1))) (columns . ((xs1 1 2 3 4 5 5.5 6) (ys1 8.2 7 5 4 3 2.5 0.9))))) (axis . #hasheq((x . #hasheq((label . cans of spinach consumed))) (y . #hasheq((label . fights lost to Bluto)))))))

Line plot

The old standby.


In [3]:
(line-c3 '(1 2 3 4 5 5.5 6) '(8.2 7 5 4 3 2.5 0.9) 
            #:xlabel "cans of spinach consumed" #:ylabel "fights lost to Bluto")


Out[3]:
(c3-data . #hasheq((data . #hasheq((type . line) (xs . #hasheq((ys1 . xs1))) (columns . ((xs1 1 2 3 4 5 5.5 6) (ys1 8.2 7 5 4 3 2.5 0.9))))) (axis . #hasheq((x . #hasheq((label . cans of spinach consumed))) (y . #hasheq((label . fights lost to Bluto)))))))

Bar chart

Looks lousy unless xs are evenly-spaced.

TODO: figure out how to customize the x tick labels.


In [4]:
(bar-c3 '(1 2 3 4 5 5.5 6) '(8.2 7 5 4 3 2.5 0.9) 
            #:xlabel "cans of spinach consumed" #:ylabel "fights lost to Bluto")


Out[4]:
(c3-data . #hasheq((data . #hasheq((type . bar) (xs . #hasheq((ys1 . xs1))) (columns . ((xs1 1 2 3 4 5 5.5 6) (ys1 8.2 7 5 4 3 2.5 0.9))))) (axis . #hasheq((x . #hasheq((label . cans of spinach consumed))) (y . #hasheq((label . fights lost to Bluto)))))))

Histogram

A special kind of bar chart, with a little arithmetic up front...


In [ ]:

sbs-hist-vals returns a list containing three lists:

  1. bin-edges, of length one greater than the optional input, n-bins. For $i$ less than or equal to n-bins, the $i$th element is the (inclusive) lower bound of the $i$th bin. The last element is the upper bound of the n-bins-th bin. All upper bounds are non-inclusive (strictly less than).
  2. bin-centers, of length n-bins. The $i$-th element is the average of the lower and upper bounds of of bin $i$.
  3. counts, the $i$th element of which is the number of elements of vals in the $i$th bin.

In [ ]:


In [ ]:


In [5]:
(require math/distributions)
(hist-c3 (sample (exponential-dist) 100))


Out[5]:
(c3-data . #hasheq((data . #hasheq((type . bar) (xs . #hasheq((counts . xs1))) (columns . ((xs1 0.15134236746281898 0.44665872716283855 0.741975086862858 1.0372914465628775 1.3326078062628972 1.6279241659629167 1.9232405256629361 2.2185568853629554 2.513873245062975 2.8091896047629943 3.104505964463014 3.3998223241630336 3.695138683863053 3.9904550435630726 4.285771403263093) (counts 23 21 17 8 9 5 2 3 3 3 1 0 2 1 2))))) (axis . #hasheq((x . #hasheq((label . values) (tick . #hasheq((values . (0.0 0.3 0.59 0.89 1.18 1.48 1.78 2.07 2.37 2.66 2.96 3.25 3.55 3.84 4.14 4.43)) (rotate . 90))))) (y . #hasheq((label . counts)))))))

Multiple plots

Blah blah...


In [6]:
(multi-line-c3 '((5 6 7 8) (5.5 6 7.5 8)) '((9 8 6 4) (10 12 13 14.7)))


Out[6]:
(c3-data . #hasheq((data . #hasheq((type . line) (xs . #hasheq((ys1 . xs1) (ys0 . xs0))) (columns . ((xs0 5 6 7 8) (xs1 5.5 6 7.5 8) (ys0 9 8 6 4) (ys1 10 12 13 14.7))))) (axis . #hasheq((x . #hasheq((label . x))) (y . #hasheq((label . y)))))))

In [ ]:

TODO:

  1. Hide the endpoint dots on the regression lines. "point" isn't working as expected.
  2. Make all the regression lines light grey.
  3. Make only one legend entry for all regression lines (or kill the legend entirely).

In [7]:
(multi-reg-line-plus-scatter-c3 
 '((0 10) (0 10) (0 10) (0 10) (0 10) (0 10) (0 10)) 
 '((3 7) (4 6.1) (3.5 6.8) (3.2 6.6) (3.38 7) (3.3 6.25) (3.6 6.7)) 
 '(1 3 4 7 8 9) 
 '(4 4.1 5.5 6 7 6.6))


Out[7]:
(c3-data . #hasheq((data . #hasheq((type . line) (types . #hasheq((true-ys . scatter))) (xs . #hasheq((true-ys . true-xs) (ys1 . xs1) (ys0 . xs0) (ys2 . xs2) (ys3 . xs3) (ys4 . xs4) (ys5 . xs5) (ys6 . xs6))) (columns . ((true-xs 1 3 4 7 8 9) (true-ys 4 4.1 5.5 6 7 6.6) (xs0 0 10) (xs1 0 10) (xs2 0 10) (xs3 0 10) (xs4 0 10) (xs5 0 10) (xs6 0 10) (ys0 3 7) (ys1 4 6.1) (ys2 3.5 6.8) (ys3 3.2 6.6) (ys4 3.38 7) (ys5 3.3 6.25) (ys6 3.6 6.7))))) (axis . #hasheq((x . #hasheq((label . x))) (y . #hasheq((label . y))))) (point . #hasheq((show . false)))))

In [ ]: