csvplotgnuplotdynamic-columns

GnuPlot: Draw line per CSV line


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


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