Skip to content

Latest commit

 

History

History
304 lines (281 loc) · 20.4 KB

gnuplot.md

File metadata and controls

304 lines (281 loc) · 20.4 KB

Gnuplot from J

Although a number of plotting options are available in J and they are well documented in its wiki I will opt for direct calling gnuplot from J. The reason behind this is that I want to have every option available in gnuplot and wield complete control over it. Also because I work with many languages I prefer to reuse my knowledge of gnuplot rather than rely on, very often not so sophisticated, many graphical solutions provided.

Let's say we have the following CSV, toplot.csv:

$ cat toplot.csv
date,quote,symbol
2000-01-03,6.58,DGS10
2000-01-04,6.49,DGS10
2000-01-05,6.62,DGS10
2000-01-06,6.57,DGS10
2000-01-07,6.52,DGS10
2000-01-10,6.57,DGS10

We can use the heredocs feature introduced in gnuplot 5 which allows embedding the data in the same file as plot's setting and driver. Example, below

$ cat toplot.gp
$d << EOD
date,quote,symbol
2000-01-03,6.58,DGS10
2000-01-04,6.49,DGS10
2000-01-05,6.62,DGS10
2000-01-06,6.57,DGS10
2000-01-07,6.52,DGS10
2000-01-10,6.57,DGS10
EOD
set datafile separator comma
set xdata time
set timefmt "%y-%m-%d"
set format x "%m/%d"
plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"

In order to invoke the above script the following command can be used. As a result, the gnuplot script will be invoked (of course gnuplot is required to be installed) in a persistent mode in a separate window, one can look at the plot and optionally save it.

$ gnuplot -p -c toplot.gp

One should see the following figure

image

My prefered way of visual exploration of data using J will be creating gnuplot file, like toplot.gp, and then invoking it. Afterwards, optionally, changing the settings, data, and invoking again until I get the desired result. At some point I will optionally save the figure and the corresponding *.gp script if it is worth persisting or documenting. This is what is needed to accomplish that flow in J in a very straightforward way.

   NB. we have table
   ]t
┌──────────┬─────┬──────┐
│date      │quote│symbol│
├──────────┼─────┼──────┤
│2000-01-036.58 │DGS10 │
│2000-01-046.49 │DGS10 │
│2000-01-056.62 │DGS10 │
│2000-01-066.57 │DGS10 │
│2000-01-076.52 │DGS10 │
│2000-01-106.57 │DGS10 │
└──────────┴─────┴──────┘

   NB. we can transform it to grid form (see j/analysis.ijs)
   ]g=: toGridFromTable t
┌──────────┬─────┬──────┐
│date      │quote│symbol│
├──────────┼─────┼──────┤
│2000-01-036.58 │DGS10 │
├──────────┼─────┼──────┤
│2000-01-046.49 │DGS10 │
├──────────┼─────┼──────┤
│2000-01-056.62 │DGS10 │
├──────────┼─────┼──────┤
│2000-01-066.57 │DGS10 │
├──────────┼─────┼──────┤
│2000-01-076.52 │DGS10 │
├──────────┼─────┼──────┤
│2000-01-106.57 │DGS10 │
└──────────┴─────┴──────┘

   NB. we can invoke any linux command in J
   NB. here creating file temp.gp with one line
   2!:0 'echo ''$d << EOD'' > temp.gp'

   NB. now we can append the grid
   g appenddsv 'temp.gp';',';''
150

   NB. appending lines
   2!:0 'echo ''EOD'' >> temp.gp'

   2!:0 'echo ''set datafile separator comma'' >> temp.gp'

   2!:0 'echo ''set xdata time'' >> temp.gp'

   2!:0 'echo ''set timefmt "%y-%m-%d"'' >> temp.gp'

   2!:0 'echo ''set format x "%m/%d"'' >> temp.gp'

   2!:0 'echo ''plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"'' >> temp.gp'

After that we have the above toplot.gp file recreated

$ cat temp.gp
$d << EOD
date,quote,symbol
2000-01-03,6.58,DGS10
2000-01-04,6.49,DGS10
2000-01-05,6.62,DGS10
2000-01-06,6.57,DGS10
2000-01-07,6.52,DGS10
2000-01-10,6.57,DGS10
EOD
set datafile separator comma
set xdata time
set timefmt "%y-%m-%d"
set format x "%m/%d"
plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"

We can now load it in gnuplot in J console

  2!:0 'gnuplot -p -c temp.gp'
  NB. gnuplot is spawn and we see the same figure

Alternatively we can use functionality from j/analysis.ijs which is doing exactly the same

   ]cmds=:'set datafile separator comma';'set xdata time;set timefmt "%y-%m-%d"';'set format x "%m/%d"';'plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"'
