Studio/Plot

From J Wiki
Jump to navigation Jump to search

The plot package is described here. Additional demos are available from the J system main menu under Studio... |Demos... |plot. To use the package, first enter:

   load 'plot'


Cantor Ternary Function

ctf i produces the i-th number whose base-3 representation consists only of 0 and 1. The plot of the first 5000 such numbers illustrates the characteristic staircase-shape of the Cantor function.

   ctf=: 3 #. #:
   plot ctf i.5000

Ctf1.png

See also OEIS A005836

Prime Number Race

Are there more primes equal to 3 mod 4 than primes equal to 1 mod 4?

It turns out that in the early going, the "3's mod 4" dominate, though the "1's mod 4" catch up, and the lead changes infinitely often.

The graph shows the part of the race between the 808000 and 812000-th primes, where the lead changes a few times. These primes are in the range given by:

   p: 808000 812000
12325063 12390403


   primerace=: [: +/\ _2 + 4 | p:
   plot 808000 }.each (;primerace) i.812000

Primerace1.png

See also OEIS A038698

3D Function Plot

The graph shows the real part of the gamma function (here derived from the factorial function) near the origin of the complex plane.

To avoid infinities, for example at gamma (_3,0), the calculated values are confined to the interval [_3,12].

   require 'trig numeric plot'
   gamma=: !@<:
   real=: {.@+.
   x=: steps _3.5 4.5 40
   y=: steps _1 1 40
   z=: real gamma x j./ y
   dat=: _3 >. 12 <. z
   'surface;noaxes;viewpoint _1 _2.5 1' plot dat

Gamma1.png

Combining different plot types

Cmbtypfig1.png
Fig. 1: MS Works plot

The following data, (taken from Elementary Statistics. Second edition by P. G. Hoel) was used by Keith Smillie to demonstrate simple linear regression in J in his two page Introduction to J brochure.

The brochure shows a plot (Fig. 1) of observed yields (y) at different levels of watering (x) and their regression line created in MS Works. In this example we will attempt to recreate the plot using J.

   X=: 12 18 24 30 36 42 48       NB. levels of watering
   Y=: 5.3 5.7 6.3 7.2 8 8.7 8.4  NB. yields
   ]b=: Y %. 1 ,. X               NB. regression coefficients
   plot X; b p. X                 NB. plot regression line
   NB. plot 12 48; 'b p. y'       NB. alternative plot format
Cmbtypfig2.png
Fig. 2:  plot X; b p. X
Cmbtypfig3.png
Fig. 3:  options plot X; b p. X

Now lets add some of the missing text to the plot.

   xlbl=: 'Water (in.)'
   ylbl=: 'Yield (bu.)'
   tittxt=: 'Yield vs Water'
   options=: 'title ',tittxt,';xcaption ',xlbl,';ycaption ',ylbl
   options plot X; b p. X         NB. plot regression line with text labels

Ok, we're getting there now, but we need to add the series of observed yields to get an idea of how well our regression fits the data.

   keytxt=: 'Obs.,Est.'
   options=: options,';key ',keytxt
   options plot X ; Y ,: b p. X    NB. plot regression line and observed yields
Cmbtypfig4.png
Fig. 4:  options plot X ; Y ,: b p. X

The plot would be much clearer if, as in the original MS Works plot, the observed yields were plotted as points, but the regression as a line. However we can't use the plot verb to get different plot types for series on the same plot, instead we need to use the more powerful underlying verb pd.

Using pd we can build up the elements of the plot step by step giving us much more flexibility as to which options apply to which series.

It is possible to run the list of commands below in a J session one by one to create the plot, however wrapping the commands in a verb (in this case called plot_yldvwater0) that we define in a script file makes it much easier to experiment with changes. Just make the desired change, reload the script and run the verb. File:Combplottype.ijs


Cmbtypfig5.png
Fig. 5:  X plot_yldvwater0 Y
require 'plot'
X=: 12 18 24 30 36 42 48       NB. levels of watering
Y=: 5.3 5.7 6.3 7.2 8 8.7 8.4  NB. observed yields

