1

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/

HSchmale
  • 1,838
  • 2
  • 21
  • 48
  • There's a contradiction: "values are shown as fractions of a total" and "I do not know the yrange" ? The code looks fine, explain the issues you have. Are there 24 columns in each line e.g. ? – Joce May 09 '16 at 07:25
  • @Joce I just added the information about the columns, and removed the unknown yrange. – HSchmale May 09 '16 at 15:51
  • What about adding zeroes in the empty columns? You can also define a "missing data" symbol that will be used in your file, e.g. `set datafile missing "?"`. You should include a minimal example with a small data sample to get a precise answer. – Joce May 09 '16 at 20:36
  • @joce I added an example of my data. – HSchmale May 10 '16 at 14:45
  • OK, so the number of columns does not vary through the file, but you want a script which adapts to the number of columns of the file it's given? And the total per line needs to be calculated on the fly. That's doable, answer soon. – Joce May 10 '16 at 15:09
  • @joce That's exactly it. – HSchmale May 10 '16 at 18:12

1 Answers1

1

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

Community
  • 1
  • 1
Joce
  • 2,220
  • 15
  • 26