|
| 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 | + |
0 commit comments