├── .github
├── FUNDING.yml
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── suggest-a-recipe.md
├── .gitignore
├── LICENSE
├── README.md
├── examples
├── Iso
│ ├── Iso.png
│ ├── Iso.rkt
│ └── README.md
├── colorbar
│ ├── README.md
│ ├── colorbar.rkt
│ └── colorbar.svg
├── cosandderiv
│ ├── README.md
│ ├── cosandderiv.png
│ └── cosandderiv.rkt
├── f1
│ ├── F1.txt
│ ├── README.md
│ ├── f1.md
│ ├── f1.pdf
│ ├── f1.png
│ └── plots-splines.rkt
├── f2
│ ├── F2.txt
│ ├── README.md
│ ├── f2.pdf
│ ├── f2.png
│ └── plots-splines-text-outside.rkt
├── face
│ ├── 59ECFEE5-B9BF-40BA-A2DA-BC3E0655F5C3.png
│ ├── README.md
│ └── face.rkt
├── logo
│ ├── README.md
│ ├── logo-plot.png
│ └── logo.rkt
├── lorenz
│ ├── README.md
│ ├── lorenz.rkt
│ └── lorenz.svg
├── rose
│ ├── README.md
│ ├── rose.rkt
│ └── rose.svg
├── sines
│ ├── README.md
│ ├── sines.png
│ └── sines.rkt
└── violin
│ ├── README.md
│ ├── violin.png
│ └── violin.rkt
├── info.rkt
├── main.rkt
└── scribblings
├── basic-cook.scrbl
├── common.rkt
├── plot-cookbook.scrbl
├── sines.scrbl
└── violin.scrbl
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | github: racket
2 | custom: https://racket-lang.org/sfc.html
3 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: "[BUG]"
5 | labels: bug
6 | assignees: spdegabrielle
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/suggest-a-recipe.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Suggest a recipe
3 | about: Quickly suggest a recipe
4 | title: "[new recipe]"
5 | labels: ''
6 | assignees: spdegabrielle
7 |
8 | ---
9 |
10 |
11 |
12 |
13 |
14 | # *a title for your recipe*
15 |
16 | *image* (you can paste an image and click preview tab to see it)
17 |
18 | *A description for your recipe*
19 |
20 | ## example code
21 |
22 | ```scheme
23 | #lang racket
24 |
25 |
26 | ```
27 |
28 | Link to external code: http://pasterack.org/pastes/97561
29 |
30 |
31 | ## data files
32 |
33 | filename:
34 | ```
35 | *paste file contents here*
36 | ```
37 |
38 | external file link:
39 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | \#*
3 | .\#*
4 | .DS_Store
5 | compiled/
6 | /doc/
7 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Racket-Cookbooks
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Racket Plot Cookbook
2 |
3 |
4 |
5 | [](https://racket-lang.org)
6 | [](https://racket.discourse.group/)
7 | [](https://discord.gg/6Zq8sH5)
8 |
9 | Welcome to the Racket Plot Cookbook. This is a growing collection of recipes for creating the tastiest plots with the [Racket Plot library](https://docs.racket-lang.org/plot/index.html) (documentation).
10 |
11 | If you would like to contribute please create an issue or pull request with your contribution. Please include image, code, and short description.
12 |
13 | Contributions are accepted on the condition they are licenced under the same terms as Racket: MIT or Apache 2.
14 |
15 | ## Plots
16 |
17 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/examples/Iso/Iso.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/Iso/Iso.png
--------------------------------------------------------------------------------
/examples/Iso/Iso.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 | (require plot)
3 | ;; Iso
4 |
5 | (define xg0 1)
6 |
7 | (define (g x)
8 | (/ 3 (- x xg0)))
9 |
10 | (define Xs
11 | '(0.0
12 | 2.302785323219288
13 | 3.501885860538783
14 | 4.387531553378729
15 | 5.116359864244201
16 | 5.748205229427828))
17 |
18 | (define aspect-ratio 2.)
19 | (define xmax 7)
20 | (define ymax (/ xmax aspect-ratio))
21 |
22 | (parameterize ([plot-x-ticks no-ticks]
23 | [plot-y-ticks no-ticks])
24 | (plot
25 | #:aspect-ratio aspect-ratio
26 | #:y-min 0 #:y-max ymax
27 | #:x-max xmax
28 | #:width 700
29 | #:x-label #f
30 | #:legend-anchor 'top-right
31 | (list (function g #:color 1 #:label "g(x)")
32 | (x-ticks (list (tick 0 #t "X₀=0")))
33 | (lines-interval (list (list 0 0) (list 0 ymax))
34 | (list (list xg0 0) (list xg0 ymax))
35 | #:line1-style 'transparent
36 | #:line2-color 1 #:line2-style 'long-dash
37 | #:color 1 #:style 'fdiagonal-hatch #:alpha 1)
38 | (for/list ([X Xs]
39 | [X2 (rest Xs)]
40 | [t (in-naturals 1)]
41 | [t-1_idx '(₀ ₁ ₂ ₃ ₄ ₅ ₆)]
42 | [t_idx '(₁ ₂ ₃ ₄ ₅ ₆ ₇)])
43 | (define gX2 (g X2))
44 | (define X2+δ (+ X2 (/ (+ t 1.)) #;(* .4 (- X2 X))))
45 | (list
46 | (function (λ (x) (- x X))
47 | #:color "black"
48 | X X2+δ)
49 | (vrule X2 0 gX2 #:style 'long-dash #:color "black")
50 | (point-label (list X2 gX2) (format "iso~a" t_idx) #:anchor 'left)
51 | (x-ticks (list (tick X2 #t (format "X~a" t_idx))))
52 | (point-label (list X2+δ (- X2+δ X)) (format "y = x - X~a" t-1_idx)
53 | #:anchor 'left #:point-size 0)))
54 | )))
55 |
56 |
--------------------------------------------------------------------------------
/examples/Iso/README.md:
--------------------------------------------------------------------------------
1 | # Iso
2 |
3 | 
4 |
5 | ```scheme
6 | #lang racket
7 | (require plot)
8 |
9 | (define xg0 1)
10 |
11 | (define (g x)
12 | (/ 3 (- x xg0)))
13 |
14 | (define Xs
15 | '(0.0
16 | 2.302785323219288
17 | 3.501885860538783
18 | 4.387531553378729
19 | 5.116359864244201
20 | 5.748205229427828))
21 |
22 | (define aspect-ratio 2.)
23 | (define xmax 7)
24 | (define ymax (/ xmax aspect-ratio))
25 |
26 | (parameterize ([plot-x-ticks no-ticks]
27 | [plot-y-ticks no-ticks])
28 | (plot
29 | #:aspect-ratio aspect-ratio
30 | #:y-min 0 #:y-max ymax
31 | #:x-max xmax
32 | #:width 700
33 | #:x-label #f
34 | #:legend-anchor 'top-right
35 | (list (function g #:color 1 #:label "g(x)")
36 | (x-ticks (list (tick 0 #t "X₀=0")))
37 | (lines-interval (list (list 0 0) (list 0 ymax))
38 | (list (list xg0 0) (list xg0 ymax))
39 | #:line1-style 'transparent
40 | #:line2-color 1 #:line2-style 'long-dash
41 | #:color 1 #:style 'fdiagonal-hatch #:alpha 1)
42 | (for/list ([X Xs]
43 | [X2 (rest Xs)]
44 | [t (in-naturals 1)]
45 | [t-1_idx '(₀ ₁ ₂ ₃ ₄ ₅ ₆)]
46 | [t_idx '(₁ ₂ ₃ ₄ ₅ ₆ ₇)])
47 | (define gX2 (g X2))
48 | (define X2+δ (+ X2 (/ (+ t 1.)) #;(* .4 (- X2 X))))
49 | (list
50 | (function (λ (x) (- x X))
51 | #:color "black"
52 | X X2+δ)
53 | (vrule X2 0 gX2 #:style 'long-dash #:color "black")
54 | (point-label (list X2 gX2) (format "iso~a" t_idx) #:anchor 'left)
55 | (x-ticks (list (tick X2 #t (format "X~a" t_idx))))
56 | (point-label (list X2+δ (- X2+δ X)) (format "y = x - X~a" t-1_idx)
57 | #:anchor 'left #:point-size 0)))
58 | )))
59 |
60 | ```
61 |
62 | http://pasterack.org/pastes/28373
63 |
64 | ***
65 |
--------------------------------------------------------------------------------
/examples/colorbar/README.md:
--------------------------------------------------------------------------------
1 | 
2 | ```scheme
3 | #lang racket
4 | (require plot plot/utils racket/draw colormaps/tol pict flomat)
5 |
6 | (define (mismatch q ecc q0 ecc0)
7 | (+ (sqr (- q q0)) (sqr (- ecc ecc0))))
8 | (define qs (for/list ([id (build-list 1000 values)]) (random)))
9 | (define eccs (for/list ([id (build-list 1000 values)]) (random)))
10 | (define mismatches (for/list ([ecc eccs]
11 | [q qs])
12 | (mismatch q ecc 0.5 0.5)))
13 |
14 | (define (color-map->list-of-colors cm)
15 | (parameterize ([plot-pen-color-map cm])
16 | (for/list ([c (in-range (color-map-size cm))])
17 | (match-define (list r g b) (->pen-color c))
18 | (make-object color% r g b))))
19 |
20 | (define (get-levels min-val max-val color-count)
21 | (let* ([intervals-count (sub1 color-count)]
22 | [step (/ (- max-val min-val) intervals-count)])
23 | (inclusive-range min-val (+ step max-val) step)))
24 |
25 | (define (get-color-index val min-val max-val color-count)
26 | (let* ([intervals (get-levels min-val max-val color-count)])
27 | (index-of intervals val >=)))
28 |
29 | (define color-list (color-map->list-of-colors 'tol-sd))
30 | (define (get-color val min-val max-val color-list)
31 | (let* ((color-count (length color-list))
32 | (color-index (get-color-index val min-val max-val (sub1 color-count))))
33 | (list-ref color-list color-index)))
34 |
35 | (define min-mm (apply min mismatches))
36 | (define max-mm (apply max mismatches))
37 |
38 | (define scatter (plot-pict
39 | (for/list ([ecc eccs]
40 | [q qs]
41 | [mm mismatches])
42 | (points (map vector (list ecc) (list q)) #:color (get-color mm min-mm max-mm color-list) #:sym 'fullcircle2))))
43 |
44 | (define (get-color-picts list-of-colors)
45 | (for/list ([c list-of-colors])
46 | (filled-rectangle 10 (/ (- (plot-height) 33) (length list-of-colors)) #:draw-border? #f #:color c)))
47 |
48 | (define (get-label-picts min-val max-val color-count)
49 | (for/list ([l (in-list (flatten (flomat->lists (linspace min-val max-val color-count))))]
50 | [idx (in-list (range 0 color-count))])
51 | (cc-superimpose
52 | (ghost (filled-rectangle 10 (/ (- (plot-height) 33) color-count))) (text (~a l #:width 4)))))
53 |
54 | (define label-picts (apply vl-append (get-label-picts min-mm max-mm (add1 (length color-list)))))
55 | (define color-picts (apply vl-append (get-color-picts color-list)))
56 | (define pad-below (ghost (rectangle 20 33)))
57 | (define colorbar (vl-append (hc-append 5 color-picts label-picts) pad-below))
58 |
59 | (define scatter+colorbar (hc-append 10 scatter colorbar))
60 | ```
61 |
--------------------------------------------------------------------------------
/examples/colorbar/colorbar.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 | (require plot plot/utils racket/draw colormaps/tol pict flomat)
3 |
4 | (define (mismatch q ecc q0 ecc0)
5 | (+ (sqr (- q q0)) (sqr (- ecc ecc0))))
6 | (define qs (for/list ([id (build-list 1000 values)]) (random)))
7 | (define eccs (for/list ([id (build-list 1000 values)]) (random)))
8 | (define mismatches (for/list ([ecc eccs]
9 | [q qs])
10 | (mismatch q ecc 0.5 0.5)))
11 |
12 | (define (color-map->list-of-colors cm)
13 | (parameterize ([plot-pen-color-map cm])
14 | (for/list ([c (in-range (color-map-size cm))])
15 | (match-define (list r g b) (->pen-color c))
16 | (make-object color% r g b))))
17 |
18 | (define (get-levels min-val max-val color-count)
19 | (let* ([intervals-count (sub1 color-count)]
20 | [step (/ (- max-val min-val) intervals-count)])
21 | (inclusive-range min-val (+ step max-val) step)))
22 |
23 | (define (get-color-index val min-val max-val color-count)
24 | (let* ([intervals (get-levels min-val max-val color-count)])
25 | (index-of intervals val >=)))
26 |
27 | (define color-list (color-map->list-of-colors 'tol-sd))
28 | (define (get-color val min-val max-val color-list)
29 | (let* ((color-count (length color-list))
30 | (color-index (get-color-index val min-val max-val (sub1 color-count))))
31 | (list-ref color-list color-index)))
32 |
33 | (define min-mm (apply min mismatches))
34 | (define max-mm (apply max mismatches))
35 |
36 | (define scatter (plot-pict
37 | (for/list ([ecc eccs]
38 | [q qs]
39 | [mm mismatches])
40 | (points (map vector (list ecc) (list q)) #:color (get-color mm min-mm max-mm color-list) #:sym 'fullcircle2))))
41 |
42 | (define (get-color-picts list-of-colors)
43 | (for/list ([c list-of-colors])
44 | (filled-rectangle 10 (/ (- (plot-height) 33) (length list-of-colors)) #:draw-border? #f #:color c)))
45 |
46 | (define (get-label-picts min-val max-val color-count)
47 | (for/list ([l (in-list (flatten (flomat->lists (linspace min-val max-val color-count))))]
48 | [idx (in-list (range 0 color-count))])
49 | (cc-superimpose
50 | (ghost (filled-rectangle 10 (/ (- (plot-height) 33) color-count))) (text (~a l #:width 4)))))
51 |
52 | (define label-picts (apply vl-append (get-label-picts min-mm max-mm (add1 (length color-list)))))
53 | (define color-picts (apply vl-append (get-color-picts color-list)))
54 | (define pad-below (ghost (rectangle 20 33)))
55 | (define colorbar (vl-append (hc-append 5 color-picts label-picts) pad-below))
56 |
57 | (define scatter+colorbar (hc-append 10 scatter colorbar))
58 |
--------------------------------------------------------------------------------
/examples/cosandderiv/README.md:
--------------------------------------------------------------------------------
1 | # cosandderiv
2 |
3 | ## Draw a graph of cos and deriv^3(cos)
4 |
5 | 
6 |
7 | ```scheme
8 | #lang racket ; draw a graph of cos
9 | (require plot/pict) ; and deriv^3(cos)
10 | (define ((deriv f) x)
11 | (/ (- (f x) (f (- x 0.001))) 0.001))
12 | (define (thrice f) (lambda (x) (f (f (f x)))))
13 | (plot (list (function ((thrice deriv) sin) -5 5)
14 | (function cos -5 5 #:color 'blue)))
15 |
16 | ```
17 |
18 | http://pasterack.org/pastes/97561
19 |
20 |
--------------------------------------------------------------------------------
/examples/cosandderiv/cosandderiv.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/cosandderiv/cosandderiv.png
--------------------------------------------------------------------------------
/examples/cosandderiv/cosandderiv.rkt:
--------------------------------------------------------------------------------
1 | #lang racket ; draw a graph of cos
2 | (require plot/pict) ; and deriv^3(cos)
3 | (define ((deriv f) x)
4 | (/ (- (f x) (f (- x 0.001))) 0.001))
5 | (define (thrice f) (lambda (x) (f (f (f x)))))
6 | (plot (list (function ((thrice deriv) sin) -5 5)
7 | (function cos -5 5 #:color 'blue)))
--------------------------------------------------------------------------------
/examples/f1/F1.txt:
--------------------------------------------------------------------------------
1 | 0. 1.393642849140016
2 | 0.5 1.352020631237723
3 | 1. 1.2766581012871085
4 | 1.5 1.196037644270239
5 | 2. 1.1172114886849127
6 | 2.5 1.0426909002423987
7 | 3. 0.973314609703763
8 | 3.5 0.9092132367912298
9 | 4. 0.8502032553249742
10 | 4.5 0.7959658952699017
11 | 5. 0.7461333883072874
12 | 5.5 0.7003314280604154
13 | 6. 0.6582000645749185
14 | 6.5 0.6194032402416759
15 | 7. 0.5836324849401127
16 | 7.5 0.5506074092112663
17 | 8. 0.5200747755660511
18 | 8.5 0.49180635968589737
19 | 9. 0.46559719642199265
20 | 9.5 0.44126326722433745
21 | 10. 0.41863907115181237
22 | 10.5 0.3975894480264212
23 | 11. 0.3780179946626937
24 | 11.5 0.3598326472814968
25 | 12. 0.34294134210369737
26 | 12.5 0.3272520153501624
27 | 13. 0.3126726032417589
28 | 13.5 0.2991110419993537
29 | 14. 0.2864752678438137
30 | 14.5 0.274673216996006
31 | 15. 0.26361282567679745
32 | 15.5 0.253202030107055
33 | 16. 0.2433487665076456
34 | 16.5 0.23396097109943614
35 | 17. 0.2249465801032936
36 | 17.5 0.21621352974008495
37 | 18. 0.20766975623067704
38 | 18.5 0.1992231957959369
39 | 19. 0.1907817846567314
40 | 19.5 0.18225345903392753
41 | 20. 0.1735461551483922
42 | 20.5 0.1664635907738567
43 | 21. 0.15976924952046545
44 | 21.5 0.15344890101609351
45 | 22. 0.14748831488861597
46 | 22.5 0.14187326076590784
47 | 23. 0.13658950827584418
48 | 23.5 0.13162282704630007
49 | 24. 0.12695898670515054
50 | 24.5 0.12258375688027068
51 | 25. 0.1184829071995355
52 | 25.5 0.11464220729082009
53 | 26. 0.1110474267819995
54 | 26.5 0.10768433530094876
55 | 27. 0.10453870247554295
56 | 27.5 0.10159629793365711
57 | 28. 0.09884289130316631
58 | 28.5 0.09626425221194561
59 | 29. 0.09384615028787004
60 | 29.5 0.09157435515881467
61 | 30. 0.08943463645265455
62 | 30.5 0.08670813408041035
63 | 31. 0.0840958433224033
64 | 31.5 0.08159412974210029
65 | 32. 0.07919935890296811
66 | 32.5 0.07690789636847364
67 | 33. 0.07471610770208376
68 | 33.5 0.07262035846726526
69 | 34. 0.07061701422748504
70 | 34.5 0.06870244054620991
71 | 35. 0.06687300298690677
72 | 35.5 0.06512506711304242
73 | 36. 0.06345499848808375
74 | 36.5 0.061859162675497584
75 | 37. 0.06033392523875079
76 | 37.5 0.0588756517413102
77 | 38. 0.057480707746642684
78 | 38.5 0.05614545881821508
79 | 39. 0.05486627051949425
80 | 39.5 0.053639508413947025
81 | 40. 0.052461538065040274
82 | 40.5 0.05116909273188936
83 | 41. 0.04992057076809234
84 | 41.5 0.04871473822289578
85 | 42. 0.04755036114554625
86 | 42.5 0.04642620558529033
87 | 43. 0.045341037591374575
88 | 43.5 0.04429362321304558
89 | 44. 0.0432827284995499
90 | 44.5 0.04230711950013412
91 | 45. 0.0413655622640448
92 | 45.5 0.04045682284052852
93 | 46. 0.039579667278831845
94 | 46.5 0.03873286162820135
95 | 47. 0.03791517193788361
96 | 47.5 0.03712536425712519
97 | 48. 0.036362204635172674
98 | 48.5 0.03562445912127262
99 | 49. 0.03491089376467161
100 | 49.5 0.03422027461461621
101 | 50. 0.03355136772035299
102 | 50.5 0.03285429510931988
103 | 51. 0.032177198341621846
104 | 51.5 0.03151957495555522
105 | 52. 0.03088092248941633
106 | 52.5 0.030260738481501497
107 | 53. 0.029658520470107055
108 | 53.5 0.029073765993529323
109 | 54. 0.028505972590064634
110 | 54.5 0.027954637798009305
111 | 55. 0.027419259155659667
112 | 55.5 0.026899334201312047
113 | 56. 0.02639436047326277
114 | 56.5 0.025903835509808164
115 | 57. 0.025427256849244552
116 | 57.5 0.024964122029868262
117 | 58. 0.024513928589975618
118 | 58.5 0.02407617406786295
119 | 59. 0.02365035600182658
120 | 59.5 0.023235971930162837
121 | 60. 0.022832519391168045
122 | 60.5 0.02242152709984197
123 | 61. 0.022020731625646623
124 | 61.5 0.02162990071474745
125 | 62. 0.021248802113309893
126 | 62.5 0.020877203567499406
127 | 63. 0.020514872823481432
128 | 63.5 0.02016157762742142
129 | 64. 0.01981708572548482
130 | 64.5 0.019481164863837074
131 | 65. 0.019153582788643632
132 | 65.5 0.018834107246069945
133 | 66. 0.018522505982281452
134 | 66.5 0.018218546743443607
135 | 67. 0.017921997275721857
136 | 67.5 0.017632625325281644
137 | 68. 0.01735019863828842
138 | 68.5 0.017074484960907632
139 | 69. 0.016805252039304726
140 | 69.5 0.01654226761964515
141 | 70. 0.016285299448094353
142 | 70.5 0.016026513747841253
143 | 71. 0.015773394096644015
144 | 71.5 0.015525822549284272
145 | 72. 0.015283681160543662
146 | 72.5 0.015046851985203818
147 | 73. 0.014815217078046375
148 | 73.5 0.014588658493852973
149 | 74. 0.014367058287405244
150 | 74.5 0.014150298513484824
151 | 75. 0.01393826122687335
152 | 75.5 0.013730828482352459
153 | 76. 0.013527882334703784
154 | 76.5 0.01332930483870896
155 | 77. 0.013134978049149624
156 | 77.5 0.012944784020807414
157 | 78. 0.012758604808463962
158 | 78.5 0.012576322466900904
159 | 79. 0.012397819050899879
160 | 79.5 0.012222976615242518
161 | 80. 0.012051677214710461
162 | 80.5 0.011880243533235052
163 | 81. 0.011712170520821907
164 | 81.5 0.011547393756626346
165 | 82. 0.011385848819803696
166 | 82.5 0.01122747128950928
167 | 83. 0.011072196744898424
168 | 83.5 0.010919960765126451
169 | 84. 0.010770698929348684
170 | 84.5 0.01062434681672045
171 | 85. 0.01048084000639707
172 | 85.5 0.010340114077533871
173 | 86. 0.010202104609286177
174 | 86.5 0.01006674718080931
175 | 87. 0.009933977371258595
176 | 87.5 0.009803730759789358
177 | 88. 0.009675942925556923
178 | 88.5 0.00955054944771661
179 | 89. 0.00942748590542375
180 | 89.5 0.009306687877833661
181 | 90. 0.009188090944101671
182 | 90.5 0.009069826873699866
183 | 91. 0.00895366218042445
184 | 91.5 0.008839559568388389
185 | 92. 0.008727481741704651
186 | 92.5 0.008617391404486203
187 | 93. 0.008509251260846012
188 | 93.5 0.008403024014897044
189 | 94. 0.008298672370752266
190 | 94.5 0.008196159032524646
191 | 95. 0.00809544670432715
192 | 95.5 0.007996498090272744
193 | 96. 0.007899275894474395
194 | 96.5 0.007803742821045072
195 | 97. 0.00770986157409774
196 | 97.5 0.007617594857745367
197 | 98. 0.007526905376100919
198 | 98.5 0.007437755833277363
199 | 99. 0.007350108933387666
200 | 99.5 0.007263927380544795
201 | 100. 0.007179173878861717
--------------------------------------------------------------------------------
/examples/f1/README.md:
--------------------------------------------------------------------------------
1 | # f1
2 |
3 | Description here
4 |
5 | 
6 |
7 |
8 | ```scheme
9 | #lang racket
10 | (require plot latex-pict) ; latex-pict is modeule to use latex
11 | (require simple-polynomial/private/poly-smooth) ; to make specific splines
12 | (require simple-polynomial/fit) ; to make simple splines
13 | (plot-new-window? #t) ; to show plot in separate window. #f is opposite
14 | (current-directory (current-directory)) ; the current directory is the directory of rkt file
15 | ;(current-directory "/path/to/directory") ; for specific directory
16 | (define F1 (call-with-input-file* "F1.txt"
17 | (λ (in)
18 | (for/list ([l (in-lines in)])
19 | (map string->number (string-split l)))))) ; Fi.txt contains two column of number separated by space - x y.
20 | ; F1 contains the same numbers but pared like (x y)
21 | ; to see F1 use (displayln F1)
22 | ;(define spF1 (points->spline-evaluator F1)) ; make smoothed data function for plot F1, simple spline
23 | (define spF1
24 | (points->spline-evaluator
25 | ((make-least-squares-point-smoother 7 4 0) F1))) ; make smoothed data function for plot F1, regulated form of spline
26 |
27 | (parameterize
28 | [(plot-y-ticks (linear-ticks #:number 2)); Number of ticks in y axis
29 | (plot-line-width 4) ; width of axes
30 | (plot-font-size 26) ; font size fpr plot
31 | (plot-font-family 'modern) ; all possibilies are here https://docs.racket-lang.org/draw/font_.html
32 | (plot-tick-size 14) ; size of ticks
33 | (plot-background-alpha 0) ; full transparancy is for 0
34 | (plot-x-far-axis? #t) ; #f - removes upper boundary of frame
35 | (plot-y-far-axis? #t) ; #f - removes right boundary of frame
36 | ]
37 | (plot
38 | (list ; use list for more than single function to plot
39 | (function spF1 0 100 #:color "Royal Blue" #:width 5) ; the first function to plot
40 | ; all colors names are here https://docs.racket-lang.org/draw/color-database___.html
41 | (point-label #(1 .1) "a)" #:point-size 0)) ; second figure to plot is label a) at point (1 .1)
42 | ; below are options to plot at whole
43 | #:title (tex-math "\\mathcal{E}^{\\mathrm{gg}}_2 /\\mathcal{E}_{\\mathrm{id}}" #:scale 5) ; title of figure
44 | ;; in the latex form. Use double backslash \\ instead of single one \
45 | ;; scale regulates size of fonts. \Large, \Huge does not work
46 | #:width 800 #:height 800 ; size of plot
47 | #:y-min 0 #:y-max 1.5 ; range for y axes
48 | #:out-file "f1.pdf" ; save file in pdf format
49 | ;#:out-file "f1.png" ; save file in png format
50 | #:y-label "" ; there is no label at y axes
51 | #:x-label (tex-math "a\\ \\mathrm{(nm)}" #:scale 5)) ; x label by using latex
52 | )
53 | ```
54 |
--------------------------------------------------------------------------------
/examples/f1/f1.md:
--------------------------------------------------------------------------------
1 | # F1
2 |
3 | description here
4 |
5 |
6 | Image `![]()`
7 |
8 | link `[]()`
9 |
--------------------------------------------------------------------------------
/examples/f1/f1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/f1/f1.pdf
--------------------------------------------------------------------------------
/examples/f1/f1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/f1/f1.png
--------------------------------------------------------------------------------
/examples/f1/plots-splines.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 | (require plot latex-pict) ; latex-pict is modeule to use latex
3 | (require simple-polynomial/private/poly-smooth) ; to make specific splines
4 | (require simple-polynomial/fit) ; to make simple splines
5 | (plot-new-window? #t) ; to show plot in separate window. #f is opposite
6 | (current-directory (current-directory)) ; the current directory is the directory of rkt file
7 | ;(current-directory "/path/to/directory") ; for specific directory
8 | (define F1 (call-with-input-file* "F1.txt"
9 | (λ (in)
10 | (for/list ([l (in-lines in)])
11 | (map string->number (string-split l)))))) ; Fi.txt contains two column of number separated by space - x y.
12 | ; F1 contains the same numbers but pared like (x y)
13 | ; to see F1 use (displayln F1)
14 | ;(define spF1 (points->spline-evaluator F1)) ; make smoothed data function for plot F1, simple spline
15 | (define spF1
16 | (points->spline-evaluator
17 | ((make-least-squares-point-smoother 7 4 0) F1))) ; make smoothed data function for plot F1, regulated form of spline
18 |
19 | (parameterize
20 | [(plot-y-ticks (linear-ticks #:number 2)); Number of ticks in y axis
21 | (plot-line-width 4) ; width of axes
22 | (plot-font-size 26) ; font size fpr plot
23 | (plot-font-family 'modern) ; all possibilies are here https://docs.racket-lang.org/draw/font_.html
24 | (plot-tick-size 14) ; size of ticks
25 | (plot-background-alpha 0) ; full transparancy is for 0
26 | (plot-x-far-axis? #t) ; #f - removes upper boundary of frame
27 | (plot-y-far-axis? #t) ; #f - removes right boundary of frame
28 | ]
29 | (plot
30 | (list ; use list for more than single function to plot
31 | (function spF1 0 100 #:color "Royal Blue" #:width 5) ; the first function to plot
32 | ; all colors names are here https://docs.racket-lang.org/draw/color-database___.html
33 | (point-label #(1 .1) "a)" #:point-size 0)) ; second figure to plot is label a) at point (1 .1)
34 | ; below are options to plot at whole
35 | #:title (tex-math "\\mathcal{E}^{\\mathrm{gg}}_2 /\\mathcal{E}_{\\mathrm{id}}" #:scale 5) ; title of figure
36 | ;; in the latex form. Use double backslash \\ instead of single one \
37 | ;; scale regulates size of fonts. \Large, \Huge does not work
38 | #:width 800 #:height 800 ; size of plot
39 | #:y-min 0 #:y-max 1.5 ; range for y axes
40 | #:out-file "f1.pdf" ; save file in pdf format
41 | ;#:out-file "f1.png" ; save file in png format
42 | #:y-label "" ; there is no label at y axes
43 | #:x-label (tex-math "a\\ \\mathrm{(nm)}" #:scale 5)) ; x label by using latex
44 | )
45 |
--------------------------------------------------------------------------------
/examples/f2/F2.txt:
--------------------------------------------------------------------------------
1 | 100 0.007179173878861717
2 | 101 0.007011868253165967
3 | 102 0.006849944468874738
4 | 103 0.0066932213463015144
5 | 104 0.006541517705759778
6 | 105 0.006394652367563013
7 | 106 0.006252444152024701
8 | 107 0.006114711879458326
9 | 108 0.0059812743701773705
10 | 109 0.005851950444495318
11 | 110 0.005726558922725651
12 | 111 0.00560381830948525
13 | 112 0.0054847144265839955
14 | 113 0.005369132780135163
15 | 114 0.00525695887625203
16 | 115 0.005148078221047873
17 | 116 0.00504237632063597
18 | 117 0.004939738681129599
19 | 118 0.004840050808642035
20 | 119 0.004743198209286555
21 | 120 0.004649066389176437
22 | 121 0.004556886628309382
23 | 122 0.004467238308981854
24 | 123 0.00438004658737474
25 | 124 0.004295236619668928
26 | 125 0.004212733562045305
27 | 126 0.004132462570684759
28 | 127 0.004054348801768177
29 | 128 0.003978317411476449
30 | 129 0.003904293555990459
31 | 130 0.0038322023914910976
32 | 131 0.0037615652634377687
33 | 132 0.003692735612109902
34 | 133 0.003625663067065445
35 | 134 0.0035602972578623452
36 | 135 0.003496587814058549
37 | 136 0.0034344843652120045
38 | 137 0.003373936540880658
39 | 138 0.0033148939706224576
40 | 139 0.0032573062839953495
41 | 140 0.003201123110557282
42 | 141 0.003146036672254688
43 | 142 0.003092269606718333
44 | 143 0.0030397871439674672
45 | 144 0.0029885545140213416
46 | 145 0.0029385369468992073
47 | 146 0.0028896996726203153
48 | 147 0.0028420079212039167
49 | 148 0.002795426922669262
50 | 149 0.002749921907035603
51 | 150 0.00270545810432219
52 | 151 0.0026618319650035123
53 | 152 0.002619187727706902
54 | 153 0.0025775008515149284
55 | 154 0.0025367467955101624
56 | 155 0.0024969010187751734
57 | 156 0.0024579389803925313
58 | 157 0.0024198361394448063
59 | 158 0.0023825679550145687
60 | 159 0.0023461098861843875
61 | 160 0.0023104373920368334
62 | 161 0.0022754124999318052
63 | 162 0.002241130975324403
64 | 163 0.002207575151947055
65 | 164 0.0021747273635321905
66 | 165 0.0021425699438122375
67 | 166 0.0021110852265196255
68 | 167 0.0020802555453867826
69 | 168 0.002050063234146138
70 | 169 0.002020490626530121
71 | 170 0.0019915200562711597
72 | 171 0.0019630559247837457
73 | 172 0.001935163221289029
74 | 173 0.0019078290026902226
75 | 174 0.0018810403258905394
76 | 175 0.0018547842477931924
77 | 176 0.0018290478253013942
78 | 177 0.0018038181153183577
79 | 178 0.0017790821747472958
80 | 179 0.0017548270604914216
81 | 180 0.0017310398294539476
82 | 181 0.0017076529324711208
83 | 182 0.001684711341971724
84 | 183 0.001662205424317574
85 | 184 0.001640125545870488
86 | 185 0.0016184620729922823
87 | 186 0.0015972053720447743
88 | 187 0.0015763458093897807
89 | 188 0.0015558737513891182
90 | 189 0.0015357795644046037
91 | 190 0.001516053614798054
92 | 191 0.001496647326628443
93 | 192 0.001477592368699997
94 | 193 0.0014588814675140988
95 | 194 0.0014405073495721318
96 | 195 0.0014224627413754788
97 | 196 0.0014047403694255232
98 | 197 0.001387332960223648
99 | 198 0.0013702332402712365
100 | 199 0.0013534339360696715
101 | 200 0.001336927774120336
102 | 201 0.0013206792589890968
103 | 202 0.001304711049533188
104 | 203 0.0012890175826743267
105 | 204 0.0012735932953342308
106 | 205 0.0012584326244346174
107 | 206 0.0012435300068972042
108 | 207 0.0012288798796437088
109 | 208 0.0012144766795958483
110 | 209 0.0012003148436753404
111 | 210 0.0011863888088039026
112 | 211 0.00117267226313625
113 | 212 0.0011591816498621329
114 | 213 0.001145912663404299
115 | 214 0.0011328609981854964
116 | 215 0.0011200223486284725
117 | 216 0.0011073924091559756
118 | 217 0.0010949668741907536
119 | 218 0.0010827414381555543
120 | 219 0.0010707117954731253
121 | 220 0.001058873640566215
122 | 221 0.0010472072067297894
123 | 222 0.0010357245865524256
124 | 223 0.0010244224114949185
125 | 224 0.0010132973130180635
126 | 225 0.0010023459225826559
127 | 226 0.0009915648716494908
128 | 227 0.0009809507916793635
129 | 228 0.0009705003141330695
130 | 229 0.0009602100704714037
131 | 230 0.0009500766921551615
132 | 231 0.0009400851489207049
133 | 232 0.0009302444407244401
134 | 233 0.0009205519057983402
135 | 234 0.0009110048823743783
136 | 235 0.0009016007086845273
137 | 236 0.0008923367229607602
138 | 237 0.0008832102634350503
139 | 238 0.0008742186683393703
140 | 239 0.0008653592759056934
141 | 240 0.0008566294243659926
142 | 241 0.0008480175579355467
143 | 242 0.0008395304478943377
144 | 243 0.0008311659715056535
145 | 244 0.0008229220060327819
146 | 245 0.0008147964287390108
147 | 246 0.0008067871168876278
148 | 247 0.0007988919477419209
149 | 248 0.000791108798565178
150 | 249 0.0007834355466206867
151 | 250 0.000775870069171735
152 | 251 0.0007684033891906631
153 | 252 0.000761040653643279
154 | 253 0.0007537801552044431
155 | 254 0.0007466201865490156
156 | 255 0.0007395590403518573
157 | 256 0.0007325950092878283
158 | 257 0.0007257263860317891
159 | 258 0.0007189514632586001
160 | 259 0.0007122685336431217
161 | 260 0.0007056758898602143
162 | 261 0.0006991664909987216
163 | 262 0.0006927442865671582
164 | 263 0.0006864078924880216
165 | 264 0.0006801559246838099
166 | 265 0.0006739869990770207
167 | 266 0.0006678997315901516
168 | 267 0.0006618927381457008
169 | 268 0.0006559646346661659
170 | 269 0.0006501140370740444
171 | 270 0.0006443395612918345
172 | 271 0.0006386356359569811
173 | 272 0.0006330053180518863
174 | 273 0.0006274474772738997
175 | 274 0.0006219609833203706
176 | 275 0.0006165447058886487
177 | 276 0.000611197514676083
178 | 277 0.0006059182793800232
179 | 278 0.0006007058696978189
180 | 279 0.0005955591553268193
181 | 280 0.000590477005964374
182 | 281 0.0005854549774619295
183 | 282 0.000580495454201884
184 | 283 0.0005755975067207322
185 | 284 0.0005707602055549693
186 | 285 0.0005659826212410905
187 | 286 0.0005612638243155908
188 | 287 0.0005566028853149653
189 | 288 0.0005519988747757091
190 | 289 0.0005474508632343171
191 | 290 0.0005429579212272846
192 | 291 0.0005385164742197773
193 | 292 0.0005341283981269729
194 | 293 0.0005297929237927197
195 | 294 0.000525509282060866
196 | 295 0.0005212767037752604
197 | 296 0.0005170944197797511
198 | 297 0.0005129616609181864
199 | 298 0.0005088776580344149
200 | 299 0.0005048416419722848
201 | 300 0.0005008528435756444
202 | 301 0.000496908368854574
203 | 302 0.000493009702264494
204 | 303 0.0004891562034270573
205 | 304 0.0004853472319639164
206 | 305 0.00048158214749672364
207 | 306 0.00047786030964713154
208 | 307 0.00047418107803679275
209 | 308 0.00047054381228735963
210 | 309 0.0004669478720204848
211 | 310 0.0004633926168578207
212 | 311 0.00045987568832423653
213 | 312 0.00045639826826524596
214 | 313 0.0004529598204295792
215 | 314 0.00044955980856596655
216 | 315 0.00044619769642313835
217 | 316 0.0004428729477498249
218 | 317 0.0004395850262947564
219 | 318 0.00043633339580666317
220 | 319 0.0004331175200342755
221 | 320 0.00042993686272632377
222 | 321 0.0004267894866478609
223 | 322 0.0004236763414393962
224 | 323 0.0004205969757577614
225 | 324 0.00041755093825978864
226 | 325 0.0004145377776023098
227 | 326 0.0004115570424421568
228 | 327 0.0004086082814361615
229 | 328 0.000405691043241156
230 | 329 0.0004028048765139722
231 | 330 0.0003999493299114419
232 | 331 0.0003971228057647097
233 | 332 0.0003943260685305791
234 | 333 0.0003915587363401661
235 | 334 0.0003888204273245867
236 | 335 0.00038611075961495693
237 | 336 0.0003834293513423928
238 | 337 0.0003807758206380104
239 | 338 0.0003781497856329257
240 | 339 0.00037555086445825476
241 | 340 0.0003729786752451136
242 | 341 0.0003704318912474917
243 | 342 0.00036791113273891205
244 | 343 0.00036541607511577105
245 | 344 0.0003629463937744651
246 | 345 0.0003605017641113906
247 | 346 0.000358081861522944
248 | 347 0.0003556863614055218
249 | 348 0.0003533149391555203
250 | 349 0.00035096727016933595
251 | 350 0.00034864302984336524
252 | 351 0.0003463411108633024
253 | 352 0.0003440620187732582
254 | 353 0.00034180547640664133
255 | 354 0.00033957120659686054
256 | 355 0.0003373589321773245
257 | 356 0.0003351683759814417
258 | 357 0.00033299926084262105
259 | 358 0.0003308513095942711
260 | 359 0.00032872424506980057
261 | 360 0.00032661779010261806
262 | 361 0.0003245310158982723
263 | 362 0.00032246433641062956
264 | 363 0.00032041751396569604
265 | 364 0.000318390310889478
266 | 365 0.0003163824895079817
267 | 366 0.00031439381214721336
268 | 367 0.0003124240411331792
269 | 368 0.00031047293879188554
270 | 369 0.0003085402674493386
271 | 370 0.00030662578943154464
272 | 371 0.0003047287221822578
273 | 372 0.0003028494059329032
274 | 373 0.0003009876360326538
275 | 374 0.0002991432078306828
276 | 375 0.0002973159166761632
277 | 376 0.000295505557918268
278 | 377 0.00029371192690617025
279 | 378 0.00029193481898904297
280 | 379 0.0002901740295160592
281 | 380 0.0002884293538363921
282 | 381 0.0002867001288401353
283 | 382 0.0002849866361209399
284 | 383 0.00028328869881337774
285 | 384 0.0002816061400520205
286 | 385 0.00027993878297144
287 | 386 0.00027828645070620807
288 | 387 0.00027664896639089645
289 | 388 0.0002750261531600769
290 | 389 0.0002734178341483213
291 | 390 0.0002718238324902013
292 | 391 0.0002702435847813963
293 | 392 0.00026867732412197003
294 | 393 0.00026712489707309385
295 | 394 0.0002655861501959392
296 | 395 0.00026406093005167726
297 | 396 0.00026254908320147945
298 | 397 0.00026105045620651704
299 | 398 0.00025956489562796144
300 | 399 0.0002580922480269839
301 | 400 0.0002566323599647558
302 | 401 0.0002551847498028286
303 | 402 0.00025374961219287956
304 | 403 0.00025232681358696606
305 | 404 0.00025091622043714543
306 | 405 0.00024951769919547505
307 | 406 0.00024813111631401236
308 | 407 0.0002467563382448147
309 | 408 0.0002453932314399394
310 | 409 0.00024404166235144395
311 | 410 0.00024270149743138567
312 | 411 0.00024137232428381893
313 | 412 0.0002400543051086831
314 | 413 0.0002387473232579145
315 | 414 0.00023745126208344952
316 | 415 0.0002361660049372245
317 | 416 0.00023489143517117577
318 | 417 0.0002336274361372397
319 | 418 0.00023237389118735259
320 | 419 0.00023113068367345085
321 | 420 0.0002298976969474708
322 | 421 0.00022867457770401134
323 | 422 0.0002274614602952152
324 | 423 0.0002262582424158877
325 | 424 0.0002250648217608341
326 | 425 0.00022388109602485966
327 | 426 0.00022270696290276973
328 | 427 0.00022154232008936953
329 | 428 0.0002203870652794644
330 | 429 0.0002192410961678596
331 | 430 0.00021810431044936042
332 | 431 0.000216976398338605
333 | 432 0.0002158574775851214
334 | 433 0.00021474745845827042
335 | 434 0.00021364625122741296
336 | 435 0.00021255376616190986
337 | 436 0.00021146991353112202
338 | 437 0.0002103946036044103
339 | 438 0.00020932774665113557
340 | 439 0.0002082692529406587
341 | 440 0.00020721903274234055
342 | 441 0.00020617682287367756
343 | 442 0.00020514271756812925
344 | 443 0.00020411663760729068
345 | 444 0.00020309850377275693
346 | 445 0.0002020882368461231
347 | 446 0.00020108575760898425
348 | 447 0.0002000909868429354
349 | 448 0.00019910384532957173
350 | 449 0.00019812425385048823
351 | 450 0.00019715213318728002
352 | 451 0.00019618725179790743
353 | 452 0.0001952296920193357
354 | 453 0.00019427938386489538
355 | 454 0.00019333625734791696
356 | 455 0.00019240024248173094
357 | 456 0.00019147126927966784
358 | 457 0.0001905492677550582
359 | 458 0.00018963416792123252
360 | 459 0.0001887258997915213
361 | 460 0.00018782439337925507
362 | 461 0.00018692944714876976
363 | 462 0.00018604113063505683
364 | 463 0.0001851593818241131
365 | 464 0.00018428413870193543
366 | 465 0.00018341533925452067
367 | 466 0.00018255292146786568
368 | 467 0.0001816968233279673
369 | 468 0.0001808469828208224
370 | 469 0.00018000333793242779
371 | 470 0.00017916582664878036
372 | 471 0.00017833427310512444
373 | 472 0.000177508736038255
374 | 473 0.00017668916033421452
375 | 474 0.00017587549087904543
376 | 475 0.0001750676725587902
377 | 476 0.0001742656502594913
378 | 477 0.00017346936886719117
379 | 478 0.0001726787732679323
380 | 479 0.00017189380834775711
381 | 480 0.0001711144189927081
382 | 481 0.00017034045072149642
383 | 482 0.00016957195380975831
384 | 483 0.00016880887916579876
385 | 484 0.0001680511776979227
386 | 485 0.00016729880031443514
387 | 486 0.000166551697923641
388 | 487 0.00016580982143384523
389 | 488 0.00016507312175335285
390 | 489 0.00016434154979046877
391 | 490 0.000163615056453498
392 | 491 0.00016289350569318606
393 | 492 0.00016217694064555246
394 | 493 0.00016146531748905725
395 | 494 0.0001607585924021605
396 | 495 0.00016005672156332233
397 | 496 0.0001593596611510028
398 | 497 0.00015866736734366198
399 | 498 0.00015797979631975996
400 | 499 0.0001572969042577568
401 | 500 0.00015661864733611258
--------------------------------------------------------------------------------
/examples/f2/README.md:
--------------------------------------------------------------------------------
1 | # f2
2 |
3 | Description here
4 |
5 | 
6 |
7 |
8 | ```scheme
9 | #lang racket
10 | (require plot latex-pict pict) ; to use latex
11 | (require (prefix-in jp: metapict)) ; to save in different formats
12 | ;(require metapict/save-pdf) ; to save plot as pdf by using save-pict-as-pdf
13 | ;(require metapict/save-svg) ; to save plot as svg by using save-pict-as-svg
14 | (require simple-polynomial/private/poly-smooth) ; to make specific splines
15 | (require simple-polynomial/fit) ; to make simple splines
16 | (current-directory (current-directory)) ; the current directory is the directory of rkt file
17 | ;(current-directory "/path/to/directory") ; for specific directory
18 | (define F2 (call-with-input-file* "F2.txt"
19 | (λ (in)
20 | (for/list ([l (in-lines in)])
21 | (map string->number (string-split l)))))) ; F2.txt contains two column of number separated by space - x y.
22 | ; F2 contains the same numbers but pared like (x y)
23 | ; to see F2 use (displayln F1)
24 | ;(define spF2 (points->spline-evaluator F2)) ; make smoothed data function for plot F2, simple spline
25 | (define spF2
26 | (points->spline-evaluator
27 | ((make-least-squares-point-smoother 7 4 0) F2))); make smoothed data function for plot F2, regulated form of spline
28 | (define nspF2 (lambda (x) (* (spF2 x) 1e3))) ; the scale of numbers is 10^{-3}. One multiplies on 10^3 and put information
29 | ; about this in upper of figure, outside of it: 10^{-3}
30 | ;; fig is the main figure. Remember that nspF2 = 10^3 spF2
31 | (define fig
32 | (parameterize
33 | [(plot-y-ticks (linear-ticks #:number 4)); Number of ticks in y axis
34 | (plot-line-width 4) ; width of axes
35 | (plot-font-size 26) ; font size for plot
36 | (plot-font-family 'modern) ; all possibilies are here https://docs.racket-lang.org/draw/font_.html
37 | (plot-tick-size 14) ; size of ticks
38 | (plot-background-alpha 0) ; full transparancy is for 0
39 | (plot-x-far-axis? #t) ; #f - removes upper boundary of frame
40 | (plot-y-far-axis? #t) ; #f - removes right boundary of frame
41 | ]
42 | (plot-pict
43 | (list ; use list for more than single function to plot
44 | (function nspF2 100 500 #:color "Royal Blue" #:width 5); the first function to plot
45 | ; all colors names are here https://docs.racket-lang.org/draw/color-database___.html
46 | (point-label #(100 .5) "b)" #:point-size 0) ; second figure to plot is label b) at point (100 .5)
47 | )
48 | ; below are options to plot at whole
49 | #:title (tex-math "\\mathcal{E}^{\\mathrm{gg}}_2 /\\mathcal{E}_{\\mathrm{id}}" #:scale 5); title of figure
50 | ;; in the latex form. Use double backslash \\ instead of single one \
51 | ;; scale regulates size of fonts. \Large, \Huge does not work
52 | #:width 800 #:height 800 ; size of plot
53 | #:y-min 0 #:y-max 7 ; range for y axes
54 | #:y-label "" ; there is no label at y axes
55 | #:x-label (tex-math "a\\ (nm)" #:scale 5))) ; x label by using latex
56 | )
57 | (define lab (tex-math "\\times 10^{-3}" #:scale 4)) ; lab is the text with scale factor which will put outside
58 | (define res (lt-superimpose fig (inset lab 60 30))) ; res is combination of fig and lab. position is (60 30)
59 | (pict->bitmap res) ; to show figure, pict->bitmap needs for correct latex representation
60 | ;(save-pict-as-pdf res "f2.pdf") ; to save plot as pdf with metapict/save-pdf
61 | ;(save-pict-as-svg res "f2.svg") ; to save plot as svg with metapict/save-svg
62 | (jp:save-pict "f2.png" res 'png) ; to save plot in png, svg, pdf, xbm, xpm and bmp.
63 | ```
64 |
--------------------------------------------------------------------------------
/examples/f2/f2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/f2/f2.pdf
--------------------------------------------------------------------------------
/examples/f2/f2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/f2/f2.png
--------------------------------------------------------------------------------
/examples/f2/plots-splines-text-outside.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 | (require plot latex-pict pict) ; to use latex
3 | (require (prefix-in jp: metapict)) ; to save in different formats
4 | ;(require metapict/save-pdf) ; to save plot as pdf by using save-pict-as-pdf
5 | ;(require metapict/save-svg) ; to save plot as svg by using save-pict-as-svg
6 | (require simple-polynomial/private/poly-smooth) ; to make specific splines
7 | (require simple-polynomial/fit) ; to make simple splines
8 | (current-directory (current-directory)) ; the current directory is the directory of rkt file
9 | ;(current-directory "/path/to/directory") ; for specific directory
10 | (define F2 (call-with-input-file* "F2.txt"
11 | (λ (in)
12 | (for/list ([l (in-lines in)])
13 | (map string->number (string-split l)))))) ; F2.txt contains two column of number separated by space - x y.
14 | ; F2 contains the same numbers but pared like (x y)
15 | ; to see F2 use (displayln F1)
16 | ;(define spF2 (points->spline-evaluator F2)) ; make smoothed data function for plot F2, simple spline
17 | (define spF2
18 | (points->spline-evaluator
19 | ((make-least-squares-point-smoother 7 4 0) F2))); make smoothed data function for plot F2, regulated form of spline
20 | (define nspF2 (lambda (x) (* (spF2 x) 1e3))) ; the scale of numbers is 10^{-3}. One multiplies on 10^3 and put information
21 | ; about this in upper of figure, outside of it: 10^{-3}
22 | ;; fig is the main figure. Remember that nspF2 = 10^3 spF2
23 | (define fig
24 | (parameterize
25 | [(plot-y-ticks (linear-ticks #:number 4)); Number of ticks in y axis
26 | (plot-line-width 4) ; width of axes
27 | (plot-font-size 26) ; font size for plot
28 | (plot-font-family 'modern) ; all possibilies are here https://docs.racket-lang.org/draw/font_.html
29 | (plot-tick-size 14) ; size of ticks
30 | (plot-background-alpha 0) ; full transparancy is for 0
31 | (plot-x-far-axis? #t) ; #f - removes upper boundary of frame
32 | (plot-y-far-axis? #t) ; #f - removes right boundary of frame
33 | ]
34 | (plot-pict
35 | (list ; use list for more than single function to plot
36 | (function nspF2 100 500 #:color "Royal Blue" #:width 5); the first function to plot
37 | ; all colors names are here https://docs.racket-lang.org/draw/color-database___.html
38 | (point-label #(100 .5) "b)" #:point-size 0) ; second figure to plot is label b) at point (100 .5)
39 | )
40 | ; below are options to plot at whole
41 | #:title (tex-math "\\mathcal{E}^{\\mathrm{gg}}_2 /\\mathcal{E}_{\\mathrm{id}}" #:scale 5); title of figure
42 | ;; in the latex form. Use double backslash \\ instead of single one \
43 | ;; scale regulates size of fonts. \Large, \Huge does not work
44 | #:width 800 #:height 800 ; size of plot
45 | #:y-min 0 #:y-max 7 ; range for y axes
46 | #:y-label "" ; there is no label at y axes
47 | #:x-label (tex-math "a\\ (nm)" #:scale 5))) ; x label by using latex
48 | )
49 | (define lab (tex-math "\\times 10^{-3}" #:scale 4)) ; lab is the text with scale factor which will put outside
50 | (define res (lt-superimpose fig (inset lab 60 30))) ; res is combination of fig and lab. position is (60 30)
51 | (pict->bitmap res) ; to show figure, pict->bitmap needs for correct latex representation
52 | ;(save-pict-as-pdf res "f2.pdf") ; to save plot as pdf with metapict/save-pdf
53 | ;(save-pict-as-svg res "f2.svg") ; to save plot as svg with metapict/save-svg
54 | (jp:save-pict "f2.png" res 'png) ; to save plot in png, svg, pdf, xbm, xpm and bmp.
55 |
--------------------------------------------------------------------------------
/examples/face/59ECFEE5-B9BF-40BA-A2DA-BC3E0655F5C3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/face/59ECFEE5-B9BF-40BA-A2DA-BC3E0655F5C3.png
--------------------------------------------------------------------------------
/examples/face/README.md:
--------------------------------------------------------------------------------
1 | # Face
2 |
3 |
4 | 
5 |
6 | ```scheme
7 | #lang racket/base
8 |
9 | (require plot
10 | racket/match
11 | racket/file
12 | math)
13 |
14 | (module+ test (require rackunit))
15 | (define (^ b [e 2])(expt b e))
16 |
17 | ;--------------------------------------------
18 | ;ribbon coordinates
19 | ;--------------------------------------------
20 | (define R 100)
21 | (define P 90)
22 | (define π (angle -1))
23 | (define 2π (* 2 π))
24 |
25 | (define (xyz->ribbon x y z)
26 | (define ϕ0 (atan y x))
27 | (define n0 (/ (- z (* P (/ ϕ0 2π))) P))
28 | (define n (round n0))
29 | (define ϕ (+ ϕ0 (* n 2π)))
30 | (define t (- z (* P (/ ϕ 2π))))
31 | (define d (- (sqrt (+ (* x x) (* y y))) R))
32 | ;(println (list ϕ0 n0 n ϕ t d))
33 | (list ϕ t d))
34 | (module+ test
35 | (check-within (xyz->ribbon 100 0 0) '(0 0 0) 1e-15)
36 | (check-within (xyz->ribbon 90 0 0) '(0 0 -10) 1e-15)
37 | (check-within (xyz->ribbon 110 0 0) '(0 0 10) 1e-15)
38 |
39 | (check-within (xyz->ribbon 100 0 4) '(0 4 0) 1e-15)
40 | (check-within (xyz->ribbon 100 0 -4) '(0 -4 0) 1e-15)
41 | (check-within (xyz->ribbon 100 0 (/ P 3)) (list 0 (/ P 3) 0) 1e-15)
42 | (check-within (xyz->ribbon 100 0 (/ P -3)) (list 0 (/ P -3) 0) 1e-15)
43 |
44 | (check-within (xyz->ribbon -100 0 (/ P 2)) (list π 0 0) 1e-15)
45 | (check-within (xyz->ribbon -100 0 (+ (/ P 2)(/ P 3))) (list π (/ P 3) 0) 1e-14)
46 | (check-within (xyz->ribbon -100 0 (+ (/ P 2)(/ P -3))) (list π (/ P -3) 0) 1e-14)
47 |
48 | (check-within (xyz->ribbon 100 0 P) (list 2π 0 0) 1e-15)
49 | (check-within (xyz->ribbon 0 100 (/ P 4)) (list (/ π 2) 0 0) 1e-15)
50 | (check-within (xyz->ribbon 0 90 (/ P 4)) (list (/ π 2) 0 -10) 1e-15)
51 | (check-within (xyz->ribbon 100 100 (* P (+ 2 1/8))) (list (+ (* (+ 2 1/8) 2π)) 0 (* 100 (- (sqrt 2) 1))) 1e-13)
52 | )
53 |
54 | (define (ribbon->xyz ϕ t d)
55 | (list (* (+ R d) (cos ϕ))
56 | (* (+ R d) (sin ϕ))
57 | (+ (* P (/ ϕ 2π)) t)))
58 | (module+ test
59 | (let ([x (* (random) 100)]
60 | [y (* (random) 100)]
61 | [z (* (random) 100)])
62 | (check-within (apply ribbon->xyz (xyz->ribbon x y z)) (list x y z) 1e-10)))
63 |
64 | ;--------------------------------------------
65 | ;egg-face (in cilinder coordinates)
66 | ;--------------------------------------------
67 | (define (skull θ z)
68 | (define Z0 10)(define Z1 290)
69 | (define C0 90)(define C1 -10)
70 | (define C (+ (* (/ (- C0 C1)(expt (- Z1 Z0) 2))(expt (- Z1 z) 2)) C1))
71 | (define A/B0 .2)
72 | (define A/B (+ 1 (* A/B0 (/ (- Z0 z)(- Z0 Z1)))))
73 | (define R0 130)
74 | (define R (* R0 (sqrt (/ (* 4 (- z Z0)(- Z1 z)) (^ (- Z1 Z0))))))
75 |
76 | (define a (+ (^ (cos θ))(^ (* A/B (sin θ)))))
77 | (define b (* -2 C (cos θ)))
78 | (define c (- (^ C)(^ (* R A/B))))
79 | (define D (sqrt (+ (^ b) (* -4 a c))))
80 |
81 | (and (<= Z0 z Z1)
82 | (real? D)
83 | (max (/ (- (- b) D) 2 a)(/ (+ (- b) D) 2 a))))
84 | (define (neck θ z)
85 | (define Z0 0)(define Z1 90)
86 | (define R0 80)(define R1 70)
87 | (and (<= Z0 z Z1)
88 | (- R0 (* (- R0 R1)(* 4 (/ (- z Z0) (- Z1 Z0)) (- 1 (/ (- z Z0) (- Z1 Z0))))))))
89 | (define (nose θ z)
90 | (define Z0 120)(define Z1 200)(define Z2 480)
91 | (define θ0 .15)
92 | (define R0 140)(define R1 180)
93 | (and (<= Z0 z Z1)
94 | (<= (- θ0) θ (+ θ0))
95 | (* (- 1 (/ (- z Z0)
96 | (- Z2 Z0)))
97 | (+ R0 (* (- R1 R0)
98 | (let ([θ* (/ (+ θ θ0) 2 θ0)])(* 4 θ* (- 1 θ*))))))))
99 | (define (lips θ z)
100 | (define Z0 70)(define Z1 110)
101 | (define θ0 .4)
102 | (define R0 150)(define R1 170)
103 | (define Z/2 (/ (+ Z0 Z1) 2))
104 | (and (<= Z0 z Z1)
105 | (<= (- θ0) θ (+ θ0))
106 | (+ R0 (* (- R1 R0)
107 | (if (< z Z/2)
108 | (let ([z* (/ (- z Z0)(- Z/2 Z0))])( * -2.25 z* (- z* 4/3)))
109 | (let ([z* (/ (- z Z/2)(- Z1 Z/2))])(* -2.25 (+ z* 1/3) (- z* 1))))
110 | (let ([θ* (/ (+ θ θ0) 2 θ0)])(* 4 θ* (- 1 θ*)))))))
111 | (define (eyes θ z)
112 | (define Z0 170)(define Z1 190)
113 | (define θ0 .15)(define θ1 .35)
114 | (define R0 140)(define R1 150)
115 | (and (<= Z0 z Z1)
116 | (<= θ0 (abs θ) θ1)
117 | (+ R0 (* (- R1 R0)
118 | (let ([z* (/ (- z Z0)(- Z1 Z0))])(* 4 z* (- 1 z*)))
119 | (let ([θ* (/ (- (abs θ) θ0)(- θ1 θ0))])(* 4 θ* (- 1 θ*)))))))
120 | (define (eyebrow θ z)
121 | (define Z0 195)(define Z1 205)
122 | (define θ0 .55)
123 | (define R0 130)(define R1 145)
124 | (and (<= Z0 z Z1)
125 | (<= (- θ0) θ (+ θ0))
126 | (+ R0 (* (- R1 R0)
127 | (let ([z* (/ (- z Z0)(- Z1 Z0))])(* 4 z* (- 1 z*)))
128 | (let ([θ* (/ (+ θ θ0) 2 θ0)])(* 4 θ* (- 1 θ*)))))))
129 | (define (ears1 θ z)
130 | (define Z0 110)(define Z1 200)
131 | (define θ0 (* .35 (sqrt (/ (* 4 (- z Z0)(- Z1 z)) (^ (- Z1 Z0))))))
132 | (define R0 140)
133 | (and (<= Z0 z Z1)
134 | (<= (- (/ π 2) θ0) (abs θ) (+ (/ π 2) θ0))
135 | R0))
136 | (define (ears2 θ z)
137 | (define Z0 190)(define Z1 295)
138 | (define θ0 .1)
139 | (and (<= Z0 z Z1)
140 | (<= (- (/ π 2) θ0) (abs θ) (+ (/ π 2) θ0))
141 | (+ (or (skull θ z) 0) 10)))
142 | ;combine
143 | (define (face θ z)
144 | (apply
145 | max
146 | (filter
147 | values
148 | (list
149 | (skull θ z) (neck θ z) (nose θ z) (lips θ z)
150 | (eyes θ z) (eyebrow θ z)
151 | (ears1 θ z) (ears2 θ z)
152 | 0))))
153 |
154 | ;--------------------------------------------
155 | ;some plot functions to show the face
156 | ;--------------------------------------------
157 | (module+ main
158 | (define (ribonFct f
159 | #:ϕ-min [ϕ-min -inf.0]
160 | #:ϕ-max [ϕ-max +inf.0]
161 | #:t-min [t-min -inf.0]
162 | #:t-max [t-max +inf.0])
163 | (λ (x y z)
164 | (match-define (list ϕ t d0) (xyz->ribbon x y z))
165 | (if (and (<= ϕ-min ϕ ϕ-max)
166 | (<= t-min t t-max))
167 | (- d0 (f ϕ t))
168 | +inf.0)))
169 | (define (memo-parapoint f u0 u1 v0 v1 file #:nr [nr 10000])
170 | (define memo (if (file-exists? file) (file->list file read) '()))
171 | (define len (length memo))
172 | (append
173 | memo
174 | (for*/list ([i (in-range (max 0 (- nr len)))]
175 | [u (in-value (+ u0 (* (- u1 u0) (random))))]
176 | [v (in-value (+ v0 (* (- v1 v0) (random))))]
177 | [xyz (in-value (with-handlers ([exn:fail? (λ (e) #f)])(f u v)))]
178 | #:when xyz)
179 | (call-with-output-file file (λ (out) (writeln xyz out)) #:exists 'append)
180 | xyz)))
181 | (define (parapoint f u0 u1 v0 v1 #:nr [nr 10000] #:memo-it [file #f])
182 | (if file
183 | (memo-parapoint f u0 u1 v0 v1 file #:nr nr)
184 | (for*/list ([i (in-range nr)]
185 | [u (in-value (+ u0 (* (- u1 u0) (random))))]
186 | [v (in-value (+ v0 (* (- v1 v0) (random))))]
187 | [xyz (in-value (with-handlers ([exn:fail? (λ (e) #f)])(f u v)))]
188 | #:when xyz)
189 | xyz)))
190 | )
191 |
192 | #;(module+ main
193 | (plot3d
194 | (list
195 | (points3d (parapoint (λ (θ z) (define r (skull θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 10 290) #:size 1 #:color 'brown)
196 | (points3d (parapoint (λ (θ z) (define r (neck θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 0 90) #:size 1 #:color 'green)
197 | (points3d (parapoint (λ (θ z) (define r (nose θ z)) (list (* r (cos θ))(* r (sin θ)) z)) -.2 .2 120 200) #:size 1 #:color 'red)
198 | (points3d (parapoint (λ (θ z) (define r (lips θ z)) (list (* r (cos θ))(* r (sin θ)) z)) -.4 .4 70 110) #:size 1 #:color 'yellow)
199 | (points3d (parapoint (λ (θ z) (define r (eyes θ z)) (list (* r (cos θ))(* r (sin θ)) z)) -.4 .4 170 190) #:size 1 #:color 'blue)
200 | (points3d (parapoint (λ (θ z) (define r (eyebrow θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 195 205) #:size 1 #:color 'blue)
201 | (points3d (parapoint (λ (θ z) (define r (ears1 θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 100 210) #:size 1 #:color 'gray)
202 | (points3d (parapoint (λ (θ z) (define r (ears2 θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 195 300) #:size 1 #:color 'gray))
203 | #:width 800
204 | #:height 800
205 | #:angle 197
206 | #:altitude 7))
207 |
208 | ;--------------------------------------------
209 | ;result: EEF (escher-egg-face)
210 | ;--------------------------------------------
211 | (module+ main
212 | (plot3d
213 | (points3d
214 | (parapoint
215 | (λ (θ z) (define r (face θ z))
216 | (match-define (list x y _)(list (* r (cos θ))(* r (sin θ)) z))
217 | (match-define (list ϕ t d)(xyz->ribbon x y z))
218 | (if (< (abs t) 10) (list x y z) #f))
219 | (- π) π 0 300
220 | #:nr 30000)
221 | #:size 1)
222 | #:x-min -300 #:x-max 300
223 | #:y-min -300 #:y-max 300
224 | #:z-min 0 #:z-max 600
225 | #:width 800
226 | #:height 800
227 | #:angle 197
228 | #:altitude 7))
229 | (module+ main
230 | (parameterize ([plot-decorations? #f])
231 | (plot3d
232 | (parametric-surface3d
233 | (λ (ϕ t)
234 | (match-define (list x y z)(ribbon->xyz ϕ t 0))
235 | (ribbon->xyz ϕ t (- (face (atan y x) z) R)))
236 | 0 (* (/ 290 P) 2π) #:s-samples 1500
237 | -7 +40 #:t-samples 30
238 | #:color "navajowhite"
239 | #:line-color "navajowhite")
240 | #:x-min -300 #:x-max 300
241 | #:y-min -300 #:y-max 300
242 | #:z-min 0 #:z-max 600
243 | #:width 800
244 | #:height 800
245 | #:angle 210
246 | #:altitude 10)))
247 | ```
248 |
249 |
250 | https://github.com/standard-fish/summer-competititon-2019/blob/master/entries/bedeke/face.rkt
251 |
--------------------------------------------------------------------------------
/examples/face/face.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (require plot
4 | racket/match
5 | racket/file
6 | math)
7 |
8 | (module+ test (require rackunit))
9 | (define (^ b [e 2])(expt b e))
10 |
11 | ;--------------------------------------------
12 | ;ribbon coordinates
13 | ;--------------------------------------------
14 | (define R 100)
15 | (define P 90)
16 | (define π (angle -1))
17 | (define 2π (* 2 π))
18 |
19 | (define (xyz->ribbon x y z)
20 | (define ϕ0 (atan y x))
21 | (define n0 (/ (- z (* P (/ ϕ0 2π))) P))
22 | (define n (round n0))
23 | (define ϕ (+ ϕ0 (* n 2π)))
24 | (define t (- z (* P (/ ϕ 2π))))
25 | (define d (- (sqrt (+ (* x x) (* y y))) R))
26 | ;(println (list ϕ0 n0 n ϕ t d))
27 | (list ϕ t d))
28 | (module+ test
29 | (check-within (xyz->ribbon 100 0 0) '(0 0 0) 1e-15)
30 | (check-within (xyz->ribbon 90 0 0) '(0 0 -10) 1e-15)
31 | (check-within (xyz->ribbon 110 0 0) '(0 0 10) 1e-15)
32 |
33 | (check-within (xyz->ribbon 100 0 4) '(0 4 0) 1e-15)
34 | (check-within (xyz->ribbon 100 0 -4) '(0 -4 0) 1e-15)
35 | (check-within (xyz->ribbon 100 0 (/ P 3)) (list 0 (/ P 3) 0) 1e-15)
36 | (check-within (xyz->ribbon 100 0 (/ P -3)) (list 0 (/ P -3) 0) 1e-15)
37 |
38 | (check-within (xyz->ribbon -100 0 (/ P 2)) (list π 0 0) 1e-15)
39 | (check-within (xyz->ribbon -100 0 (+ (/ P 2)(/ P 3))) (list π (/ P 3) 0) 1e-14)
40 | (check-within (xyz->ribbon -100 0 (+ (/ P 2)(/ P -3))) (list π (/ P -3) 0) 1e-14)
41 |
42 | (check-within (xyz->ribbon 100 0 P) (list 2π 0 0) 1e-15)
43 | (check-within (xyz->ribbon 0 100 (/ P 4)) (list (/ π 2) 0 0) 1e-15)
44 | (check-within (xyz->ribbon 0 90 (/ P 4)) (list (/ π 2) 0 -10) 1e-15)
45 | (check-within (xyz->ribbon 100 100 (* P (+ 2 1/8))) (list (+ (* (+ 2 1/8) 2π)) 0 (* 100 (- (sqrt 2) 1))) 1e-13)
46 | )
47 |
48 | (define (ribbon->xyz ϕ t d)
49 | (list (* (+ R d) (cos ϕ))
50 | (* (+ R d) (sin ϕ))
51 | (+ (* P (/ ϕ 2π)) t)))
52 | (module+ test
53 | (let ([x (* (random) 100)]
54 | [y (* (random) 100)]
55 | [z (* (random) 100)])
56 | (check-within (apply ribbon->xyz (xyz->ribbon x y z)) (list x y z) 1e-10)))
57 |
58 | ;--------------------------------------------
59 | ;egg-face (in cilinder coordinates)
60 | ;--------------------------------------------
61 | (define (skull θ z)
62 | (define Z0 10)(define Z1 290)
63 | (define C0 90)(define C1 -10)
64 | (define C (+ (* (/ (- C0 C1)(expt (- Z1 Z0) 2))(expt (- Z1 z) 2)) C1))
65 | (define A/B0 .2)
66 | (define A/B (+ 1 (* A/B0 (/ (- Z0 z)(- Z0 Z1)))))
67 | (define R0 130)
68 | (define R (* R0 (sqrt (/ (* 4 (- z Z0)(- Z1 z)) (^ (- Z1 Z0))))))
69 |
70 | (define a (+ (^ (cos θ))(^ (* A/B (sin θ)))))
71 | (define b (* -2 C (cos θ)))
72 | (define c (- (^ C)(^ (* R A/B))))
73 | (define D (sqrt (+ (^ b) (* -4 a c))))
74 |
75 | (and (<= Z0 z Z1)
76 | (real? D)
77 | (max (/ (- (- b) D) 2 a)(/ (+ (- b) D) 2 a))))
78 | (define (neck θ z)
79 | (define Z0 0)(define Z1 90)
80 | (define R0 80)(define R1 70)
81 | (and (<= Z0 z Z1)
82 | (- R0 (* (- R0 R1)(* 4 (/ (- z Z0) (- Z1 Z0)) (- 1 (/ (- z Z0) (- Z1 Z0))))))))
83 | (define (nose θ z)
84 | (define Z0 120)(define Z1 200)(define Z2 480)
85 | (define θ0 .15)
86 | (define R0 140)(define R1 180)
87 | (and (<= Z0 z Z1)
88 | (<= (- θ0) θ (+ θ0))
89 | (* (- 1 (/ (- z Z0)
90 | (- Z2 Z0)))
91 | (+ R0 (* (- R1 R0)
92 | (let ([θ* (/ (+ θ θ0) 2 θ0)])(* 4 θ* (- 1 θ*))))))))
93 | (define (lips θ z)
94 | (define Z0 70)(define Z1 110)
95 | (define θ0 .4)
96 | (define R0 150)(define R1 170)
97 | (define Z/2 (/ (+ Z0 Z1) 2))
98 | (and (<= Z0 z Z1)
99 | (<= (- θ0) θ (+ θ0))
100 | (+ R0 (* (- R1 R0)
101 | (if (< z Z/2)
102 | (let ([z* (/ (- z Z0)(- Z/2 Z0))])( * -2.25 z* (- z* 4/3)))
103 | (let ([z* (/ (- z Z/2)(- Z1 Z/2))])(* -2.25 (+ z* 1/3) (- z* 1))))
104 | (let ([θ* (/ (+ θ θ0) 2 θ0)])(* 4 θ* (- 1 θ*)))))))
105 | (define (eyes θ z)
106 | (define Z0 170)(define Z1 190)
107 | (define θ0 .15)(define θ1 .35)
108 | (define R0 140)(define R1 150)
109 | (and (<= Z0 z Z1)
110 | (<= θ0 (abs θ) θ1)
111 | (+ R0 (* (- R1 R0)
112 | (let ([z* (/ (- z Z0)(- Z1 Z0))])(* 4 z* (- 1 z*)))
113 | (let ([θ* (/ (- (abs θ) θ0)(- θ1 θ0))])(* 4 θ* (- 1 θ*)))))))
114 | (define (eyebrow θ z)
115 | (define Z0 195)(define Z1 205)
116 | (define θ0 .55)
117 | (define R0 130)(define R1 145)
118 | (and (<= Z0 z Z1)
119 | (<= (- θ0) θ (+ θ0))
120 | (+ R0 (* (- R1 R0)
121 | (let ([z* (/ (- z Z0)(- Z1 Z0))])(* 4 z* (- 1 z*)))
122 | (let ([θ* (/ (+ θ θ0) 2 θ0)])(* 4 θ* (- 1 θ*)))))))
123 | (define (ears1 θ z)
124 | (define Z0 110)(define Z1 200)
125 | (define θ0 (* .35 (sqrt (/ (* 4 (- z Z0)(- Z1 z)) (^ (- Z1 Z0))))))
126 | (define R0 140)
127 | (and (<= Z0 z Z1)
128 | (<= (- (/ π 2) θ0) (abs θ) (+ (/ π 2) θ0))
129 | R0))
130 | (define (ears2 θ z)
131 | (define Z0 190)(define Z1 295)
132 | (define θ0 .1)
133 | (and (<= Z0 z Z1)
134 | (<= (- (/ π 2) θ0) (abs θ) (+ (/ π 2) θ0))
135 | (+ (or (skull θ z) 0) 10)))
136 | ;combine
137 | (define (face θ z)
138 | (apply
139 | max
140 | (filter
141 | values
142 | (list
143 | (skull θ z) (neck θ z) (nose θ z) (lips θ z)
144 | (eyes θ z) (eyebrow θ z)
145 | (ears1 θ z) (ears2 θ z)
146 | 0))))
147 |
148 | ;--------------------------------------------
149 | ;some plot functions to show the face
150 | ;--------------------------------------------
151 | (module+ main
152 | (define (ribonFct f
153 | #:ϕ-min [ϕ-min -inf.0]
154 | #:ϕ-max [ϕ-max +inf.0]
155 | #:t-min [t-min -inf.0]
156 | #:t-max [t-max +inf.0])
157 | (λ (x y z)
158 | (match-define (list ϕ t d0) (xyz->ribbon x y z))
159 | (if (and (<= ϕ-min ϕ ϕ-max)
160 | (<= t-min t t-max))
161 | (- d0 (f ϕ t))
162 | +inf.0)))
163 | (define (memo-parapoint f u0 u1 v0 v1 file #:nr [nr 10000])
164 | (define memo (if (file-exists? file) (file->list file read) '()))
165 | (define len (length memo))
166 | (append
167 | memo
168 | (for*/list ([i (in-range (max 0 (- nr len)))]
169 | [u (in-value (+ u0 (* (- u1 u0) (random))))]
170 | [v (in-value (+ v0 (* (- v1 v0) (random))))]
171 | [xyz (in-value (with-handlers ([exn:fail? (λ (e) #f)])(f u v)))]
172 | #:when xyz)
173 | (call-with-output-file file (λ (out) (writeln xyz out)) #:exists 'append)
174 | xyz)))
175 | (define (parapoint f u0 u1 v0 v1 #:nr [nr 10000] #:memo-it [file #f])
176 | (if file
177 | (memo-parapoint f u0 u1 v0 v1 file #:nr nr)
178 | (for*/list ([i (in-range nr)]
179 | [u (in-value (+ u0 (* (- u1 u0) (random))))]
180 | [v (in-value (+ v0 (* (- v1 v0) (random))))]
181 | [xyz (in-value (with-handlers ([exn:fail? (λ (e) #f)])(f u v)))]
182 | #:when xyz)
183 | xyz)))
184 | )
185 |
186 | #;(module+ main
187 | (plot3d
188 | (list
189 | (points3d (parapoint (λ (θ z) (define r (skull θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 10 290) #:size 1 #:color 'brown)
190 | (points3d (parapoint (λ (θ z) (define r (neck θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 0 90) #:size 1 #:color 'green)
191 | (points3d (parapoint (λ (θ z) (define r (nose θ z)) (list (* r (cos θ))(* r (sin θ)) z)) -.2 .2 120 200) #:size 1 #:color 'red)
192 | (points3d (parapoint (λ (θ z) (define r (lips θ z)) (list (* r (cos θ))(* r (sin θ)) z)) -.4 .4 70 110) #:size 1 #:color 'yellow)
193 | (points3d (parapoint (λ (θ z) (define r (eyes θ z)) (list (* r (cos θ))(* r (sin θ)) z)) -.4 .4 170 190) #:size 1 #:color 'blue)
194 | (points3d (parapoint (λ (θ z) (define r (eyebrow θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 195 205) #:size 1 #:color 'blue)
195 | (points3d (parapoint (λ (θ z) (define r (ears1 θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 100 210) #:size 1 #:color 'gray)
196 | (points3d (parapoint (λ (θ z) (define r (ears2 θ z)) (list (* r (cos θ))(* r (sin θ)) z)) (- π) π 195 300) #:size 1 #:color 'gray))
197 | #:width 800
198 | #:height 800
199 | #:angle 197
200 | #:altitude 7))
201 |
202 | ;--------------------------------------------
203 | ;result: EEF (escher-egg-face)
204 | ;--------------------------------------------
205 | (module+ main
206 | (plot3d
207 | (points3d
208 | (parapoint
209 | (λ (θ z) (define r (face θ z))
210 | (match-define (list x y _)(list (* r (cos θ))(* r (sin θ)) z))
211 | (match-define (list ϕ t d)(xyz->ribbon x y z))
212 | (if (< (abs t) 10) (list x y z) #f))
213 | (- π) π 0 300
214 | #:nr 30000)
215 | #:size 1)
216 | #:x-min -300 #:x-max 300
217 | #:y-min -300 #:y-max 300
218 | #:z-min 0 #:z-max 600
219 | #:width 800
220 | #:height 800
221 | #:angle 197
222 | #:altitude 7))
223 | (module+ main
224 | (parameterize ([plot-decorations? #f])
225 | (plot3d
226 | (parametric-surface3d
227 | (λ (ϕ t)
228 | (match-define (list x y z)(ribbon->xyz ϕ t 0))
229 | (ribbon->xyz ϕ t (- (face (atan y x) z) R)))
230 | 0 (* (/ 290 P) 2π) #:s-samples 1500
231 | -7 +40 #:t-samples 30
232 | #:color "navajowhite"
233 | #:line-color "navajowhite")
234 | #:x-min -300 #:x-max 300
235 | #:y-min -300 #:y-max 300
236 | #:z-min 0 #:z-max 600
237 | #:width 800
238 | #:height 800
239 | #:angle 210
240 | #:altitude 10)))
241 |
--------------------------------------------------------------------------------
/examples/logo/README.md:
--------------------------------------------------------------------------------
1 | # Logo plot
2 |
3 | 
4 |
5 | ```scheme
6 | #lang racket
7 |
8 | (require plot)
9 |
10 | (let ([blue '(0 0 164)] [red '(164 0 0)])
11 | (parameterize ([plot-decorations? #f])
12 | (plot3d
13 | (list
14 | (surface3d (λ(x y)(/ x 10)) 0 5 0 1 #:color red #:line-color red)
15 | (surface3d (λ(x y)(- 1 (/ (+ 1 (exp (- 5 x)))))) 0 10 0 1 #:color blue #:line-color blue))
16 | #:angle 335 #:altitude 5)))
17 |
18 | ```
19 |
20 | https://github.com/standard-fish/summer-competititon-2019/blob/c5af58e2b1a55733e5e66ca550ebb73420737c4c/entries/Metaxal/racket-logo-plot.rkt
21 |
22 |
--------------------------------------------------------------------------------
/examples/logo/logo-plot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/logo/logo-plot.png
--------------------------------------------------------------------------------
/examples/logo/logo.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 |
3 | (require plot)
4 |
5 | (let ([blue '(0 0 164)] [red '(164 0 0)])
6 | (parameterize ([plot-decorations? #f])
7 | (plot3d
8 | (list
9 | (surface3d (λ(x y)(/ x 10)) 0 5 0 1 #:color red #:line-color red)
10 | (surface3d (λ(x y)(- 1 (/ (+ 1 (exp (- 5 x)))))) 0 10 0 1 #:color blue #:line-color blue))
11 | #:angle 335 #:altitude 5)))
12 |
13 |
--------------------------------------------------------------------------------
/examples/lorenz/README.md:
--------------------------------------------------------------------------------
1 | # Lorenz
2 |
3 | 
4 | ```scheme
5 | #lang racket
6 | (require (planet williams/science/ode-initval)
7 | plot)
8 |
9 | (define (lorenz t y dy params)
10 | (let ((sigma (car params))
11 | (rho (second params))
12 | (beta (third params))
13 | (y0 (vector-ref y 0))
14 | (y1 (vector-ref y 1))
15 | (y2 (vector-ref y 2)))
16 | (vector-set! dy 0 (* sigma (- y1 y0)))
17 | (vector-set! dy 1 (- (* y0 (- rho y2)) y1))
18 | (vector-set! dy 2 (- (* y0 y1) (* beta y2)))))
19 |
20 | (define (main sigma rho beta)
21 | (let* ((type rk4-ode-type)
22 | (step (make-ode-step type 3))
23 | (params (list sigma rho beta))
24 | (system (make-ode-system lorenz #f 3 params))
25 | (t 0.0)
26 | (t1 500.0)
27 | (h 0.01)
28 | (y (vector 1.0 0.0 0.5))
29 | (y-err (make-vector 3))
30 | (dydt-in (make-vector 3))
31 | (dydt-out (make-vector 3))
32 | (y0-values '())
33 | (y1-values '())
34 | (y2-values '()))
35 | (ode-system-function-eval system t y dydt-in)
36 | (let loop ()
37 | (when (< t t1)
38 | (ode-step-apply step t h
39 | y y-err
40 | dydt-in
41 | dydt-out
42 | system)
43 | (set! y0-values (cons (vector-ref y 0) y0-values))
44 | (set! y1-values (cons (vector-ref y 1) y1-values))
45 | (set! y2-values (cons (vector-ref y 2) y2-values))
46 | (vector-set! dydt-in 0 (vector-ref dydt-out 0))
47 | (vector-set! dydt-in 1 (vector-ref dydt-out 1))
48 | (vector-set! dydt-in 2 (vector-ref dydt-out 2))
49 | (set! t (+ t h))
50 | (loop)))
51 | (define x-z (plot (lines (map vector y0-values y2-values) #:color "blue")
52 | #:x-label "x"
53 | #:y-label "z"))
54 | x-z))
55 |
56 | (main 10.0 28. (/ 8. 3.))
57 | ```
58 |
--------------------------------------------------------------------------------
/examples/lorenz/lorenz.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 | (require (planet williams/science/ode-initval)
3 | plot)
4 |
5 | (define (lorenz t y dy params)
6 | (let ((sigma (car params))
7 | (rho (second params))
8 | (beta (third params))
9 | (y0 (vector-ref y 0))
10 | (y1 (vector-ref y 1))
11 | (y2 (vector-ref y 2)))
12 | (vector-set! dy 0 (* sigma (- y1 y0)))
13 | (vector-set! dy 1 (- (* y0 (- rho y2)) y1))
14 | (vector-set! dy 2 (- (* y0 y1) (* beta y2)))))
15 |
16 | (define (main sigma rho beta)
17 | (let* ((type rk4-ode-type)
18 | (step (make-ode-step type 3))
19 | (params (list sigma rho beta))
20 | (system (make-ode-system lorenz #f 3 params))
21 | (t 0.0)
22 | (t1 500.0)
23 | (h 0.01)
24 | (y (vector 1.0 0.0 0.5))
25 | (y-err (make-vector 3))
26 | (dydt-in (make-vector 3))
27 | (dydt-out (make-vector 3))
28 | (y0-values '())
29 | (y1-values '())
30 | (y2-values '()))
31 | (ode-system-function-eval system t y dydt-in)
32 | (let loop ()
33 | (when (< t t1)
34 | (ode-step-apply step t h
35 | y y-err
36 | dydt-in
37 | dydt-out
38 | system)
39 | (set! y0-values (cons (vector-ref y 0) y0-values))
40 | (set! y1-values (cons (vector-ref y 1) y1-values))
41 | (set! y2-values (cons (vector-ref y 2) y2-values))
42 | (vector-set! dydt-in 0 (vector-ref dydt-out 0))
43 | (vector-set! dydt-in 1 (vector-ref dydt-out 1))
44 | (vector-set! dydt-in 2 (vector-ref dydt-out 2))
45 | (set! t (+ t h))
46 | (loop)))
47 | (plot (lines (map vector y0-values y2-values) #:color "blue")
48 | #:x-label "x"
49 | #:y-label "z"
50 | #:out-file "lorenz.svg")))
51 |
52 | (main 10.0 28. (/ 8. 3.))
--------------------------------------------------------------------------------
/examples/rose/README.md:
--------------------------------------------------------------------------------
1 | # Rose
2 |
3 | 
4 |
5 | ```scheme
6 | #lang racket
7 | (require plot)
8 |
9 | ;; plot settings
10 | (line-width 1.5)
11 | (plot-width 200)
12 | (plot-height 200)
13 | (plot-font-size 10)
14 | (plot-x-far-axis? #f)
15 | (plot-y-far-axis? #f)
16 | (plot-x-axis? #f)
17 | (plot-y-axis? #f)
18 | (plot-x-label #f)
19 | (plot-y-label #f)
20 | (plot-pen-color-map 'tab10)
21 |
22 | (define (rose theta k a)
23 | (* a (cos (* k theta))))
24 |
25 | (plot (polar (lambda (theta) (rose theta (/ 7 8) 1)) 0 (* 16 pi) #:samples 500 #:color "blue")
26 | #:out-file "rose.svg")
27 | ```
28 |
--------------------------------------------------------------------------------
/examples/rose/rose.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 | (require plot)
3 |
4 | ;; plot settings
5 | (line-width 1.5)
6 | (plot-width 200)
7 | (plot-height 200)
8 | (plot-font-size 10)
9 | (plot-x-far-axis? #f)
10 | (plot-y-far-axis? #f)
11 | (plot-x-axis? #f)
12 | (plot-y-axis? #f)
13 | (plot-x-label #f)
14 | (plot-y-label #f)
15 | (plot-pen-color-map 'tab10)
16 |
17 | (define (rose theta k a)
18 | (* a (cos (* k theta))))
19 |
20 | (plot (polar (lambda (theta) (rose theta (/ 7 8) 1)) 0 (* 16 pi) #:samples 500 #:color "blue")
21 | #:out-file "rose.svg")
22 |
--------------------------------------------------------------------------------
/examples/rose/rose.svg:
--------------------------------------------------------------------------------
1 |
2 |
8 |
--------------------------------------------------------------------------------
/examples/sines/README.md:
--------------------------------------------------------------------------------
1 | # Sines
2 |
3 |
4 |
5 | 
6 |
7 | ```scheme
8 | #lang at-exp racket
9 | (require infix
10 | plot/pict
11 | threading)
12 |
13 | (define (f a x) @${sin[(10+a)*x]})
14 | (define (g a x) @${x / (a+5)})
15 |
16 | (parameterize ([line-width 2]
17 | [plot-x-label "Degrees"]
18 | [plot-y-label "Magnitude"]
19 | [plot-y-ticks (linear-ticks #:number 5)]
20 | [plot-width 800]
21 | [plot-aspect-ratio 2]
22 | [plot-legend-anchor 'outside-right-top]
23 | [plot-pen-color-map 'tab10])
24 | (plot #:x-min 0
25 | #:x-max 50
26 | (for/list ([a (inclusive-range 1 6)])
27 | (function (λ~>> degrees->radians
28 | (f a)
29 | (g a))
30 | #:color (sub1 a)
31 | #:label (~a "1/" (+ a 5) " sin " (+ 10 a) "x")))))
32 |
33 | ```
34 |
35 | https://gist.github.com/hunkyjimpjorps/9e512686230c6c1472f4a0d73b81d9dc
36 |
37 |
38 |
39 |
40 | ***
41 |
42 |
--------------------------------------------------------------------------------
/examples/sines/sines.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/sines/sines.png
--------------------------------------------------------------------------------
/examples/sines/sines.rkt:
--------------------------------------------------------------------------------
1 | #lang at-exp racket
2 | (require infix
3 | plot/pict
4 | threading)
5 |
6 | (define (f a x) @${sin[(10+a)*x]})
7 | (define (g a x) @${x / (a+5)})
8 |
9 | (parameterize ([line-width 2]
10 | [plot-x-label "Degrees"]
11 | [plot-y-label "Magnitude"]
12 | [plot-y-ticks (linear-ticks #:number 5)]
13 | [plot-width 800]
14 | [plot-aspect-ratio 2]
15 | [plot-legend-anchor 'outside-right-top])
16 |
17 | (plot (for/list ([a (inclusive-range 1 6)])
18 | (function (λ~>> degrees->radians
19 | (f a)
20 | (g a))
21 | 0 50
22 | #:color a
23 | #:label (~a "1/" (+ a 5) " sin " (+ 10 a) "x")))))
--------------------------------------------------------------------------------
/examples/violin/README.md:
--------------------------------------------------------------------------------
1 | # Violin Plot
2 |
3 | 
4 |
5 | ```scheme
6 | #lang racket
7 |
8 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 | ;; require
10 |
11 | (require math/statistics
12 | plot/pict
13 | plot/utils)
14 |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 | ;; violin
17 |
18 | (define (violin vals
19 | #:bandwidth [bandwidth (silverman (second vals))]
20 | #:x-min [x-min #f]
21 | #:x-max [x-max #f]
22 | #:y-min [y-min #f]
23 | #:y-max [y-max #f]
24 | #:color [color (interval-color)]
25 | #:style [style (interval-style)]
26 | #:line1-color [line1-color (interval-line1-color)]
27 | #:line1-width [line1-width (interval-line1-width)]
28 | #:line1-style [line1-style (interval-line1-style)]
29 | #:line2-color [line2-color (interval-line2-color)]
30 | #:line2-width [line2-width (interval-line2-width)]
31 | #:line2-style [line2-style (interval-line2-style)]
32 | #:alpha [alpha (interval-alpha)]
33 | #:label [label #f])
34 | (define y-shift (first vals))
35 | (define-values (f low high)
36 | (kde (second vals) bandwidth))
37 | (define x-axis (const 0))
38 | (define x-min* (or x-min low))
39 | (define x-max* (or x-max high))
40 | (define settings
41 | `([#:y-min . ,y-min]
42 | [#:y-max . ,y-max]
43 | [#:color . ,color]
44 | [#:style . ,style]
45 | [#:line1-color . ,line1-color]
46 | [#:line1-width . ,line1-width]
47 | [#:line1-style . ,line1-style]
48 | [#:line2-color . ,line2-color]
49 | [#:line2-width . ,line2-width]
50 | [#:line2-style . ,line2-style]
51 | [#:alpha . ,alpha]
52 | [#:label . ,label]))
53 | (list (keyword-apply/dict function-interval settings
54 | (shift-up (invert f) y-shift)
55 | (shift-up f y-shift)
56 | x-min* x-max* null)))
57 |
58 | (define (shift-up f shift)
59 | (λ (x)
60 | (+ (f x) shift)))
61 |
62 | (define ((invert f) x)
63 | (- (f x)))
64 |
65 | (define (silverman vals)
66 | (define iqr (interquartile-range vals))
67 | (define n (length vals))
68 | (* 0.9
69 | (min (stddev vals) (/ iqr 1.34))
70 | (expt n -0.2)))
71 |
72 | (define (interquartile-range vals)
73 | (- (quantile 3/4 < vals)
74 | (quantile 1/4 < vals)))
75 |
76 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77 | ;; example
78 | ;;
79 | ;; `(violin (list y-center x-data))`
80 | ;;
81 |
82 | (parameterize ([plot-y-ticks no-ticks]
83 | [plot-y-label #f]
84 | [plot-x-far-ticks no-ticks]
85 | [plot-x-label "Time (sec)"])
86 | (plot (list (violin `[0.00 (0 1 1 2 3 4 4 4 5 6 7 9 10 10 10 11 13)])
87 | (violin `[0.30 (15 16 17 18 19 20 20 21 23 30)]))))
88 | ```
89 |
90 | http://pasterack.org/pastes/66996
91 |
92 |
93 |
--------------------------------------------------------------------------------
/examples/violin/violin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Racket-Cookbooks/Plot-cookbook/5030990113d194875d050798b37a93f0793be245/examples/violin/violin.png
--------------------------------------------------------------------------------
/examples/violin/violin.rkt:
--------------------------------------------------------------------------------
1 | #lang racket
2 |
3 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 | ;; require
5 |
6 |
7 | (require math/statistics
8 | plot/pict
9 | plot/utils)
10 |
11 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 | ;; violin
13 |
14 | (define (violin vals
15 | #:bandwidth [bandwidth (silverman (second vals))]
16 | #:x-min [x-min #f]
17 | #:x-max [x-max #f]
18 | #:y-min [y-min #f]
19 | #:y-max [y-max #f]
20 | #:color [color (interval-color)]
21 | #:style [style (interval-style)]
22 | #:line1-color [line1-color (interval-line1-color)]
23 | #:line1-width [line1-width (interval-line1-width)]
24 | #:line1-style [line1-style (interval-line1-style)]
25 | #:line2-color [line2-color (interval-line2-color)]
26 | #:line2-width [line2-width (interval-line2-width)]
27 | #:line2-style [line2-style (interval-line2-style)]
28 | #:alpha [alpha (interval-alpha)]
29 | #:label [label #f])
30 | (define y-shift (first vals))
31 | (define-values (f low high)
32 | (kde (second vals) bandwidth))
33 | (define x-axis (const 0))
34 | (define x-min* (or x-min low))
35 | (define x-max* (or x-max high))
36 | (define settings
37 | `([#:y-min . ,y-min]
38 | [#:y-max . ,y-max]
39 | [#:color . ,color]
40 | [#:style . ,style]
41 | [#:line1-color . ,line1-color]
42 | [#:line1-width . ,line1-width]
43 | [#:line1-style . ,line1-style]
44 | [#:line2-color . ,line2-color]
45 | [#:line2-width . ,line2-width]
46 | [#:line2-style . ,line2-style]
47 | [#:alpha . ,alpha]
48 | [#:label . ,label]))
49 | (list (keyword-apply/dict function-interval settings
50 | (shift-up (invert f) y-shift)
51 | (shift-up f y-shift)
52 | x-min* x-max* null)))
53 |
54 | (define (shift-up f shift)
55 | (λ (x)
56 | (+ (f x) shift)))
57 |
58 | (define ((invert f) x)
59 | (- (f x)))
60 |
61 | (define (silverman vals)
62 | (define iqr (interquartile-range vals))
63 | (define n (length vals))
64 | (* 0.9
65 | (min (stddev vals) (/ iqr 1.34))
66 | (expt n -0.2)))
67 |
68 | (define (interquartile-range vals)
69 | (- (quantile 3/4 < vals)
70 | (quantile 1/4 < vals)))
71 |
72 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 | ;; example
74 | ;;
75 | ;; `(violin (list y-center x-data))`
76 | ;;
77 |
78 | (parameterize ([plot-y-ticks no-ticks]
79 | [plot-y-label #f]
80 | [plot-x-far-ticks no-ticks]
81 | [plot-x-label "Time (sec)"])
82 | (plot (list (violin `[0.00 (0 1 1 2 3 4 4 4 5 6 7 9 10 10 10 11 13)])
83 | (violin `[0.30 (15 16 17 18 19 20 20 21 23 30)]))))
84 |
--------------------------------------------------------------------------------
/info.rkt:
--------------------------------------------------------------------------------
1 | #lang info
2 | (define collection "plot-cookbook")
3 | (define deps '("base"))
4 | (define build-deps '("scribble-lib" "racket-doc" "rackunit-lib" "infix" "threading" "math/statistics" "data-frame" "csv-reading"))
5 | (define scribblings '(("scribblings/plot-cookbook.scrbl" ())))
6 | (define pkg-desc "Description Here")
7 | (define version "0.0")
8 | (define pkg-authors '(spdegabrielle))
9 | (define license '(Apache-2.0 OR MIT))
10 |
--------------------------------------------------------------------------------
/main.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (module+ test
4 | (require rackunit))
5 |
6 | ;; Notice
7 | ;; To install (from within the package directory):
8 | ;; $ raco pkg install
9 | ;; To install (once uploaded to pkgs.racket-lang.org):
10 | ;; $ raco pkg install <>
11 | ;; To uninstall:
12 | ;; $ raco pkg remove <>
13 | ;; To view documentation:
14 | ;; $ raco docs <>
15 | ;;
16 | ;; For your convenience, we have included LICENSE-MIT and LICENSE-APACHE files.
17 | ;; If you would prefer to use a different license, replace those files with the
18 | ;; desired license.
19 | ;;
20 | ;; Some users like to add a `private/` directory, place auxiliary files there,
21 | ;; and require them in `main.rkt`.
22 | ;;
23 | ;; See the current version of the racket style guide here:
24 | ;; http://docs.racket-lang.org/style/index.html
25 |
26 | ;; Code here
27 |
28 |
29 |
30 | (module+ test
31 | ;; Any code in this `test` submodule runs when this file is run using DrRacket
32 | ;; or with `raco test`. The code here does not run when this file is
33 | ;; required by another module.
34 |
35 | (check-equal? (+ 2 2) 4))
36 |
37 | (module+ main
38 | ;; (Optional) main submodule. Put code here if you need it to be executed when
39 | ;; this file is run using DrRacket or the `racket` executable. The code here
40 | ;; does not run when this file is required by another module. Documentation:
41 | ;; http://docs.racket-lang.org/guide/Module_Syntax.html#%28part._main-and-test%29
42 |
43 | (require racket/cmdline)
44 | (define who (box "world"))
45 | (command-line
46 | #:program "my-program"
47 | #:once-each
48 | [("-n" "--name") name "Who to say hello to" (set-box! who name)]
49 | #:args ()
50 | (printf "hello ~a~n" (unbox who))))
51 |
--------------------------------------------------------------------------------
/scribblings/basic-cook.scrbl:
--------------------------------------------------------------------------------
1 | #lang scribble/manual
2 | @(require "common.rkt" plot threading data-frame)
3 |
4 | @(define plot-eval-df
5 | (let ([eval (make-base-eval)])
6 | (eval '(begin
7 | (require racket
8 | plot/pict
9 | plot/utils
10 | data-frame
11 | threading)))
12 | eval))
13 |
14 | @title{Basic Recipes}
15 |
16 | Inspired by the @(hyperlink "https://github.com/PacktPublishing/Matplotlib-3.0-Cookbook"
17 | "Matplotlib Cookbook"), this section contains
18 | some recipes that begin at the very beginning.
19 |
20 | @bold{NOTICE}: All of the code in this section is designed to be used with @tt{#lang racket}. If you use @tt{racket/base} you will need to require more libraries.
21 |
22 | @section{Two Points}
23 |
24 | A line through (0, 1.5) and (1, 3).
25 |
26 | @interaction[#:eval plot-eval
27 | (eval:alts (require plot) (void))
28 | (plot
29 | (lines (list (vector 0 1.5)
30 | (vector 1 3.0))))]
31 |
32 | Thicker and more colorful lines, with axes labels and a title.
33 |
34 | @interaction[#:eval plot-eval
35 | (eval:alts (require plot) (void))
36 | (parameterize [(line-width 2)]
37 | (plot (list (lines (list #(0 1.5) #(1 3.0)) #:color 'blue)
38 | (lines (list #(0 3.5) #(1 2.5)) #:color 'orange))
39 | #:title "Interactive Plot"
40 | #:x-label "X-axis"
41 | #:y-label "Y-axis"))]
42 |
43 | @section{Getting Data}
44 |
45 | I advise using the @racketmodname{data-frame} package to import your data. It is easy
46 | to work with and does not require dropping into "lower level" operations
47 | to do things like parse strings into numbers.
48 |
49 | One way to get data from an external source is to write it to a CSV
50 | file. The file would look like this:
51 |
52 | @verbatim{x,y
53 | 1,1
54 | 2,4
55 | 3,9
56 | 4,16
57 | 5,25}
58 |
59 | We put the data into a string so this tutorial does not need any
60 | external files. There are better choices for importing data in practice.
61 | @itemlist[@item{Using
62 | @code{(df-read/csv "filename.csv")} is best choice. Use a header row to specify column names.}
63 | @item{A single string that crosses multiple lines is the next best choice. For example, you could copy and paste the comma-separated x and y
64 | coordinates into the string.}
65 | @item{Embedded @tt{\n} are used below just to save space in the example.}]
66 |
67 | The @racket[df-select*] function is used to extract the specified
68 | columns from a data frame and turn them into a list of vectors, one
69 | for each row. This format is suitable for @racket[lines].
70 |
71 | @interaction[#:eval plot-eval-df
72 | (eval:alts (require plot data-frame) (void))
73 |
74 | (define csv-data "x,y\n1,1\n2,4\n3,9\n4,16\n5,25")
75 | (define csv-port (open-input-string csv-data))
76 | (define df1 (df-read/csv (open-input-string csv-data)))
77 | (close-input-port csv-port)
78 | (plot (lines (df-select* df1 "x" "y")))]
79 |
80 | Note: a fancier way to read the data is the one liner @code{(call-with-input-string csv-data df-read/csv)}, but it is a little harder to understand and modify.
81 |
--------------------------------------------------------------------------------
/scribblings/common.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (require scribble/eval
4 | (for-label racket
5 | racket/gui/base
6 | pict
7 | db
8 | plot
9 | plot/utils
10 | plot/snip
11 | (only-in racket/sequence sequence/c)))
12 |
13 | (provide (all-defined-out)
14 | (all-from-out scribble/eval)
15 | (for-label (all-from-out racket
16 | racket/gui/base
17 | pict
18 | db
19 | plot
20 | plot/snip
21 | plot/utils)
22 | sequence/c))
23 |
24 | (require (for-syntax racket/base
25 | syntax/parse
26 | racket/syntax)
27 | (prefix-in s. scribble/manual)
28 | (only-in racket/contract any/c)
29 | (for-label (only-in racket/contract any/c)))
30 |
31 | (define (author-email) "neil.toronto@gmail.com")
32 |
33 | (define (plot-name) "Plot")
34 |
35 | (define plot-eval
36 | (let ([eval (make-base-eval)])
37 | (eval '(begin
38 | (require racket/math racket/match racket/list racket/draw racket/class
39 | plot/pict
40 | plot/utils)))
41 | eval))
42 |
43 | (define (close-plot-eval)
44 | (close-eval plot-eval))
45 |
46 | (require plot/no-gui plot/utils pict racket/match racket/class racket/draw)
47 | (define (pretty-print-color-maps (width 400) (height 30))
48 | (define cm-names
49 | (sort (color-map-names)
50 | (lambda (a b)
51 | (string<=? (symbol->string a) (symbol->string b)))))
52 | (define cm-labels
53 | (for/list ([cm cm-names])
54 | (text (symbol->string cm) null 16)))
55 | (define cm-picts
56 | (for/list ([cm cm-names])
57 | (parameterize ([plot-pen-color-map cm])
58 | (define w (/ width (color-map-size cm)))
59 | (apply
60 | hc-append 0
61 | (for/list ([c (in-range (color-map-size cm))])
62 | (match-define (list r g b) (->pen-color c))
63 | (define color (make-object color% r g b))
64 | (filled-rectangle w height #:draw-border? #f #:color color))))))
65 | (define picts
66 | (let loop ([result '()]
67 | [labels cm-labels]
68 | [picts cm-picts])
69 | (if (null? labels)
70 | (reverse result)
71 | (loop (cons (car picts) (cons (car labels) result))
72 | (cdr labels)
73 | (cdr picts)))))
74 | (table 2 picts lc-superimpose cc-superimpose 15 3))
--------------------------------------------------------------------------------
/scribblings/plot-cookbook.scrbl:
--------------------------------------------------------------------------------
1 | #lang scribble/manual
2 | @require[@for-label[racket/base]]
3 | @(require "common.rkt")
4 |
5 | @title[#:tag "top"]{Plot Cookbook}
6 | @author{Editor:spdegabrielle/various authors}
7 |
8 | @defmodule[plot-cookbook]
9 |
10 | The Plot library provides a flexible interface for producing nearly any kind of plot.
11 | It includes many common kinds of plots already, such as scatter plots, line plots, contour plots, histograms, and 3D surfaces and isosurfaces.
12 | Thanks to Racket's excellent multiple-backend drawing library, Plot can render plots as interactive snips in DrRacket, as picts in slideshows, as PNG, PDF, PS and SVG files, or on any device context.
13 |
14 | The Plot Cookbook aims to provide an set of examples for education and resuse.
15 |
16 | For more about Plot see @racketmodname[plot].
17 |
18 |
19 | @table-of-contents[]
20 |
21 | @include-section["sines.scrbl"]
22 |
23 | @include-section["violin.scrbl"]
24 |
25 | @include-section["basic-cook.scrbl"]
26 |
27 | @close-plot-eval[]
28 |
29 | @; Needs a timeout for testing:
30 | @(module* test racket/base
31 | (require (submod ".."))
32 | (module config info
33 | (define timeout 180)))
--------------------------------------------------------------------------------
/scribblings/sines.scrbl:
--------------------------------------------------------------------------------
1 | #lang scribble/manual
2 |
3 | @(require "common.rkt" infix plot/pict threading)
4 |
5 | @title[#:tag "Sines"]{Sines}
6 |
7 | Overlaying a set of sine functions in a single plot.
8 |
9 | @;@racketinput[]
10 | @interaction[#:eval plot-eval
11 | (eval:alts (require infix
12 | plot/pict
13 | threading) (void))
14 | (define (f a x) @${sin[(10+a)*x]})
15 | (define (g a x) @${x / (a+5)})
16 |
17 | (parameterize ([line-width 2]
18 | [plot-x-label "Degrees"]
19 | [plot-y-label "Magnitude"]
20 | [plot-y-ticks (linear-ticks #:number 5)]
21 | [plot-width 800]
22 | [plot-aspect-ratio 2]
23 | [plot-legend-anchor 'outside-right-top])
24 |
25 | (plot (for/list ([a (inclusive-range 1 6)])
26 | (function (λ~>> degrees->radians
27 | (f a)
28 | (g a))
29 | 0 50
30 | #:color a
31 | #:label (~a "1/" (+ a 5) " sin " (+ 10 a) "x")))))]
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/scribblings/violin.scrbl:
--------------------------------------------------------------------------------
1 | #lang scribble/manual
2 |
3 | @(require "common.rkt" plot/pict math/statistics)
4 |
5 | @title[#:tag "Violin"]{Violin}
6 |
7 | Violin plot.
8 |
9 | @;@racketinput[]
10 | @interaction[#:eval plot-eval
11 | (eval:alts (require math/statistics
12 | plot/pict
13 | plot/utils) (void))
14 |
15 |
16 |
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18 | ;; violin
19 |
20 | (define (violin vals
21 | #:bandwidth [bandwidth (silverman (second vals))]
22 | #:x-min [x-min #f]
23 | #:x-max [x-max #f]
24 | #:y-min [y-min #f]
25 | #:y-max [y-max #f]
26 | #:color [color (interval-color)]
27 | #:style [style (interval-style)]
28 | #:line1-color [line1-color (interval-line1-color)]
29 | #:line1-width [line1-width (interval-line1-width)]
30 | #:line1-style [line1-style (interval-line1-style)]
31 | #:line2-color [line2-color (interval-line2-color)]
32 | #:line2-width [line2-width (interval-line2-width)]
33 | #:line2-style [line2-style (interval-line2-style)]
34 | #:alpha [alpha (interval-alpha)]
35 | #:label [label #f])
36 | (define y-shift (first vals))
37 | (define-values (f low high)
38 | (kde (second vals) bandwidth))
39 | (define x-axis (const 0))
40 | (define x-min* (or x-min low))
41 | (define x-max* (or x-max high))
42 | (define settings
43 | `([#:y-min . ,y-min]
44 | [#:y-max . ,y-max]
45 | [#:color . ,color]
46 | [#:style . ,style]
47 | [#:line1-color . ,line1-color]
48 | [#:line1-width . ,line1-width]
49 | [#:line1-style . ,line1-style]
50 | [#:line2-color . ,line2-color]
51 | [#:line2-width . ,line2-width]
52 | [#:line2-style . ,line2-style]
53 | [#:alpha . ,alpha]
54 | [#:label . ,label]))
55 | (list (keyword-apply/dict function-interval settings
56 | (shift-up (invert f) y-shift)
57 | (shift-up f y-shift)
58 | x-min* x-max* null)))
59 |
60 | (define (shift-up f shift)
61 | (λ (x)
62 | (+ (f x) shift)))
63 |
64 | (define ((invert f) x)
65 | (- (f x)))
66 |
67 | (define (silverman vals)
68 | (define iqr (interquartile-range vals))
69 | (define n (length vals))
70 | (* 0.9
71 | (min (stddev vals) (/ iqr 1.34))
72 | (expt n -0.2)))
73 |
74 | (define (interquartile-range vals)
75 | (- (quantile 3/4 < vals)
76 | (quantile 1/4 < vals)))
77 |
78 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 | ;; example
80 | ;;
81 | ;; `(violin (list y-center x-data))`
82 | ;;
83 |
84 | (parameterize ([plot-y-ticks no-ticks]
85 | [plot-y-label #f]
86 | [plot-x-far-ticks no-ticks]
87 | [plot-x-label "Time (sec)"])
88 | (plot (list (violin `[0.00 (0 1 1 2 3 4 4 4 5 6 7 9 10 10 10 11 13)])
89 | (violin `[0.30 (15 16 17 18 19 20 20 21 23 30)]))))]
90 |
--------------------------------------------------------------------------------