plot_yldvwater0=: 4 : 0
  b=. y %. 1 ,. x              NB. regression coeffs for straight line
  xlbl=. 'Water (in.)'
  ylbl=. 'Yield (bu.)'
  tittxt=. 'Yield vs Water'
  keytxt=. 'Obs.,Est.'
  pd 'reset'
  pd 'title ',tittxt
  pd 'xcaption ',xlbl
  pd 'ycaption ',ylbl
  pd 'key ',keytxt
  pd 'keystyle marker'
  pd 'keycolor blue'
  pd 'keymarkers circle,line'
  pd 'type marker'
  pd 'markers circle'
  pd x;y
  pd 'type line'
  pd x;b p. x
  pd 'show'
)

To illustrate the effects of a number of the many plot options we can attempt to emulate the original MS Works plot. File:Combplottype.ijs

Cmbtypfig6.png
Fig. 6:  X plot_yldvwater1 Y
plot_yldvwater1=: 4 : 0
  b=. y %. x ^/ i. 2
  xlbl=. 'Water (in.)'
  ylbl=. 'Yield (bu.)'
  tittxt=. 'Yield vs Water'
  keytxt=. 'Obs.,Est.'
  pd 'reset'
  pd 'title ',tittxt
  pd 'xcaption ',xlbl
  pd 'ycaption ',ylbl
  pd 'frame 0'                 NB. no rectangular frame
  pd 'axes 1 1'                NB. but the show x & y axes
  pd 'grids 0 0'               NB. no gridlines
  pd 'key ',keytxt
  pd 'keystyle mho'            NB. marker,horizontal,open
  pd 'keypos tco'              NB. top,center,out
  pd 'keymarkers circle,line'  NB. specify markers for the key
  pd 'ticstyle out'            NB. tics are outside the plot
  pd 'xticpos 12 16 20 24 28 32 36 40 44 48'
  pd 'yticpos 5 6 7 8 9'       NB. define values to have tics at
  pd 'itemcolor black'         NB. set color to use until changed
  pd 'type marker'             NB. use marker type for series
  pd 'markersize 1.2'          NB. set size of markers
  pd 'markers circle'          NB. use circle as the marker
  pd x;y                       NB. plot the observed points
  pd 'type line'               NB. set line type for series
  pd 'pensize 1.1'             NB. set size of pen to draw lines
  pd 12 48; (":b),' p. y'      NB. Use function plot syntax for smoother curves
  pd 'show'
)

We could also re-parameterize the verb to enable us to investigate fitting higher degree polynomicals to the data by changing the left argument to plot_yldvwater.

Cmbtypfig7.png
Fig. 7:  2 plot_yldvwater X;Y to fit quadratic
Cmbtypfig8.png
Fig. 8:  3 plot_yldvwater X;Y to fit cubic
plot_yldvwater=: 4 : 0
  polydegree=. x
  'X Y'=. y
  b=. Y %. X ^/ i. >: polydegree
  xlbl=. 'Water (in.)'
  ylbl=. 'Yield (bu.)'
  tittxt=. 'Yield vs Water'
  keytxt=. 'Observed,"Predicted yield"'
  scolors=. ;:'blue darkgreen red orange green'  NB. series colours
  smarkers=. ;:'circle times square plus diamond triangle line'
  pd 'reset'
  pd 'title ',tittxt
  pd 'xcaption ',xlbl
  pd 'ycaption ',ylbl
  pd 'yticpos 5 6 7 8 9' NB. specify positions of tics on y axis
  pd 'key ',keytxt
  pd 'keystyle mvo'      NB. marker,vertical,open
  pd 'keypos bri'        NB. bottom,right,inside
  pd 'keymarkers ', ','&joinstring 0 _1 {smarkers
  pd 'keycolor ', ','&joinstring scolors
  pd 'itemcolor ', ','&joinstring scolors
  pd 'type marker'
  pd 'markersize 1.2'
  pd 'markers ', ','&joinstring 0{smarkers
  pd X;Y
  pd 'type line'
  pd 'itemcolor ', ','&joinstring }.scolors
  pd 'pensize 1.7'
  pd ((<./ , >./) X); (":b),' p. y' NB. Use function plot syntax for smoother curves
  pd 'show'
)

Note 'Commands to create plots'
  X plot_yldvwater0 Y
  X plot_yldvwater1 Y  
  2 plot_yldvwater  X;Y  NB. investigate degree of polynomial fit
)