Plotting arrows with gnuplot

后端 未结 3 604
轻奢々
轻奢々 2021-01-18 07:37

I have data generated in a simulation. The generated data file looks something like this:

1990/01/01 99
1990/01/02 92.7
1990/01/03 100.3
1990/01/04 44.2
1990         


        
3条回答
  •  萌比男神i
    2021-01-18 08:14

    If you didn't want the arrow head then you might try the impulses style (with impulses rather than with lines) (If you still want the lines on top then you can plot twice).

    If you really want the arrow heads then the following might help: It uses a for loop (or sorts) to add vertical arrows to a plot.

    Gnuplot script, for loop within or adding to existing plot

    Specifically:

    create a file simloop.gp which looks like the following:

    count  = count+1
    #save the count to count.gp
    system 'echo '.count.' > count.gp'
    #load the simloop shell
    system "./simloop.sh"
    
    #draw the arrow
    load 'draw_arrow.gp'
    
    if(count

    Then create a simloop.sh file that looks something like so

    #!/bin/bash
    
    #read the count
    count=$(awk -F, '{print $1}' count.gp)
    #read the file
    xcoord=$(awk -v count=$count -F, 'BEGIN{FS=" ";}{ if(NR==count) print $1}' simulation.dat)
    ycoord=$(awk -v count=$count -F, 'BEGIN{FS=" "}{ if(NR==count) print $2}' simulation.dat)
    dir=$(awk -v count=$count -F, 'BEGIN{FS=" "}{ if(NR==count) print $3}' simulation.dat)
    
    #choose the direction of the arrow
    if [ \"$dir\" == \"0\" ]; then
        echo '' > draw_arrow.gp
    fi
    
    if [ \"$dir\" == \"1\" ]; then
      echo 'set arrow from ' $xcoord' ,0 to '$xcoord','$ycoord' head' > draw_arrow.gp
    fi
    
    if [ \"$dir\" == \"2\" ]; then
     echo 'set arrow from '$xcoord',0 to '$xcoord','$ycoord' backhead' > draw_arrow.gp
    fi
    

    Then create a simulation.gp file that looks something like so:

    count = 0;
    max = 5;
    load "simloop.gp"
    set yrange[0:*]
    plot "simulation.dat" u 1:2 w l
    

    Make sure the shell file has executable permissions (chmod +wrx simloop.sh), load up gnuplot and type

    load "./simulation.gp"
    

    This worked for me with the data file

    1  99   0
    2  92.7 1
    3 100.3 2
    4 44.2  0
    5 71.23 1
    

    (For testing I got rid of the time formatting You should be able to put it back without too much trouble.)

    Then I got this graph: enter image description here

    Which I think is more or less what you want.

提交回复
热议问题