使用名称而不是x轴上的数字在Racket中绘图

时间:2017-01-08 16:51:00

标签: plot scheme racket

我可以使用Using Racket to Plot Points上显示的以下代码绘制点数:

(require plot)
(define xs '(0 1 2 3 4 5))
(define ys '(0 1 4 9 16 25))
(plot (points (map vector xs ys) #:color 'red))

但是我如何绘制x轴点的名称而不是数字:

(define xs '(AA BB CC DD AA CC))

使用上面的代码尝试它会产生一条很长的错误消息:

points: contract violation
  expected: Real
  given: 'AA
  in: an element of
      an element of
      the 1st argument of
      (->*
       ((sequence/c (sequence/c Real)))
       (#:alpha
        Nonnegative-Real
        #:color
        (or/c
         Integer
         Symbol
         String
         (recursive-contract g2149 #:impersonator)
         (cons/c
          Real
          (cons/c Real (cons/c Real ()))))
        #:fill-color
        (or/c
         Integer
         Symbol
         String
         (recursive-contract g2149 #:impersonator)
         (cons/c
          Real
          (cons/c Real (cons/c Real ()))))
        #:label
        (or/c #f String)
        #:line-width
        Nonnegative-Real
        #:size
        Nonnegative-Real
        #:sym
        (or/c
         Integer
         Char
         String
         dot
         point
         pixel
         plus
         times
         asterisk
         5asterisk
         odot
         oplus
         otimes
         oasterisk
         o5asterisk
         circle
         square
         diamond
         triangle
         fullcircle
         fullsquare
         fulldiamond
         fulltriangle
         triangleup
         triangledown
         triangleleft
         triangleright
         fulltriangleup
         fulltriangledown
         fulltriangleleft
         fulltriangleright
         rightarrow
         leftarrow
         uparrow
         downarrow
         4star
         5star
         6star
         7star
         8star
         full4star
         full5star
         full6star
         full7star
         full8star
         circle1
         circle2
         circle3
         circle4
         circle5
         circle6
         circle7
         circle8
         bullet
         fullcircle1
         fullcircle2
         fullcircle3
         fullcircle4
         fullcircle5
         fullcircle6
         fullcircle7
         fullcircle8)
        #:x-max
        (or/c Real #f)
        #:x-min
        (or/c Real #f)
        #:y-max
        (or/c Real #f)
        #:y-min
        (or/c Real #f))
       any)
  contract from: 
      <pkgs>/plot-lib/plot/private/plot2d/point.rkt
  blaming: anonymous-module
   (assuming the contract is correct)
  at: <pkgs>/plot-lib/plot/private/plot2d/point.rkt:47.9

如何制作x轴具有组名而不是数字的图?

1 个答案:

答案 0 :(得分:3)

您可能正在做两件事之一。我的第一个猜测是你正在绘制某种直方图;也就是说,您的XS代表不同的类别。在这种情况下,您可能希望使用例如discrete-histogram。在这种情况下,你要写

#lang racket
(require plot)
(define xs '(AA BB CC DD AA CC))
(define ys '(0 1 4 9 16 25))
(plot (discrete-histogram (map vector xs ys) #:color 'red))

如果这不是您的想法,请告诉我们。

编辑:根据下面的讨论,您可以在此处更新特定值的刻度标签。

#lang racket

(require plot)

(define current-layout
  (ticks-layout (plot-x-ticks)))
(define current-formatter
  (ticks-format (plot-x-ticks)))

(define my-label-hash
  (hash 1 "AA"
        2 "BB"))

(parameterize
    ([plot-x-ticks
      (ticks (ticks-layout (plot-x-ticks))
             (λ (min max pre-ticks)
               (define default-layed-out
                 (current-formatter min max pre-ticks))
               (for/list ([pt (in-list pre-ticks)]
                          [default (in-list default-layed-out)])
                 (or (hash-ref my-label-hash (pre-tick-value pt) #f)
                     default))))])
  (plot (function (λ (x) x))
        #:x-min 0
        #:x-max 4))