[racket] Producing a PLoT with labels on the histogram bars

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Mon Feb 20 20:21:11 EST 2012

Just wanted to post this just in case it helps anyone else:


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket
(require plot)

;; histogram-with-points: (sequenceof (sequence any/c real)) ->
(listof renderer2d)
;;
;; Given a sequence of data to draw as bar graphs, uses
discrete-histogram.  Also
;; adds points at the top of each bar.
;;
(define (histogram-with-points data)
  (define-values (histogram-points label-points)
    (for/fold ([histogram-points '()]
               [label-points '()])
              ([data-point data]
               [i (in-naturals)])
       (define floored-val (inexact->exact (floor (second data-point))))
       (values (cons (discrete-histogram (list (vector (first
data-point) (second data-point)))
                                         #:x-min i)
                     histogram-points)
               (cons (point-label (vector (+ i 0.5) (second
data-point)) (number->string floored-val))
                     label-points))))
  (append histogram-points label-points))

;; Example:
(plot (histogram-with-points '((gauss 50)
                               (gauss-iter 60)
                               (cpstack 40)))
      #:y-max 70)

Posted on the users mailing list.