Created
November 5, 2021 10:48
-
-
Save Metaxal/0cb555abb0d928d2d892ee92580d3623 to your computer and use it in GitHub Desktop.
Customizing plot ticks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require plot) | |
(define (tlayout low high) | |
(list | |
; major ticks: tick is shown with label | |
(pre-tick pi #t) | |
(pre-tick (sqrt 2) #t) | |
(pre-tick 0.5 #t) | |
; minor ticks: tick is shown, but without label | |
(pre-tick 1 #f) | |
(pre-tick 2 #f) | |
(pre-tick 3 #f) | |
(pre-tick 4 #f))) | |
(define (tformat low high pre-ticks) | |
; custom formatter of the major tick labels | |
(map (λ (p) (~r (pre-tick-value p) | |
#:precision 4 | |
#:min-width 4)) | |
pre-ticks)) | |
(parameterize ([plot-x-ticks | |
(ticks tlayout tformat)]) | |
(plot | |
(function values 0 5))) |
Author
Metaxal
commented
Nov 5, 2021
and this is how to have specific labels at specific positions:
(define (make-string-ticks l)
;; Plot uses exact numbers, so we need to convert the values.
;; Turn into a dict at the same time
(define d (map (λ (p) (cons (inexact->exact (first p)) (second p))) l))
(ticks
; layout
(λ (low high) (filter-map (λ (p) (and (<= low (car p) high)
(pre-tick (car p) #t)))
d))
;format
(λ (low high pre-ticks)
(map (λ (t) (dict-ref d (pre-tick-value t))) pre-ticks))))
(parameterize ([plot-y-ticks (make-string-ticks '((1 "ya") (1.5 "yb") (2.3 "yc")))]
[plot-x-ticks (make-string-ticks '((0 "a") (1 "b") (2.92 "c")))])
(plot (function sqrt 0 10)))
(yes, the numbers are wrong, I don't care)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment