Search code examples
plotgnuplotgnu

gnuplot strange behavior when not using "first" option


I was having trouble plotting a horizontal line

...
set arrow 2 from graph 0, y(x) to x, y(x) nohead
...

For clarity, assume that x = 1 => y = 3 So as far as I understood, that should have generated a line from (0,3) to (1, 3). However, the y coordinate of the first point (0,3) turned out to appear somewhere outside the plot. But if I use (according to a post here)

set arrow 2 from graph 0, first y(x) to x, y(x) nohead

then it yields the output I wanted.

Can someone explain to me the magic above using first?


Solution

  • It is worth reading help coordinates to learn about the different coordinate systems. In short, the first coordinate system is that defined by the current ranges of the x and y axes: the bottom left corner of the graph has coordinates (xmin,ymin), and the top right corner has coordinates (xmax, ymax). In the graph coordinate system the bottom left corner is always (0,0) and the top right corner is always (1,1), independent of the ranges of the two axes.

    Here is a brief example:

    set xrange [-4:4]
    set yrange [-3:3]
    set grid
    set arrow 1 from first 0,0 to first 1,1 ls 1 lw 3
    set arrow 2 from graph 0,0 to graph 1,1 ls 2 lw 3
    plot 1/0 ti ""
    

    enter image description here

    The purple vector is arrow 1, which goes from (0,0) to (1,1) in the first coordinate system. The second vector is arrow 2, which goes from (0,0) to (1,1) in the graph coordinate system.

    The default rules for which coordinate system will be used are

    If the coordinate system for x is not specified, first is used. If the system for y is not specified, the one used for x is adopted.

    and for the special case of set arrow,

    A coordinate system specifier does not carry over from the first endpoint description [to] the second.

    It sounds like you want to use the first coordinate system, so you shouldn't have to do anything:

    set arrow from 0, y(x) to x, y(x)
    

    When you use

    set arrow from graph 0, y(x) to x, y(x)
    

    you use the graph coordinate system for the starting point, and the first coordinate system for the end point.

    When you use

    set arrow from graph 0, first y(x) to x, y(x)
    

    you use the graph coordinate system for the starting point's x coordinate, and the first coordinate system for the remaining coordinates. If range of the x axis starts at zero, this would be the same as using the first coordinate system for everything.