Question

I just started to work with Gnu Plot and i created a few simple plots. But now i have a new problem.

The input is a csv-file like this:

name;n0;n1;n2
Benj;1;3;2
Silv;6;1;2
Steffi;3;2;2

The count of the columns and the count of the rows is dynamic. So this is also a valid input file:

name;n0;n1;n2;n3;n4
Benj;2;2;3;2;1

I like to have a graph like this (input is the first csv): enter image description here

I played around with some Gnu Plot files which i created before, but i always just got something totally different.

Maybe someone has a hint or something to help me?

Thank you very much,

Best regards Kevin

Was it helpful?

Solution

As noted by Christoph, you need to transpose the data before plotting, the answers here should be helpful with that. To determine the number of rows in the data, recent versions of Gnuplot can use the stats command (see comment by Christoph below), otherwise the number of lines needs to be counted by other means.

Here is how you could handle both issues with external programs:

dfile = "data.csv"
nrec  = system("wc -l < " . dfile)
set datafile separator ';'
plot for [i=2:nrec] '<transpose --fsep \; -t '.dfile using i:xtic(1) with lines title columnhead

I used wc from coreutils to count the number of lines and transpose as suggested by flyingsheep to handle the transposition.

Result:

Running the same script on data with more plots and data points:

data2.csv

name;n0;n1;n2;n3;n4
Benj;1;3;2;5;3
Silv;6;1;2;3;4
Steffi;3;2;2;4;2
Carl;2;4;5;3;2

OTHER TIPS

That's the wrong data file format for gnuplot. Usually, for a single plot, gnuplot uses all rows for a single column. With a data file like

name;Benj;Silv;Steffi
n0;1;6;3
n1;3;1;2
n2;2;2;2

the plot becomes as easy as

set datafile separator ";"
set style data lines
set key autotitle columnhead
plot 'data.csv' using 2:xtic(1), '' using 3, '' using 4

with the result (using 4.6.3):

enter image description here

As mentioned already in the other answers, the most efficient way is certainly to transpose the data and use simple standard gnuplot plotting commands.

If you want a platform-independent gnuplot-only solution you could do the following: If all rows have the same number of columns you can plot the data using the option matrix (check help matrix). There are a few examples here on SO. However, here, the additional difficulty is to get the xticlabels and the legend as well.

I tried a few script variations, but the one below was the only one I found which seems to work unchanged for both gnuplot4.6.0 (March, 2012) and gnuplot5.5.0 (July 2021). I don't want to say the solution is beautiful, but it works with gnuplot-only.

Data: SO22438313.dat (taken from Thor's answer)

name;    n0; n1; n2; n3; n4
Benj;    1;  3;  2;  5;  3
Silv;    6;  1;  2;  3;  4
Steffi;  3;  2;  2;  4;  2
Carl;    2;  4;  5;  3;  2

Script: (works for gnuplot>=4.6.0, March 2012)

### plotting rows with xticlabels and legend
reset

FILE = "SO22438318.dat"

set datafile separator ';'
set yrange[0:]
set key noautotitle
# set colorsequence classic   # optional for gnuplot>=5.0

myTitles = ''

plot FILE matrix u (c=$1):3:(r=$2) every ::1:1 w l lw 2 lc var, \
        for [i=1:c]'' u (i):(i==1?myTitles=myTitles.' '.strcol(1):0,NaN): \
            xtic($0==0?xt=strcol(i+1):xt) w l lw 2, \
        for [i=1:r] '+' u 1:(NaN) every ::0::0 w l lc i lw 2 ti word(myTitles,i+1)
### end of script

Result:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top