Search code examples
bar-chartgnuplothistogram

gnuplot histogram cluster plot without using histogram


I am to create histogram bar plot cluster without using set style data histogram directly with boxes

I didn't manage to right place the different lines.

Here is my MWE and the result

$data <<EOD
jour v0 v1 v2
15-07-2024 3796 5711 151
16-07-2024 2804 4697 154
17-07-2024 2824 4724 155
18-07-2024 2813 4697 150
19-07-2024 1607 2922 154
20-07-2024 2149 4214 153
21-07-2024 1841 3949 151
EOD

set terminal wxt size 1440,800

set key top autotitle columnhead

set title "TST_HISTO"
set style fill   solid 1.00 border lt -1

set boxwidth .8
stats $data
ClusterSize = STATS_records

print("clustersize: %d\n", ClusterSize)
    
xcoord(i) = (i-1)*ClusterSize + column(0)
offset=0
plot for [i=1:3] $data using (xcoord(i)):(column(i+1)) with boxes,\
     for [i=1:3] '' using (xcoord(i)):(column(i+1)):i+1 with labels offset 1,1

my result ::

my reslut

and here, I would like :

an image of what I would like


Solution

  • Sometimes it takes less time to write the script than searching for it on SO. I was pretty sure a similar task was posted here. Well, no surprise, I guess most questions on SO for gnuplot are about "histograms" or "bar graphs" since there are so many variations (row-stacked, column-stacked, grouped, vertical, horizontal, from flat-lists, special input data, ordered, colored, labeled, etc. ).

    Check the following example. It uses another (dummy) plot command for plotting actually nothing (NaN), but the label on the center of the group. I guess you could do all this with plotting style histogram, but I'm also regularly puzzled about the required dataformat, etc. so that I'm ending up doing it "manually" like the following example.

    Script:

    ### plot bar chart without style histogram
    reset session
    
    $Data <<EOD
    jour v0 v1 v2
    15-07-2024 3796 5711 151
    16-07-2024 2804 4697 154
    17-07-2024 2824 4724 155
    18-07-2024 2813 4697 150
    19-07-2024 1607 2922 154
    20-07-2024 2149 4214 153
    21-07-2024 1841 3949 151
    EOD
    
    GroupSize   = 3
    Gap         = 1
    
    set key top autotitle columnheader
    set title "TST_HISTO" noenhanced
    set style fill solid 1.0 border lt -1
    set boxwidth .8
    set xrange[:] noextend
    set tics out
    
    xPos(i)    = (column(0))*(GroupSize + Gap) + i - 1
    xCenter(i) = column(0)*(GroupSize + Gap) + (GroupSize - Gap)/2.
    
    plot for [i=1:GroupSize] $Data u (xPos(i)):(column(i+1)) w boxes ti columnheader(i+1), \
         for [i=1:GroupSize] ''    u (xPos(i)):(column(i+1)):i+1 w labels center offset 0,0.7 , \
         '' u (xCenter(0)):(NaN):xtic(1)
    ### end of script
    

    Result: (wxt terminal size 1000, 400)

    enter image description here