Skip to content

Commit c80324d

Browse files
authored
add #:scientific? argument to log-ticks and log-ticks-format (#86)
1 parent a237ef7 commit c80324d

File tree

11 files changed

+46
-7
lines changed

11 files changed

+46
-7
lines changed

plot-doc/plot/scribblings/ticks.scrbl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,19 @@ To format ticks, @racket[linear-ticks-format] uses @racket[real->plot-label] pas
296296
@deftogether[(@defproc[(log-ticks-layout [#:number number exact-positive-integer? (ticks-default-number)]
297297
[#:base base (and/c exact-integer? (>=/c 2)) 10])
298298
ticks-layout/c]
299-
@defproc[(log-ticks-format [#:base base (and/c exact-integer? (>=/c 2)) 10])
299+
@defproc[(log-ticks-format [#:base base (and/c exact-integer? (>=/c 2)) 10]
300+
[#:scientific? scientific? #t])
300301
ticks-format/c]
301302
@defproc[(log-ticks [#:number number exact-positive-integer? (ticks-default-number)]
302-
[#:base base (and/c exact-integer? (>=/c 2)) 10])
303+
[#:base base (and/c exact-integer? (>=/c 2)) 10]
304+
[#:scientific? scientific? #t])
303305
ticks?])]{
304306
The layout function, format function, and combined @racket[ticks] for exponentially spaced major ticks.
305307
(The minor ticks between are uniformly spaced.)
306308
Use these ticks for @racket[log-transform]ed axes, because when exponentially spaced tick positions are @racket[log-transform]ed, they become uniformly spaced.
307309

308310
The @racket[#:base] keyword argument is the logarithm base.
311+
The @racket[#:scientific] keyword argument disables scientific formatting, similarly to @racket[linear-ticks].
309312
See @racket[plot-z-far-ticks] for an example of use.
310313
}
311314

plot-lib/plot/private/common/ticks.rkt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@
216216
(and (zero? i) (zero? m))))]
217217
[else (list (pre-tick x (zero? m)))]))))]))]))
218218

219-
(:: log-ticks-format (->* [] [#:base Positive-Integer] Ticks-Format))
220-
(define (log-ticks-format #:base [base 10])
219+
(:: log-ticks-format (->* [] [#:base Positive-Integer #:scientific? Boolean] Ticks-Format))
220+
(define (log-ticks-format #:base [base 10] #:scientific? [scientific? #t])
221221
(cond
222222
[(< base 2) (error 'log-ticks-format "expected base >= 2; given ~e" base)]
223223
[else
@@ -231,6 +231,8 @@
231231
(cond [(<= x 0) (raise-argument-error 'log-ticks-format
232232
"(Listof pre-tick) with positive positions"
233233
2 x-min x-max ts)]
234+
[(not scientific?)
235+
(real->plot-label x base-digits scientific?)]
234236
[else
235237
(define log-x (floor-log/base base x))
236238
(define (major-str)
@@ -241,12 +243,12 @@
241243
(real->plot-label (/ x (expt base log-x)) base-digits)
242244
(major-str))])]))))]))
243245

244-
(:: log-ticks (->* [] [#:number Positive-Integer #:base Positive-Integer] ticks))
245-
(define (log-ticks #:number [number (ticks-default-number)] #:base [base 10])
246+
(:: log-ticks (->* [] [#:number Positive-Integer #:base Positive-Integer #:scientific? Boolean] ticks))
247+
(define (log-ticks #:number [number (ticks-default-number)] #:base [base 10] #:scientific? [scientific? #t])
246248
(cond
247249
[(< base 2) (error 'log-ticks "expected base >= 2; given ~e" base)]
248250
[else (ticks (log-ticks-layout #:number number #:base base)
249-
(log-ticks-format #:base base))]))
251+
(log-ticks-format #:base base #:scientific? scientific?))]))
250252

251253
;; ===================================================================================================
252254
;; Date/time helpers

plot-test/plot/tests/PRs/86.rkt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#lang racket
2+
(require rackunit
3+
plot pict racket/runtime-path
4+
"../helpers.rkt")
5+
6+
; tests for PR#86, https://github.com/racket/plot/pull/86
7+
; "add scientific? argument to log-ticks and log-ticks-format"
8+
9+
(define ((do-plot scientific? [base 10]) output-fn)
10+
(parameterize ([plot-x-ticks (log-ticks #:scientific? scientific? #:base base)]
11+
[plot-x-transform log-transform])
12+
(output-fn
13+
(function (λ (x) (log x base)) 1 1000))))
14+
15+
(define-runtime-path pr86-base10-sci-data "./test-data/pr86-1.dat")
16+
(define-runtime-path pr86-base10-nosci-data "./test-data/pr86-2.dat")
17+
(define-runtime-path pr86-base2-sci-data "./test-data/pr86-3.dat")
18+
(define-runtime-path pr86-base2-nosci-data "./test-data/pr86-4.dat")
19+
20+
(define pr86-test-suite
21+
(test-suite
22+
"PR#86: add scientific? to log-ticks"
23+
(test-case "PR86: base 10, scientific? #t"
24+
(check-draw-steps (do-plot #t 10) pr86-base10-sci-data))
25+
(test-case "PR86: base 10, scientific? #f"
26+
(check-draw-steps (do-plot #f 10) pr86-base10-nosci-data))
27+
(test-case "PR86: base 2, scientific? #t"
28+
(check-draw-steps (do-plot #t 2) pr86-base2-sci-data))
29+
(test-case "PR86: base 2, scientific? #f"
30+
(check-draw-steps (do-plot #f 2) pr86-base2-nosci-data))))
31+
32+
(module+ test
33+
(require rackunit/text-ui)
34+
(run-tests pr86-test-suite))
8.11 KB
Binary file not shown.
27.4 KB
Loading
7.94 KB
Binary file not shown.
26 KB
Loading
7.63 KB
Binary file not shown.
26.8 KB
Loading
7.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)