{"size": 2872, "ext": "scrbl", "max_stars_repo_path": "scribblings/basic-cook.scrbl", "max_stars_repo_name": "md-arif-shaikh/Plot-cookbook", "max_stars_repo_stars_event_min_datetime": "2022-01-15T08:37:22.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-06T14:48:38.000Z", "max_issues_repo_path": "scribblings/basic-cook.scrbl", "max_issues_repo_name": "md-arif-shaikh/Plot-cookbook", "max_issues_repo_issues_event_min_datetime": "2022-01-14T21:15:26.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-20T17:22:46.000Z", "max_forks_repo_path": "scribblings/basic-cook.scrbl", "max_forks_repo_name": "md-arif-shaikh/Plot-cookbook", "max_forks_repo_forks_event_min_datetime": "2022-01-16T02:35:51.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-19T14:15:23.000Z"}
{"max_stars_count": 17, "max_issues_count": 4, "max_forks_count": 6, "avg_line_length": 35.4567901235, "max_line_length": 162, "alphanum_fraction": 0.6671309192}
#lang scribble/manual
@(require "common.rkt" plot threading data-frame)

@(define plot-eval-df
  (let ([eval  (make-base-eval)])
    (eval '(begin
             (require racket
                      plot/pict
                      plot/utils
                      data-frame
                      threading)))
    eval))

@title{Basic Recipes}

Inspired by the @(hyperlink "https://github.com/PacktPublishing/Matplotlib-3.0-Cookbook"
"Matplotlib Cookbook"), this section contains 
some recipes that begin at the very beginning.

@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.

@section{Two Points}

A line through (0, 1.5) and (1, 3).

@interaction[#:eval plot-eval
                    (eval:alts (require plot) (void))
(plot
 (lines (list (vector 0 1.5)
              (vector 1 3.0))))]

Thicker and more colorful lines, with axes labels and a title.

@interaction[#:eval plot-eval
                    (eval:alts (require plot) (void))
(parameterize [(line-width 2)]
  (plot (list (lines (list #(0 1.5) #(1 3.0)) #:color 'blue)
              (lines (list #(0 3.5) #(1 2.5)) #:color 'orange))
        #:title "Interactive Plot"
        #:x-label "X-axis"
        #:y-label "Y-axis"))]

@section{Getting Data}

I advise using the @racketmodname{data-frame} package to import your data. It is easy
to work with and does not require dropping into "lower level" operations
to do things like parse strings into numbers.

One way to get data from an external source is to write it to a CSV
file. The file would look like this:

@verbatim{x,y
1,1
2,4
3,9
4,16
5,25}

We put the data into a string so this tutorial does not need any
external files. There are better choices for importing data in practice.
@itemlist[@item{Using
                   @code{(df-read/csv "filename.csv")} is best choice. Use a header row to specify column names.}
@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
coordinates into the string.}
@item{Embedded @tt{\n} are used below just to save space in the example.}]

The @racket[df-select*] function is used to extract the specified
columns from a data frame and turn them into a list of vectors, one
for each row. This format is suitable for @racket[lines].

@interaction[#:eval plot-eval-df
                    (eval:alts (require plot data-frame) (void))

(define csv-data "x,y\n1,1\n2,4\n3,9\n4,16\n5,25")
(define csv-port (open-input-string csv-data))
(define df1 (df-read/csv (open-input-string csv-data)))
(close-input-port csv-port)
(plot (lines (df-select* df1 "x" "y")))]

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.
