Search code examples
pythonvectorgnuplot

How to use gnu plot to plot circles with vectors indicating direction?


I have a data file like this:

1  1.680  24.250 0.1 0.0  1.830  24.250
2  6.330  24.860 0.1 30.0  6.460  24.935
3  9.440  24.320 0.1 60.0  9.515  24.450
4  12.110  25.790 0.1 -30.0  12.240  25.715

representing 4 circles with the same radius of 0.1 meters. The format is:

number x y radius angle offset-x offset-y

The objective is to draw the 4 circles with arrows at the offset-x and offset-y at the indicated angle and have all of the arrows be the same length. I have examined a lot of examples, but haven't been able to extract what I'm trying to do from them.

Here is my gnu plot code:

#!/usr/local/bin/gnuplot
reset
set term pngcairo dashed font "Times,20" size 1920,1080

set loadpath "/Users/woo/.gnuplotting"
load 'default.plt'

set output 'avatars.png'

set grid
set xlabel "X" font "Times Bold,20"
set ylabel "Y" font "Times Bold,20"
set key outside right top vertical

xf(theta) = 0.1*cos(theta/180.0*pi)
yf(theta) = 0.1*sin(theta/180.0*pi)

plot "plot_file.txt" using 2:3:4 with circles, \
  "plot_file.txt" using 6:7:xf(5):yf(5)  \
   with vectors head size 0.1,20,60 filled

the 0.1 was to try to reduce the sizes of the vectors. I am getting the circles, but the offsets are not the same and should be. my offsets are at a radius of 0.15 meters supposedly. But, all of the vectors are at growing lengths and do not point in the specified directions.

Circles with vectors showing direction but not magnitude

I have a feeling that the solution is obvious, but I can't find it.


Solution

  • The main problem was the using part for the vectors:

    xf(theta) = 1.0*cos(theta/180.0*pi)
    yf(theta) = 1.0*sin(theta/180.0*pi)
    
    plot "plot_file.txt" using 2:3:4 with circles,\
         "plot_file.txt" using 6:7:(xf($5)):(yf($5)) \
          with vectors head size 0.1,20,60 filled
    

    With an expression, write $5 instead of 5. Also, the expression should be in parenthesis: using 6:7:(xf($5)):(yf($5)) instead of 6:7:xf(5):yf(5).

    I also changed 0.1 to 1.0 because I wanted unit vectors.

    This is the result:

    enter image description here

    Every arrow is really the same length but it doesn't look that way because the x- and y-axes don't have the same scaling. To make the two axes have to same scaling, do

    set size ratio -1
    

    This is the result:

    enter image description here