Histogram with numeric x-axis in gnuplot?

15,034

Solution 1

Ok, after reading the gnuplot help for a bit, it seems that histogram style will ''always'' interpret x axis as sequential entries/categories - so indeed, there seems to be no way to get a numerical axis with a histogram style.

However, it turns out that $ can refer to a column, and those can be used to actually 'reposition' the bars in the second (frequency with boxes style) example; so with this code as hist-v2b.gplot:

set xlabel "X values"
set ylabel "Occurence"
set style fill solid border -1
set term png
set output 'hist-v2.png'
set boxwidth 0.9
set xr [330:400]
# here, setting xtics makes them positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)  
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
plot 'data.dat' using ($1-0.5):2 ti col smooth frequency with boxes, '' u ($1-0.25):3 ti col smooth frequency with boxes, '' u ($1+0.25):4 ti col smooth frequency with boxes, '' u ($1+0.5):5 ti col smooth frequency with boxes

And by calling:

gnuplot hist-v2b.gplot && eog hist-v2b.png

this image is generated: image hist-v2b.png http://img823.imageshack.us/img823/805/histv2b.png

... which is pretty much what I wanted in the first place.

Just a small note - I originally wanted to use the script with inline data; for a setup like this, it would have to be written as

plot '-' using ($1-0.5):2 ti col smooth frequency with boxes, '-' u ($1-0.25):3 ti col smooth frequency with boxes
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
end
Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00
end

... that is, the data would have to be entered multiple times, as it comes in from stdin - this problem is discussed in gnuplot - do multiple plots from data file with built-in commands.

Cheers!

PS: As there is quite a bit of space on the diagram, it would be nice if we could somehow specify separate x-axis ranges; that is discussed in:

Solution 2

Setting the box width properly is very important when you plot a histogram using "boxes" plot style. In one of my blog article I have talked about it. If any interest,click here!

enter image description here

Share:
15,034
sdaau
Author by

sdaau

Updated on June 04, 2022

Comments

  • sdaau
    sdaau almost 2 years

    I'm having this file as data.dat:

    Xstep Y1 Y2 Y3 Y4
    332 1.22 0.00 0.00 1.43
    336 5.95 12.03 6.11 10.41
    340 81.05 81.82 81.92 81.05
    394 11.76 6.16 10.46 5.87
    398 0.00 0.00 1.51 1.25
    1036 0.03 0.00 0.00 0.00
    

    I can plot this data as histogram with this script, hist-v1.gplot (using set style data histogram):

    set xlabel "X values"
    set ylabel "Occurence"
    set style data histogram
    set style histogram cluster gap 1
    set style fill solid border -1
    set term png
    set output 'hist-v1.png'
    set boxwidth 0.9
    # attempt to set xtics so they are positioned numerically on x axis:
    set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)  
    # ti col reads the first entry of the column, uses it as title name 
    plot 'data.dat' using 2:xtic(1) ti col,  '' u 3 ti col, '' u 4 ti col, '' u 5 ti col
    

    And by calling:

    gnuplot hist-v1.gplot && eog hist-v1.png
    

    this image is generated: image hist-v1.png

    However, you can notice that the X axis is not scaled numerically - it understands the X values as categories (i.e. it is a category axis).

    I can get a more numerical X axis with the following script, hist-v2.gplot (using with boxes):

    set xlabel "X values"
    set ylabel "Occurence"
    # in this case, histogram commands have no effect
    set style data histogram
    set style histogram cluster gap 1
    set style fill solid border -1
    set term png
    set output 'hist-v2.png'
    set boxwidth 0.9
    set xr [330:400]
    # here, setting xtics makes them positioned numerically on x axis:
    set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)  
    # 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
    plot 'data.dat' using 1:2 ti col smooth frequency with boxes, '' u 1:3 ti col smooth frequency with boxes
    

    And by calling:

    gnuplot hist-v2.gplot && eog hist-v2.png
    

    this image is generated: image hist-v2.png http://img266.imageshack.us/img266/6717/histv2.png

    Unfortunately, the bars 'overlap' here, so it is hard to read the graph.

    Is there a way to keep the numerical scale X axis as in hist-v2.png, but keep the 'bars' side by side with as in hist-v1.png? This thread, "Re: Histogram with x axis date error" says you cannot:

    But it will be hard to pull the x-coordinate date out of the data file, ...

    but then, it refers to a different problem...

    Thanks,

    Cheers!