HSchmale
HSchmale

Reputation: 1929

GNUPlot - Arbitrary number of columns in stacked line

I am working on a script that generates a csv with arbitrary number of y values for a given x value. The first row of the csv has the names of the data sets. The x value is a unix timestamp. I would like to use gnuplot to graph this data as a stacked line graph where the values are shown as fractions of a total for that row. How would I do this?

I've looked at the following solutions, and attempted to integrate them, but I cannot figure out what I am doing wrong.

It will either say not enough columns or some mismatch for the number of columns.

There are up to N columns of data for a given time index. The total I am looking at is for a given time index.

--

An example of my data:

KEY,CSS,JavaScript,Perl,Python,Shell
1428852630,0,0,0,0,406
1428852721,0,0,0,0,406
1428852793,0,0,0,0,406
1428853776,0,0,0,0,781
1429889154,0,0,0,0,1200
1429891056,0,0,0,0,1648
1429891182,0,0,0,0,1648
1429891642,0,0,0,0,1648
1430176065,0,0,0,0,2056

However, there might be a large number of columns, I want one that sets the number of columns on run time.


http://gnuplot.sourceforge.net/demo/histograms.html - This seems to have issues with being modified to have an arbitrary number of columns.

plot 'immigration.dat' using (100.*$2/$24):xtic(1) t column(2), \
    for [i=3:23] '' using (100.*column(i)/column(24)) title column(i)

https://newspaint.wordpress.com/2013/09/11/creating-a-filled-stack-graph-in-gnuplot/

Upvotes: 1

Views: 281

Answers (1)

Joce
Joce

Reputation: 2332

This answer shows how to count columns, with a slight modification:

file="file.dat"
get_number_of_cols = "awk -F, 'NR == 1 { print NF; exit }' ".file
nc=system(get_number_of_cols)

Then you need so sum colums 2 to nc, let's do it with a recursion:

sum(c,C)=((c!=C)?column(c)+sum(c+1,C):column(c))

And now you can plot:

set key outside
set datafile separator ","
plot for [i=nc:2:-1] file using 0:(100*sum(2,i)/sum(2,nc)):xtic(1) title columnhead(i) with filledcurve y=0

enter image description here

Upvotes: 1

Related Questions