Search code examples
gnuplot

gnuplot stacked histogram, variable number of columns


I have data like the following and I would like to draw it as a stacked histogram by row.

I am uncertain how to deal with the variable number of columns. It does not matter how many columns there are. I just wanted each row (representing the x-axis) to be represented by stacked elements of appropriate sizes.

So this is a datafile

1 177933359784 229219296664 341813157716 209533300212
2 255402807736 251229375936 835510168396 231782874672 434617172460 178787851436 266499814160 1117797639936 57592550000 198107674676 150477485904 185091125280 254699320120 75628552916 356450147160 301650257548 21282231000 186060110480
3 54813595308 54882423776 218795621308 163885979036 54818070404 20628311576 52952103036 54986144552
4 36624324132 36532865488 178057963436 2347745712 44645458912 36512335708 24739488 35906817008 139601663024 6184450232 36070413804 34858648512 42900740960 35706214936 30100400180 36448690584 35683591888 36385254484
5 19223864 41579503932 166759383460 44780600180 25495668 40949394776 40849369252 125417881448 27478998208 40865682976 41853638488 31524042208 40700534772 41418852000 40146517796 41214902564 41431844000
6 37113748488 36172184940 169715926484 82347806556 35901460368 30886933892 40753766928 31178667976 31723176 36400840248 38837320216 128079968892 41760130800 35330289244 32553077748 41700480284 45114644664 40258452076 43444918452 37580420908 5764948 37040799772
7 25959740 39432378576 151726657760 44594775408 38385820516 29059225864 25956668 30914196432 39598248152 36640081032 39871132484 116760673624 43318844936 36579580800 34569237788 43469684324 47506703156 46677036120 39181749636 25956676 39995476692
8 18876724748 18985678268 86813241780 33543334596 18064220684 87170347888 18261179328 21871605808 18495034756 15929433608 18786660804 18830319920
9 30045340 31043084896 120500393940 119774525620 30011125012 22884781568 29644663692
10 25928760 20644055996 82592846852 20597070344 20602500000 61805221532 20605604328 20583911476 20610151936 82600282720 20610967884 20643852764 20631865300 20575625824 20602692912 20639181688 20577773680 20642405512
11 44476576388
12 79978012472 79622634576 395113932124 69330382460 76254447112 75224239600 316520607248 5554724 86059624228 79151308652 72904383564 76786870752 19327796 68894935668 80512157172 23088 80039581484
13 2629209940 2181022556 2378540796 2471372512 2903991608

This is the command file

# Y-axis
set yrange [0:*]
set format y '%.1s%c'

# Legend
set nokey

# Plot
set style data histograms
set style histogram rowstacked
set boxwidth 0.5
set xtics rotate

plot "data.dat" using 2:xticlabel(1), '' using 3, '' using 4, '' using 5

And the following is an attempt at plotting it.

enter image description here

  1. I am not sure how to include all the possible columns

  2. Some elements looks weird, like #12 and #13.


Solution

  • I initially submitted a complicated answer that involved rescaling the data to avoid overflow, but after going back to clean it up I can no longer reproduce the original problem that triggered a need to rescale.

    So here is a 2nd answer that simply plots the data as given. Tested with gnuplot 5.4.9 (current version)

    # Y-axis
    set yrange [0:*]
    set format y '%.1s%c'
    set style fill solid
    
    # Legend
    set nokey
    
    # Plot
    set style data histograms
    set style histogram rowstacked
    set boxwidth 0.5
    set xtics rotate
    
    plot for [i=2:*] "overflow.dat" using (column(i)):xticlabel(1) fillcolor lt i
    

    enter image description here