Skip to content

Commit 45a30ad

Browse files
author
Mauer-Oats
committed
Started a clone of the matplotlib cookbook.
1 parent db3b7ee commit 45a30ad

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

info.rkt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#lang info
22
(define collection "plot-cookbook")
33
(define deps '("base"))
4-
(define build-deps '("scribble-lib" "racket-doc" "rackunit-lib" "infix" "threading" "math/statistics"))
4+
(define build-deps '("scribble-lib" "racket-doc" "rackunit-lib" "infix" "threading" "math/statistics" "data-frame" "csv-reading"))
55
(define scribblings '(("scribblings/plot-cookbook.scrbl" ())))
66
(define pkg-desc "Description Here")
77
(define version "0.0")

scribblings/basic-cook.scrbl

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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/math racket/match racket/list racket/draw racket/class
8+
plot/pict
9+
plot/utils
10+
data-frame)))
11+
eval))
12+
13+
@title{Basic Recipes}
14+
15+
Inspired by the @(hyperlink "https://github.com/PacktPublishing/Matplotlib-3.0-Cookbook"
16+
"Matplotlib Cookbook"), this section contains
17+
some recipes that begin at the very beginning.
18+
19+
@section{Two Points}
20+
21+
A line through (0, 1.5) and (1, 3).
22+
23+
@interaction[#:eval plot-eval
24+
(eval:alts (require plot/pict) (void))
25+
(plot
26+
(lines (list (vector 0 1.5)
27+
(vector 1 3.0))))]
28+
29+
Thicker and more colorful lines, with axes labels and a title.
30+
31+
@interaction[#:eval plot-eval
32+
(eval:alts (require plot/pict) (void))
33+
(parameterize [(line-width 2)]
34+
(plot (list (lines (list #(0 1.5) #(1 3.0)) #:color 'blue)
35+
(lines (list #(0 3.5) #(1 2.5)) #:color 'orange))
36+
#:title "Interactive Plot"
37+
#:x-label "X-axis"
38+
#:y-label "Y-axis"))]
39+
40+
@section{Getting Data}
41+
42+
I advise using the data-frame package to import your data. It is easy
43+
to work with and does not require dropping into "lower level" operations
44+
to do things like parse strings into numbers.
45+
46+
One way to get data from an external source is to write it to a CSV
47+
file. The file would look like this:
48+
49+
@verbatim{x,y
50+
1,1
51+
2,4
52+
3,9
53+
4,16
54+
5,25}
55+
56+
We put the data into a string so this tutorial does not need any
57+
external files. As a user, you would use a single string that crosses
58+
multiple lines, not using the embedded @tt{\n} used
59+
below. For example, you could copy and paste the comma-separated x and y
60+
coordinates into the string.
61+
62+
The @racket{df-select*} function is used to extract the specified
63+
columns from a data frame and turn them into a list of vectors, one
64+
for each row. This format is suitable for @racket{lines}.
65+
66+
@interaction[#:eval plot-eval-df
67+
(eval:alts (require plot data-frame) (void))
68+
69+
(define csv-data "x,y\n1,1\n2,4\n3,9\n4,16\n5,25")
70+
(define csv-port (open-input-string csv-data))
71+
(define df1 (df-read/csv csv-port))
72+
(plot (lines (df-select* df1 "x" "y")))]
73+

scribblings/plot-cookbook.scrbl

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ For more about Plot see @racketmodname[plot].
2222

2323
@include-section["violin.scrbl"]
2424

25+
@include-section["basic-cook.scrbl"]
2526

2627
@close-plot-eval[]
2728

0 commit comments

Comments
 (0)