Search code examples
printfgnuplot

gnuplot 'set title' with sprintf : representing angle in terms of fractions of pi


I'd like to run a gnuplot .inp file so all the angles in the script show up automatically in the title as fractions based on the Greek letter pi - instead of a decimal form for the angle. I already know how to use {/Symbol p}, but that is a manual intervention that is impractical in this case.

I have an example sprintf line in a gnuplot input file which can produce nice title information :

   angle=( (3*pi) /4 )
   set title sprintf ("the angle is %g radians", angle)
   plot sin(x)

... the output file (e.g. svg) or terminal (e.g. wxt) shows "2.35619", which is correct, however ; it would be nice to see the Greek letter for pi and the fraction itself, as is typically read off of a polar plot, e.g " 3/4 pi". Likewise for more complex or interesting representations of pi, such as "square root of two over two".

I already know I can manually go into the file and type in by hand "3{/Symbol p}/4", but this needs to be done automatically, because the actual title I am working with has numerous instances of pi showing up as a result of a setting of an angle.

I tried searching for examples of gnuplot being used with sprintf to produce the format of the angle I am interested in, and could not find anything. I am not aware of sprintf being capable of this. So if this is in fact impossible with gnuplot and sprintf, it will be helpful to know. Any tips on what to try next appreciated.

UPDATE: not a solution, but very interesting, might help :

use sprintf after the 'plot' to set the title that appears in the key (but not the overall title):

gnuplot setting line titles by variables

so for example here, the idea would be :

foo=20
plot sin(x)+foo t sprintf ("The angle is set to %g", foo)```

Solution

  • Here is an attempt to define a function to find fractions of Pi. Basically, sum (check help sum) is used to find suitable multiples/fractions of Pi within a certain tolerance (here: 0.0001). It is "tested" until a denominator of 32. If no integer number is found, the number itself is returned. In principle, the function could be extended to find multiples or fractions of roots, sqrt(2) or sqrt(3), etc. This approach can certainly be improved, maybe there are smarter solutions.

    Script:

    ### format number as multiple of pi
    reset session
    
    $Data <<EOD
     1.5707963267949
    -1.5707963267949
     6.28318530717959
     2.35619449019234
     2.0943951023932
    -0.98174770424681
     2.24399475256414
     1.0
     1.04
     1.047
     1.0471
     1.04719
    EOD
    
    set xrange[-10:10]
    set yrange[:] reverse
    set offset 0.25,0.25,0.25,0.25
    set key noautotitle
    
    dx = 0.0001
    fPi(x) = (_x=x/pi, _p=sprintf("%g",x), _d=NaN, sum [_i=1:32] \
             (_d!=_d && (abs(_x*_i - floor(_x*_i+dx)) < dx) ? \
             (_n=floor(_x*_i+dx),_d=_i, \
             _p=sprintf("%sπ%s",abs(_n)==1?_n<0?'-':'':sprintf("%d",_n),\
              abs(_d)==1 ? '' : sprintf("/%d",_d)),0) : 0 ), _p)
    
    plot $Data u (0):0:(fPi($1)) w labels font "Times New Roman, 16"
    ### end of script
    

    Result:

    enter image description here