Studio/Plot

From J Wiki
Jump to: navigation, 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].

   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 %. X=. 1,"0 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 plotyvw) 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.

Cmbtypfig5.png
Fig. 5:  x plotyvw y

[{{#file: "combplottype.ijs"}} Download script: combplottype.ijs ]

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

plotyvw=: 4 : 0
   b=. y %. X=. 1,"0 x
   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'
)

Note 'Commands to create plot'
  x plotyvw y
  x plotyvw2 y
)

With the addition of a few more options we can get pretty close to emulating the original MS Works plot. Try changing the value of polyorder to investigate fitting higher order polynomials.

Cmbtypfig6.png
Fig. 6:  x plotyvw2 y with polyorder=. 1
Cmbtypfig7.png
Fig. 7:  x plotyvw2 y with polyorder=. 3

[{{#file: "combplottype.ijs"}} Download script: combplottype.ijs ]

plotyvw2=: 4 : 0
   b=. y %. X=. |: x^"1 0 i. >:polyorder=. 1
   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 frame
   pd 'axes 1 1'     NB. show x & y axes
   pd 'key ',keytxt
   pd 'keystyle mho' NB. marker,horizontal,open
   pd 'keypos tco'   NB. top,center,out
   pd 'keymarkers circle,line'
   pd 'ticstyle out'
   pd 'xticpos 12 16 20 24 28 32 36 40 44 48'
   pd 'yticpos 5 6 7 8 9'
   pd 'grids 0 0'    NB. no gridlines
   pd 'itemcolor black'
   pd 'type marker'
   pd 'markersize 1.2'
   pd 'markers circle'
   pd x;y
   pd 'type line'
   pd 12 48; (":b),' p. y' NB. Use function plot syntax for smoother curves
   pd 'show'
)