Plots in POOL
Plot in POOL is an object, just like turtle, displayed in the graphics window (main, or one of the graphics tabs).
The grahical representation of the plot is a visualisation of :data
variable content. Data
collection contained in this variable may be visualised in one of several modes (y = f(x), color = f(x,y), ...) depending on the configuration
set in :style
variable. Both variables, :data
and :style
are automaticaly initialized for each plot.
Constructing the plot
New plot is created with newplot
or
anewplot
instruction. The instruction list or function provided
to these plot constructors should fill the :data
variable as well as configure the mode and style
of data visualisation (initial value of :data
is just a single point: {0, 0}, displayed according
to the :style
settings), as in the following example:
Example 1:
"p := newplot "FunctionPlot [
"data := genarray 720 [
let "x 2 * m_pi * :index / 360
let "y (radsin :x) / :x
array :x :y
]
:style,"mode := "line
setr 0
]
:functionplot_canvas_cfg,"min_x := 0
:functionplot_canvas_cfg,"max_x := 4 * m_pi
:functionplot_canvas_cfg,"min_y := -0.4
:functionplot_canvas_cfg,"max_y := 1.1
Plots will display data available in other objects in the most of practical cases. Library of classes and functions make_plots.l
facilitates implementation of the usual usage of plots. All make_XX
functions in this library return plots derived from
the base class:
to plot_base
to get_myself op this end
to set_data :d "data := :d refresh end
to set_style :name :val
:style,:name := :val
end
end
Change (update) of displayed data is possible with set_data :d
function of the above class; plot style properties may be changed
with set_style :name :val
function. make_XX
functions use asynchronous plot constructors to speed up
creation of multiple plots. In order to allow synchronization the new plots are returned as promises
(get_myself
function) - any operation (e.g. print
) using such variable will wait until
the value (plot in thic case) is ready to use.
Line plot y=f(x)
make_line :data [:c color] [:s 1] [:r 0] [:name "Plot]
Creates line plot of y=f(x). Consecutive data points are interconnected with a line. Optional parameters of make_line
function
have the following meaning: :c
- line color, by default it is the next color in the plot colors table; :s
- line
thickness; :r
- size of points, default value of 0 disables data point drawing; :name
- name of the window, where the new
plot is added.
Example 2:
The same plot as in Example 1, but created with make_line
function; displayed range is adjusted with
setrange
function, also available in the make_plots.l
library.
include "make_plots.l
"fxy := genarray 720 [
let "x 2 * m_pi * :index / 360
let "y (radsin :x) / :x
array :x :y
]
"p := make_line :fxy
setrange world @ :p {0 12} {-0.4 1.1}
Scatter plot of {x,y} points
make_xy :data [:c color] [:s "none] [:r 0.5] [:img "fcircle] [:name "Plot]
Creates scatter plot of {x,y} points. Points are not connected with line, and the order of points is not relevant. Additional properties can be
visualised as a size or color of points. Optional parameters of make_xy
function are followig: :c
- color of points, if
the argument is a number, it is used as index of the column of values used as color, otherwise the argument should be a array of color components
or a name of constant color; :s
- index of column of values used to calculate point sizes relative to the maximum size; :r
- maximum size of points; :img
- name of points symbol; :name
- name of the window, where the new plot is added.
Example 3:
include "make_plots.l
to frnd
let "x 90 + rnorm 90
let "y (rnorm 1) * sin :x
let "z abs :y
let "u abs :z - (abs sin :x)
op (array :x :y :z :u)
end
"d := genarray 5000 [frnd]
"p := (make_xy :d "purple 4 5 "dot)
(set_style "opacity 3) @ :p
setrange "Plot {-150 320} {-3 3}
Matrix Ay,x=f(x,y) plot
make_2d :data [:c color] [:s "none] [:r 1] [:img "patch] [:name "Plot]
Matrix rows are stored in the :data
variable as collections of items, where each item may be a
single value or a vector of multiple properties visualised on the plot. Optional parameters of make_2d
function are following:
:c
- color of displayed points, if this argument is a number, it is used as an index to the value in the properties vector which
is used as color, otherwise the argument should be a array of color components or a name of constant color; :s
- index to the value
in the properties vector which is used to calculate size of points relative to the maximum size; :r
- distance between points in
columns and rows of the matrix; :img
- name of the symbol used to draw points; :name
- name of the window, where the
new plot is added.
Example 4:
include "make_plots.l
to fmatrix :m :n
let "mx (newarray :m newarray :n)
for [r 1 :m] [
for [c 1 :n] [:mx,:r,:c := (cos 0.03*:r^2) * (sin 0.02*:c^2)]
]
op :mx
end
"h := 100 "w := 200
"p := (make_2d fmatrix :h :w 1)
setrange "Plot array 0 :w array 0 :h
Set range displayed in the window
setrange :name :rx :ry
Set X and Y axis range displayed in the grahics window :name
(note, the name of the main graphics window is pool). The
new ranges should be provided as a 2-element collections composed of minimum and maximum value, :rx
for X axis and :ry
for Y axis, as it is shown in examples 2-4.
Automatic range adjusment
autoscale :name [:axes "xy]
Rage displayed in the window :name
may be adjusted automatically with the autoscale
function in order to contain
all data points. Both X and Y axes are adjusted by default. Optional parameter :axes
allows to select which axis should be adjusted,
in this case parameter should contain letter corresponding to the selected axis.
Example 5:
include "make_plots.l
"d := genarray 5000 [array rnorm 100 rnorm 150]
"p := (make_xy :d "purple "none 1)
autoscale "Plot
See also:
Turtle - plot - object
Synchronization of multiple objects
newp, newplot - create plot (synchronous constructor)
anewp, anewplot - create plot (asynchronous constructor)
histogram - calculate histogram (includes examples of histogram plots)
Table of content