2

I have CSV file with the following structure:

headerString1;headerString2;...;headerStringN
doubleNumber1;doubleNumber2;...;doubleNumberN
... many other doubleNumberRows

I want to plot histograms from each of the columns in individual files - this is something that works - and I want to take the title for each individual plot from the CSV-file, first row. I searched a lot, but could find a solution. Up to now this is my gnuplot code:

set datafile separator ";"
set style data histogram

binwidth=20
set boxwidth binwidth-2
bin(x,width)=width*floor(x/width)

# No legends!
unset key

do for [COL=1:10] {
    set title sprintf("%d", columnheader(COL)) <--- This always tells me it is a number, "%s" does not work
    FILE = sprintf("%s%02d%s","Histogram",COL,".png")
    set term png giant font helvetica 24 size 1440, 1080
    set output FILE
    plot "myCSVFile.CSV" using (bin(column(COL),binwidth)):(1.0) smooth freq with boxes lc 1
}

columnheader(COL) is a number (?), at least I can convert it via sprintf("%d", columnheader(COL)) to a number string which is "-2147483648" for all plots. The output looks like this:

enter image description here

How do I retrieve the headerString# strings and use it as title in my individual plot?

WolfiG
  • 1,059
  • 14
  • 31
  • Maybe this link will help: https://stackoverflow.com/questions/36501353/how-do-i-use-the-columnhead-string-in-a-data-file-on-the-x-axis-as-a-xiticlabe – Vinicius Placco Apr 05 '18 at 11:50
  • Nope, unfortunately not. This example (like the others I saw) shows how to use the column headers to label series joined to one plot. I want to treat the series one by one and plot these to one individual file each, with the title of the DIAGRAM (not the series) given by the column header. – WolfiG Apr 05 '18 at 16:23

2 Answers2

5

You have access to the columnhead string only in very specific contexts, for example within a plot command. Setting the plot title is not one of them (set title doesn't even know which data file you are going to be using), but creating legend entries is. So you could position the legend where the title usually appears.

For example, given the datafile test.csv

First Column;Second Column
-900;-700
-1100;-800
-1000;-650

you could use

set term push

set datafile separator ";"
set style data histogram
set style fill solid 1

binwidth=20
set boxwidth binwidth-2
bin(x,width)=width*floor(x/width)

set key outside top center samplen 0

do for [COL=1:2] {
    FILE = sprintf("%s%02d%s","Histogram",COL,".png")
    set term pngcairo
    set output FILE

    plot "test.csv" using (bin(column(COL),binwidth)):(1.0) smooth freq notitle with boxes lc 1, \
         NaN title columnhead(COL) with lines lc rgb "white"

    set output
}

set term pop

and get

enter image description here enter image description here

Here I separated the plot that displays the histogram from the plot that generates the legend entry so that the sample picture does not show up in the legend.

Alternatively, if you know the possible column titles beforehand you could also use

do for [name in '"First Column" "Second Column"'] {
   set title name
   plot "test.csv" using (bin(column(name),binwidth)):(1.0) smooth freq notitle with boxes lc 1
}
user8153
  • 4,049
  • 1
  • 9
  • 18
  • Hi user8153! Thanks for this suugestion. Unfortunately I cannot test it, because I am running into errors "You can't change the terminal in multiplot mode", even if I move the _set term_ outside of the loop before the _set multiplot_ as suggested in other threads. Did you try to use your code together with the _set term_? – WolfiG Apr 06 '18 at 09:12
  • 1
    You use `unset multiplot` to get out of multiplot mode. You can put this at the beginning of the file, then set the terminal, and then switch to multiplot mode. However, you don't really need multiplot here. I edited the answer so it creates to separate figures, just like you did in your question. – user8153 Apr 06 '18 at 17:25
0

I found a workaround for my problem:

Instead of extracting the column header from the file (which were desirable), I create a title array which I have to copy from the csv file :( Code:

titles = "columnHeader1 ... columnHeaderN"

do for [COL=1:N] {
    FILE = sprintf("%s%02d%s","Histogram",COL,".png")
    set term png giant font helvetica 24 size 1440, 1080
    set output FILE
    set title word(titles, COL)
    plot "InputFileName.CSV" using (bin(column(COL),binwidth)):(1.0) smooth freq with boxes lc 1
}

This works, but is a couple of clicks more than desired...

WolfiG
  • 1,059
  • 14
  • 31