Search code examples
gnuplotcolumnheader

How do I use the columnhead (string) in a data file on the x-axis as a xiticlabel of a plot with boxes? (Gnuplot)


I´m new here and this is my first question, hope my Problem is described properly according to our rules in here...

I´ve got a data file (datafile.dat) which is used to create several plots (see below):

temp name1  name2
10   1000   1200
22   800    750
50   250    200
100  80     82
107  5      3

What I want to do is to create a plot with the values in the second and third column plotted with boxes. On the x-axis the names these values refer to shall be displayed. In Addition it shall be possible to give each of the boxes a specific colour. An additional Advantage would be that the solution can also be used in a Loop (because the original data file contains a lot more columns...). In the end I want the graph to look something like this: Desired Layout of the plot.

In order to get this I tried different things I found searching the Internet (see below). I am running gnuplot 5 on Windows with the following command file:

xticlabels If I try this e.g. for column 2 this doesn´t work:

plot 'datafile.dat' u 2:xticlabels(columnhead(2))

Using an external utility Didn´t work at all, failure message was produced

Stats Looks like a pretty good solution if I store the output in a variable. But I can´t get my code working (see below):

reset
set terminal postscript eps size 15 cm, 15 cm colour enhanced dashed "Times, 22"
set output "test.pdf"
stats 'datafile.dat' using 2
b = STATS_sum
plot 'datafile.dat' u 2:xticlabels(b) every ::1     
reset

What can I do to create the desired output from the data file above? I tried the Points mentioned above in many different combinations. Suggestion 1, Suggestion 2, Suggestion 3 are further Topic-related ideas to solve the Problem but I got none of these working. Can please anyone help me to get a solution? Any hints will be highly appreciated!!!

Thanks in advance!!!

Michael

EDIT: I found out that this question was already asked from someone else three years ago: Axis label and column header ...Is there maybe a solution today? Also: Question


Solution

  • I can see two methods for doing this. The first is more automatic, but has the disadvantage of not being able to do the colors.

    Method 1

    Using only one datapoint for each column (as your comment suggests you will be doing), we can almost accomplish this using the columnstacked histogram style. At this point, I'm not sure how to get different colors, as the columnstacked style applies colors to the sections of the stacks.

    Using, your example data, and the first line of data, we can do

    set style data histogram            # we could do w histograms in the plot command instead
    set style histogram columnstacked
    set boxwidth 0.9                    # so the boxes don't touch
    set style fill solid
    set key autotitle columnhead        # first row contains series name
    
    plot for[i=2:3] "datafile.dat" every ::0::0 u i 
    

    where every ::0::0 means use the 0th (first) line of data only.

    This produces

    enter image description here

    To plot columns 2 through 50, for example, just change the for[i=2:3] to for[i=2:50].

    Method 2

    We can do this by using the stats command to add the labels, and then do a standard plot command.

    To set the tic marks, we can do

    set xtics 1,1 format ""
    do for[i=2:3] {
        stats "datafile.dat" every ::0::0 u (a=strcol(i),1) nooutput
        set xtics add (a i-1)
    }
    

    The first command here sets the xtics to occur every 1 unit starting at 1 but suppresses the labels (we will be setting our own labels).

    We then loop over each column, reading the 0th line in the datafile with the stats command. When we read it, we store the columnheader in a variable a. We just return a 1 for the stats command to actually analyze. We actually don't care about the results of this command, we just need it to read the column headers. Finally, we use set xtics add to add this label as an xtic.

    Next, we can do some necessary set up commands

    set style fill solid
    set boxwidth 0.9      # so the boxes don't touch
    unset key
    set yrange[0:*]       # by default, the smallest boxes may be cut off
    

    Finally, we can plot with

    plot for[i=2:3] "datafile.dat" every ::1::1 u (i-1):i w boxes
    

    The result is

    enter image description here

    Again, the for loops can be changed to use any number of columns. X-ranges can be adjusted if desired, and linetype commands can be used in the plot command to set colors.


    We use every ::0::0 because the set key autotitle command causes the first line with the column headers to be ignored (processed before the plot command). Thus the first (0th) line is the first line of actual data.

    Note that here we use every ::1::1 because the 0th line is the column header line. Without the set key autotitle command, the first line is not automatically ignored.