┌────────────────────────────┬─────────────────────────────────────┬────────────────────┬─────────────────────────────────────────────────────────────────────┐
│set datafile separator comma│set xdata time;set timefmt "%y-%m-%d"│set format x "%m/%d"│plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"│
└────────────────────────────┴─────────────────────────────────────┴────────────────────┴─────────────────────────────────────────────────────────────────────┘
   ]toplot=:(<cmds),<g
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────┐
│┌────────────────────────────┬─────────────────────────────────────┬────────────────────┬─────────────────────────────────────────────────────────────────────┐│┌──────────┬─────┬──────┐│
││set datafile separator comma│set xdata time;set timefmt "%y-%m-%d"│set format x "%m/%d"│plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"│││date      │quote│symbol││
│└────────────────────────────┴─────────────────────────────────────┴────────────────────┴─────────────────────────────────────────────────────────────────────┘│├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-036.58 │DGS10 ││
│                                                                                                                                                               │├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-046.49 │DGS10 ││
│                                                                                                                                                               │├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-056.62 │DGS10 ││
│                                                                                                                                                               │├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-066.57 │DGS10 ││
│                                                                                                                                                               │├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-076.52 │DGS10 ││
│                                                                                                                                                               │├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-106.57 │DGS10 ││
│                                                                                                                                                               │└──────────┴─────┴──────┘│
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────┘
   fromGridToGnuplot toplot

   NB. Or alteratively using table
   ]toplot=:(<cmds),<t
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────┐
│┌────────────────────────────┬─────────────────────────────────────┬────────────────────┬─────────────────────────────────────────────────────────────────────┐│┌──────────┬─────┬──────┐│
││set datafile separator comma│set xdata time;set timefmt "%y-%m-%d"│set format x "%m/%d"│plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):2 w lp lw 3 title "DGS10"│││date      │quote│symbol││
│└────────────────────────────┴─────────────────────────────────────┴────────────────────┴─────────────────────────────────────────────────────────────────────┘│├──────────┼─────┼──────┤│
│                                                                                                                                                               ││2000-01-036.58 │DGS10 ││
│                                                                                                                                                               ││2000-01-046.49 │DGS10 ││
│                                                                                                                                                               ││2000-01-056.62 │DGS10 ││
│                                                                                                                                                               ││2000-01-066.57 │DGS10 ││
│                                                                                                                                                               ││2000-01-076.52 │DGS10 ││
│                                                                                                                                                               ││2000-01-106.57 │DGS10 ││
│                                                                                                                                                               │└──────────┴─────┴──────┘│
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────┘
   fromTableToGnuplot toplot

Let's investigate several examples of different types of plots that are common (here we assume we have data structured as CSV). The example data looks like follows (these are quotes for 52W, 5Y and 10Y goverment bonds, for both US and Japan, quoted in the same three week period) extracted from US and Japan central bank sites. The corresponding file is in datasets/bonds.csv

   ]scriptdir=: './j/'
./j/
   0!:1 < scriptdir,'analysis.ijs'
   ]bonds=: toTableFromCSV './datasets/bonds.csv'
