WLOG - notes something different

plotting (making graphs)

9front / plan9 provide plotting capabilities trough grap command.

Do note that this is just a preprocessor for pic, which is a preprocessor for troff. Which requires a post processor such as dpost to generate a PostScript file or GhostScript to generate a PDF file. There are some more extra steps to get an image out of it.

There is no "easy" way to plot a bunch of data and have it show in a graph. But using provided publishing tools with the system, a tool chain can be constructed to process and plot data.

All results can be viewed with the page command, which opens a provided multi format viewer.


The processing demonstrated here is just passing one type of macro in to a processor to another type of macro language.

Everything between lines containing .G1 and .G2 will be processed in to a graph. It has a lot of capabilities; such as variables, loops, control statements and even IO procedures to process and format the data withing the grap.

basic plot

So let's start with a basic grap macro with some data:

label top "Test Graph"
1 2
2 5
3 1
4 7
5 6

Save this in to a file, for this example test.gmac, or echo it into the chain.

basic plot with calculated and drawn average

Let's include some code in the macro to calculate and draw dotted average line. Note that we do the drawing and calculating in the copy until "END" thru block.

frame ht 2 wid 3
label top "Average test"
draw solid
i = sum = 0
minx = 1e12
maxx = -1e12
copy until "END" thru {
    next at $1, $2
    i = i + 1
    sum = sum + $2
    minx = min(minx,$1); maxx = max(maxx,$1);
-1 2
-2 5
-3 1
-4 3
-5 8
-6 7
-7 4
-8 7
-9 5
-10 2
line dashed from minx,(sum / i) to maxx,(sum / i)

Save this in to a file, for this example test.gmac, or echo it into the chain.

plot and view the graph

grap test.gmac | pic | troff | page

plot and save a postscript file

grap test.gmac | pic | troff | dpost >test.ps

plot and save a pdf

grap test.gmac | pic | troff | dpost | ps2pdf >test.pdf

plot and save a png image

grap test.gmac | pic | troff | dpost | gs -dSAFER -dNOPAUSE -dBATCH -q -s'DEVICE=plan9' -s'OutputFile=-' -f - | topng > test.png

plot and save a cropped png image with better quality

grap test.gmac | pic | troff | dpost | gs -dSAFER -dNOPAUSE -dBATCH -d'DEVICEWIDTHPOINTS=500' -d'DEVICEHEIGHTPOINTS=250' -q -s'DEVICE=plan9' -s'OutputFile=-' -r300 -f - | topng > test.png

processing in a script

When processing data and generating grap macros in a script, be careful, newlines are important!

To that effect, either do all in one echo '...' | grap | pic | troff | ... chain.

Or disable shell newline substitution when saving data into variables:

# disable newline substitution to save cmd result "as is"
# do stuff
data=`{cat /sys/log/x | awk '{print $1 " " $2}'}
echo ('.G1' $data '.G2') | grap | pic | troff | ...
## re enable newline substitution