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:
How do I retrieve the headerString# strings and use it as title in my individual plot?
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
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
}