┌──────────┬───────┬─────┬───────┐
│date      │quote  │tenor│country│
├──────────┼───────┼─────┼───────┤
│2022-05-30-0.0860│1Y   │JP     │
│2022-05-31-0.0840│1Y   │JP     │
│2022-06-01-0.0840│1Y   │JP     │
│2022-06-02-0.0860│1Y   │JP     │
│2022-06-03-0.0830│1Y   │JP     │
│2022-06-06-0.0800│1Y   │JP     │
│2022-06-07-0.0830│1Y   │JP     │
│2022-06-08-0.0850│1Y   │JP     │
│2022-06-09-0.0830│1Y   │JP     │
│2022-06-10-0.0900│1Y   │JP     │
│2022-06-13-0.0640│1Y   │JP     │
│2022-06-14-0.0820│1Y   │JP     │
│2022-06-15-0.0860│1Y   │JP     │
│2022-06-16-0.1060│1Y   │JP     │
│2022-06-17-0.0950│1Y   │JP     │
│2022-05-300.0000 │5Y   │JP     │
│2022-05-310.0040 │5Y   │JP     │
│2022-06-01-0.0040│5Y   │JP     │
│2022-06-020.0000 │5Y   │JP     │
│2022-06-03-0.0090│5Y   │JP     │
│2022-06-06-0.0040│5Y   │JP     │
│2022-06-070.0000 │5Y   │JP     │
│2022-06-08-0.0100│5Y   │JP     │
│2022-06-09-0.0100│5Y   │JP     │
│2022-06-10-0.0040│5Y   │JP     │
│2022-06-130.0350 │5Y   │JP     │
│2022-06-140.0700 │5Y   │JP     │
│2022-06-150.0700 │5Y   │JP     │
│2022-06-160.0380 │5Y   │JP     │
│2022-06-170.0480 │5Y   │JP     │
│2022-05-300.2300 │10Y  │JP     │
│2022-05-310.2410 │10Y  │JP     │
│2022-06-010.2350 │10Y  │JP     │
│2022-06-020.2400 │10Y  │JP     │
│2022-06-030.2350 │10Y  │JP     │
│2022-06-060.2400 │10Y  │JP     │
│2022-06-070.2450 │10Y  │JP     │
│2022-06-080.2450 │10Y  │JP     │
│2022-06-090.2490 │10Y  │JP     │
│2022-06-100.2500 │10Y  │JP     │
│2022-06-130.2550 │10Y  │JP     │
│2022-06-140.2560 │10Y  │JP     │
│2022-06-150.2520 │10Y  │JP     │
│2022-06-160.2510 │10Y  │JP     │
│2022-06-170.2250 │10Y  │JP     │
│2022-05-312.0530 │1Y   │US     │
│2022-06-012.1230 │1Y   │US     │
│2022-06-022.1260 │1Y   │US     │
│2022-06-032.1620 │1Y   │US     │
│2022-06-062.1960 │1Y   │US     │
│2022-06-072.2060 │1Y   │US     │
│2022-06-082.2450 │1Y   │US     │
│2022-06-092.3000 │1Y   │US     │
│2022-06-102.5070 │1Y   │US     │
│2022-06-132.7990 │1Y   │US     │
│2022-06-143.0520 │1Y   │US     │
│2022-06-152.9640 │1Y   │US     │
│2022-06-162.8850 │1Y   │US     │
│2022-06-172.8870 │1Y   │US     │
│2022-05-302.8053 │5Y   │US     │
│2022-05-312.8207 │5Y   │US     │
│2022-06-012.9160 │5Y   │US     │
│2022-06-022.9111 │5Y   │US     │
│2022-06-032.9371 │5Y   │US     │
│2022-06-063.0368 │5Y   │US     │
│2022-06-072.9906 │5Y   │US     │
│2022-06-083.0355 │5Y   │US     │
│2022-06-093.0702 │5Y   │US     │
│2022-06-103.2637 │5Y   │US     │
│2022-06-133.4820 │5Y   │US     │
│2022-06-143.6013 │5Y   │US     │
│2022-06-153.3734 │5Y   │US     │
│2022-06-163.2860 │5Y   │US     │
│2022-06-173.3436 │5Y   │US     │
│2022-05-302.8097 │10Y  │US     │
│2022-05-312.8495 │10Y  │US     │
│2022-06-012.9113 │10Y  │US     │
│2022-06-022.9131 │10Y  │US     │
│2022-06-032.9405 │10Y  │US     │
│2022-06-063.0399 │10Y  │US     │
│2022-06-072.9791 │10Y  │US     │
│2022-06-083.0270 │10Y  │US     │
│2022-06-093.0455 │10Y  │US     │
│2022-06-103.1649 │10Y  │US     │
│2022-06-133.3617 │10Y  │US     │
│2022-06-143.4791 │10Y  │US     │
│2022-06-153.2915 │10Y  │US     │
│2022-06-163.1952 │10Y  │US     │
│2022-06-173.2313 │10Y  │US     │
└──────────┴───────┴─────┴───────┘

Let's plot 5Y for US bond only.

   settings=:'set datafile separator comma';'set xdata time;set timefmt "%y-%m-%d"';'set format x "%m/%d"'
   plot=:'plot $d skip 1 u (timecolumn(1,"%Y-%m-%d")):( (stringcolumn(3) eq "5Y")  && (stringcolumn(4) eq "US") ? $2 : NaN) w l lw 3 t "5Y US"'
   cmds=:settings , <plot
   toplot=:(<cmds),<bonds
   fromTableToGnuplot toplot

The plot created is below

image

Let's now plot 5Y tenor for both US and JP.

   plotline1=:'$d skip 1 u (timecolumn(1,"%Y-%m-%d")):( (stringcolumn(3) eq "5Y")  && (stringcolumn(4) eq "US") ? $2 : NaN) w l lw 3 t "5Y US"'
   plotline2=:'$d skip 1 u (timecolumn(1,"%Y-%m-%d")):( (stringcolumn(3) eq "5Y")  && (stringcolumn(4) eq "JP") ? $2 : NaN) w l lw 3 t "5Y JP"'
   plot=:'plot ',plotline1,', ',plotline2
   cmds=:settings , <plot
   toplot=:(<cmds),<bonds
   fromTableToGnuplot toplot

The plot created is below

image