[nolan@nprescott.com] $  cat weblog archive feed

J Plotting Notes

2020-06-06

I only just finished complaining about the plotting facilities in J being under-documented and realized how to accomplish what I was trying to do. Just a few notes so I don't forget.

I still feel like this is all under-documented, but I don't think I understand it well enough yet to contribute much.

The Complaint

… plotting in J is just not very well documented. I spent a few minutes looking into the libraries plot and graph, but the sparse documentation didn't give me much hope.

Of course these things just eat at me if I don't figure them out so I spent some time digging through the J wiki and came up with the following solution to plotting polygons as in the prior post:

   load 'plot'
   ]V=. 0.5 0.5 ,. 1.5 0.5 ,. 0.5 1.5
0.5 1.5 0.5
0.5 0.5 1.5

   R=. (cos , -&sin) ,: (sin , cos)
   theta=. o. 1r3
   mmul=. +/ .*
   closeForm=. ([: >: $) $ ]
   polygon=. closeForm & j./
   translateCorner=. -&(0{"1 V)
   translateCenter=. -&(V +/ .* 1r3)

   pd 'reset'
   pd 'type line'
   pd polygon V
   pd polygon (R theta)&mmul V
   pd polygon (R theta)&mmul &.:translateCenter V
   pd polygon (R theta)&mmul &.:translateCorner V
   pd 'show'

Result

four triangles rotated
around the origin, a corner and the center

It turns out I was nearly there with my gnuplot solution. In the case of J's plot library it seems the easiest way to pass x-y coordinates for an arbitrary polygon is as complex numbers (why!?). I've read the docs a few times and I am almost certain it is possible to box them or pass them as 2-vectors, but I haven't managed to get that working.

As a result, I've got polygon, which invokes j./ (complex-insert) for a given matrix along with closeForm which wraps any given set of points to conclude on the first point. Both J and gnuplot require a polygon to "wrap" the points in order to close the form of a polygon.

I don't know that I'll be replacing gnuplot any time soon, but I'm at least mollified having worked out something as simple as this directly in J. As easy as it was to get things working in gnuplot, it is easier still to skip that step and tweak the plot directly at the J console.