1

I'm plotting graphs with GNUPlot. I'm able to generate a stacked histogram with a legend, but I'm struggling to extend the legend with MIX,MAX,etc values per item, something like I managed to get with RRDTool

enter image description here

My data looks something like this

DATE         2GRAN   3GRAN   4GRAN   2GTRAN 
20170401     1234    1232    5454    98765
20170402     2312    99999   11      1234
20170403     55      654     1123    10000

but with more columns.

The code I'm using is:

clear
reset
set key out
set terminal jpeg size 1900,1080 font "Helvetica" 16
set output 'plot_per_mo.jpg'
set xtics rotate out
set style data histogram
set style histogram rowstacked
set boxwidth 0.7 relative
set datafile missing '?'
set datafile separator '\t'


do for [i=2:13]{
    stats 'convertedDataMO.dat' using (column(i))
    max(i) = STATS_max
    print sprintf("Max[%2d] = %d",i,max(i))
    }
set palette defined ( 0 "black", 1 "yellow",  2 "green", 3 "blue", 4 "red", 5 "orange" )

plot for [COL=2:13] 'convertedDataMO.dat' using COL:xticlabels(1) lt palette frac COL/13. title columnheader(COL)

I tried to use

plot for [COL=2:13] 'convertedDataMO.dat' using COL:xticlabels(1) lt palette frac COL/13. title sprintf("%20s ---> %6d",columnheader(COL),max(COL))

But I have two problems with it.

  1. columnheader is not recognized as string
  2. max(COL) is only showing last value (tested after removing columnheader from sprintf)

I'm new to gnuplot, so I'm sure I'm missing something important here

taiko
  • 438
  • 1
  • 8
  • 20

2 Answers2

3

max(i) (with regular brackets) in your script is a not a element of the array but a function. How you define arrays, depends on a version of gnuplot. If it's 4.x or 5.0 then see the answer here, and if it's 5.1 or higher, here is demo script. Also, columnheader seems to work for me, at least for this data.

Michael
  • 5,095
  • 2
  • 13
  • 35
  • columnheader work fine, but a I wrote it doesn't work with sprintf. The sprointf I used, because I need to combine column names with e.g. MAX for column. Concerning array workaround, its workable as mentioned in your link – taiko Apr 14 '17 at 06:01
  • @user1133275 Fixed, remove ']' at the end. – Michael Feb 24 '19 at 18:10
0

Using the link from Michael O, describing array workarounds, I rewrote my code as follows :

 headerrow = system('head -1 convertedDataMO.dat')

 arrGet(name, i) = value(sprintf("_%s_%i", name, i))
 arrSet(name, i, value) = sprintf("_%s_%i = %.16e", name, i, value)                   #"array" element with number
 arrSetStr(name, i, value) = sprintf("_%s_%i = % 30s", name, i, value)           #"array" element with string


 do for [i=2:9]{
    stats 'convertedDataMO.dat' using (column(i))
    eval arrSet("MAX",i,STATS_max)
    eval arrSet("MIN",i,STATS_min)
    columnName = sprintf("%s",word(headerrow,i))
    eval arrSetStr("COLUMN",i,"columnName")
 }


 set palette defined ( 0 "black", 1 "yellow",  2 "green", 3 "blue", 4 "red", 5 "orange" )
 plot for [i=2:9] "convertedDataMO.dat" using i:xticlabels(1) lt palette frac i/9. title sprintf("%30s%10s%10d%10s%10d",arrGet("COLUMN",i),"MIN",arrGet("MIN",i),"MAX",arrGet("MAX",i))

The array workaround works well but I had to workaround columnheader also, as I didn't find the way to use it in sprintf in title. Column name were taken from first row instead and also inserted in "array".

At the end with this data:

Date    ParamA  ParamB  ParamC  ParamD  ParamE  ParamF  ParamG  ParamH
20170406        ?       379     ?       354     ?       ?       ?       3095
20170407        ?       469     ?       352     ?       ?       ?       1769
20170408        ?       ?       ?       ?       168     306     ?       1875
20170409        ?       ?       ?       ?       180     309     ?       2659
20170410        ?       ?       ?       ?       168     306     ?       3283
20170411        ?       ?       ?       ?       ?       306     395      1388
20170412        ?       ?       ?       ?       105     306     ?       1565
20170413        423     ?       ?       ?       ?       ?       762     1688

the rewritten code yields this legend : enter image description here

so mission is accomplished :) (data is different than in the question, but it works the same)

Community
  • 1
  • 1
taiko
  • 438
  • 1
  • 8
  • 20