astha
astha

Reputation: 613

How to set grid (vector or arrows) lines in gnuplot

The location of the grid lines (parallel to Y-axis) at X-axis are defined at third row of a files, for example.

The third lines has the points

#   0.00000000 0.08329780 0.11683890 0.20013670 0.23367770 

I can get the Ymax from another file defined as FILE with

set table $Dummy
plot FILE u ($0==1?(Ymax=$2): NaN) w table # i have updated this line. This will be used only for height of the grid line. Here FILE is a data file with two columns only which will be plotted in X-Y format.

unset table

How I can set grid lines at above locations that should end to Ymax ?

I need something like:

for i in 0.00000000 0.08329780 0.11683890 0.20013670 0.23367770
do
set arrow $i, Ymax lc rgb "black" dt 2 nohead 
done

Upvotes: 0

Views: 470

Answers (2)

theozh
theozh

Reputation: 25734

As @Ethan already pointed out, gridlines are bound to major or minor tics and span the whole graph. But you can plot something with vectors.

By the way, note that with your code

set table $Dummy
    plot FILE u ($0==1?(Ymin=$1,Ymax=$2):NaN,Xmax=$8) w table
unset table

Ymin and Ymax will be the values of of the first and second column of the second line(row) of the last dataset. If your data has no empty lines then the last dataset is also the first. Xmax will be the overall last value of the 8th column.

For you task, one solution could be the following below. No need for sed or awk, etc. Since I don't have example data from you I assume something.

  1. get the x positions for your "grid lines" from one datafile
  2. extract Ymin,Ymax,Xmax from another datafile
  3. plot your data with linespoints and the "grid lines" with vectors

Note that in earlier gnuplot versions there was a limitation of strcol() to (I guess) 63 characters. With gnuplot 5.2.7 this has been fixed.

Code:

### use vector plot to plot "grid lines"
reset session

$Data1 <<EOD
# first line
# second line
# 0.00000000 0.08329780 0.11683890 0.20013670 0.23367770
# below this line data starts
1  4
2  5
3  6
EOD

$Data2 <<EOD
1.1  2.7  0  0  1.2  0  0  0.00
1.2  2.6  0  0  1.8  0  0  0.05
1.3  2.5  0  0  2.5  0  0  0.10
1.4  2.4  0  0  2.1  0  0  0.15
1.5  2.3  0  0  1.6  0  0  0.17
1.6  2.2  0  0  1.7  0  0  0.20
1.7  2.1  0  0  2.4  0  0  0.25
EOD

set table $Dummy
    set datafile commentschars ''       # all lines will be data line
    set datafile separator '\n'         # in order to get full lines
    plot $Data1 u (xValues = strcol(1)) index 0 every ::2::2 w table   # get the complete 3rd line
    set datafile commentschars '#'      # reset the comment character
    set datafile separator whitespace   # reset the column separator
    plot t=0 $Data2 u (t==0?(Ymin=$1,Ymax=$2,t=1):NaN,Xmax=$8) w table  # get Ymin,Ymax,Xmax
unset table
print Ymin, Ymax, Xmax, xValues

xValue(n) = real(word(xValues,n+1))   # function to extract xValue

set xrange[-0.05:0.3]
set samples words(xValues)-1    # set number of datapoints of special datafile '+'

plot '+' u (xValue(int($0+1))):(Ymin):(0):(Ymax-Ymin) w vectors lc rgb "black" dt 2 nohead not, \
     $Data2 u 8:5 w lp pt 7 lc rgb "red" title "Data"
### end of code

Result:

enter image description here

Addition:

Above I've shown you how to extract the necessary values with gnuplot. Yes, that's not so easy to understand and not the shortest way, but it's gnuplot only! If you prefer to use sed, awk or the like feel free, but there I cannot help.

Another approach instead of plotting vectors is drawing arrows. The assumption is that you have your data in your variables already.

Code:

### draw arrows from a data string
reset session

xValues = "0.00000000 0.08329780 0.11683890 0.20013670 0.23367770"

Ymin = 0.2
Ymax = 0.9
Xmax = 0.25

i=0
do for [xValue in xValues] {
    i=i+1
    set arrow i from xValue,Ymin to xValue,Ymax nohead dt 2
}

set xrange[-0.05:0.4]
set yrange[0:1]

plot x
### end of code

Result:

enter image description here

Upvotes: 1

Ethan
Ethan

Reputation: 15093

Grid lines are placed at axis tick locations. The major ticks (tick level 0) and minor ticks (tick level 1) are tracked separately. The linetype of the grid lines can also be chosen. To generate thin blue vertical grid lines at the x coordinates you show using minor ticks to control the grid lines:

  set xtics add ( 0.00000000 1, 0.08329780 1, 0.11683890 1, 0.20013670 1, 0.23367770 1 )
  set grid mx lt 0, lt 1 lw 0.5 lc "blue"

As always see the documentation (help set xtics, help set grid) for more details.

Upvotes: 1

Related Questions