Search code examples
gnuplotmedianerrorbar

gnuplot - adding median to plot with errorbars AND logscale'd x-axis


So I have some data files in format

x y ymin ymax

That I'm plotting with yerrorbars.

Now how would I best add a median of the y values to the plot running over the whole range of x?

UPDATE

I'm also plotting the x axis in logscale which seems to prevent using STATS.


Solution

  • Suppose that your data looks like this:

    1 8 6 9
    2 6 5 7
    3 5 4 8
    4 6 5 8
    

    We can use the stats command to find the median. The use is similar to the plot command. Here, we only need to do analysis of the second column, so we will only specify the second column:

    stats datafile u 2 nooutput
    

    The nooutput option tells the command not to print the results. If we wish to see the full analysis, we simply omit that specification. By default, the stats command stores its results in variables of the form STATS_*. We can use a different prefix if desired. See help stats for more details.

    At this point, we have a variable STATS_median that stores the median of the y values (which is 6 for the sample data). We can now add the median to the graph in one of two ways. First we can simply add a plot specification to the existing plot command:

    plot datafile u 1:2:3:4 with yerrorbars, STATS_median
    

    enter image description here

    or we can add a line with the set arrow command and then plot just the yerrorbars:

    set arrow 1 from graph 0, first STATS_median to graph 1, first STATS_median nohead
    plot datafile u 1:2:3:4 with yerrorbars
    

    enter image description here

    Here we give the x coordinate in graph units which range from 0 (the left side) to 1 (the right side) and the y coordinate in the first coordinate system which corresponds to the y1 axis. Specifying nohead says to not draw an arrow head. The 1 immediately following set arrow tags this arrow as arrow 1 so that we can change or remove it easily later.

    Other options are available. See help arrow for more details.