E.g. if I have a graph and want to add vertical lines at every 10 units along the X-axis.
-
possible duplicate of [Gnuplot: Vertical lines at specific positions](http://stackoverflow.com/questions/4499998/gnuplot-vertical-lines-at-specific-positions) – Benjamin Gruenbaum Jul 02 '13 at 05:44
5 Answers
From the Gnuplot documentation:
To draw a vertical line from the bottom to the top of the graph at
x=3
, use:
set arrow from 3, graph 0 to 3, graph 1 nohead
Here is a snippet from my perl script to do this:
print OUTPUT "set arrow from $x1,$y1 to $x1,$y2 nohead lc rgb \'red\'\n";
As you might guess from above, it's actually drawn as a "headless" arrow.

- 5,887
- 1
- 30
- 22
-
11Thanks! Just for the benefit of the total n00bs and to be pedantic, the complete example to draw a vertical line at x=1 spanning from y=0 to y=100, would be just: set arrow from 1,0 to 1,100 nohead lc rgb 'red' – JJC Dec 06 '13 at 01:53
alternatively you can also do this:
p '< echo "x y"' w impulse
x and y are the coordinates of the point to which you draw a vertical bar

- 488
- 1
- 4
- 15
-
This seems like the cleaner way of doing it. If you want to draw multiple vertical lines, you can also use the `'-'` dummy file – hertzsprung Jul 28 '14 at 11:30
-
This gives `warning: Skipping data file with no valid points x range is invalid`. Not sure why though. – con-f-use Jan 20 '18 at 15:53
You can use the grid
feature for the second unused axis x2
, which is the most natural way of drawing a set of regular spaced lines.
set grid x2tics
set x2tics 10 format "" scale 0
In general, the grid is drawn at the same position as the tics on the axis. In case the position of the lines does not correspond to the tics position, gnuplot provides an additional set of tics, called x2tics
. format ""
and scale 0
hides the x2tics so you only see the grid lines.
You can style the lines as usual with linewith
, linecolor
.

- 246
- 2
- 3
-
Additional note: if you use x2tics to control the grid, you have to make sure that the x2axis ranges over the same values as the xaxis. It is not guaranteed if you use auto scale. Fixed with an explicit set x2range[x:y]. – Ben May 14 '17 at 09:09
To elaborate on previous answers about the "every x units" part, here is what I came up with:
# Draw 5 vertical lines
n = 5
# ... evenly spaced between x0 and x1
x0 = 1.0
x1 = 2.0
dx = (x1-x0)/(n-1.0)
# ... each line going from y0 to y1
y0 = 0
y1 = 10
do for [i = 0:n-1] {
x = x0 + i*dx
set arrow from x,y0 to x,y1 nohead linecolor "blue" # add other styling options if needed
}

- 19,520
- 4
- 51
- 74