Search code examples
gnuplot

Horizontal histogram in gnuplot


I am trying to plot a horizontal histogram in gnuplot.

This is my current vertival (usual kind) histogram:

width=0.5
hist(x,width)=width*floor(x/width)+width/2.0 
set boxwidth width*0.9
set style fill solid 0.5
plot "a" u (hist($2,width)):(1.0) smooth freq w boxes lc 3 notitle

Now what I need is the exact same result but rotated 90degrees clockwise.

I have tried this below but results are really not what I expect.

width=0.5
hist(x,width)=width*floor(x/width)+width/2.0 
set boxwidth width*0.9
set style fill solid 0.5
plot "a" u (1.0):(hist($2,width)) smooth freq w boxes lc 3 notitle

Solution

  • Although, this question is rather old and basically answered, let me give an update with an illustration, just for the records.

    As far as I know, in current gnuplot (5.4.0) there is still no dedicated horizontal histogram style, probably because you can simply achieve it with boxxyerror (if you know how) as already mentioned in the answers of users @pygrac and @adhip agarwala and in the answer linked by @Christoph. So, there is and was no need to create a vertical histogram and rotate it, because the plotting style boxxyerror existed already in gnuplot 4.0 (2004) or even earlier.

    Since gnuplot 5.0.0 (2015) datablocks are available, so there is no need anymore to write the histogram data smooth freq into a file on disk.

    Script: (works for gnuplot>=5.0.0)

    ### horizontal histogram
    reset session
    
    # create some random test data
    set table $Data
        set samples 2000
        plot '+' u (invnorm(rand(0))+2):(1)   w table, \
             '+' u (invnorm(rand(0))-2):(0.5) w table
    unset table
    
    binwidth = 0.25
    bin(x)   = binwidth * floor(x/binwidth)
    
    set table $Histo
        plot $Data u (bin($1)):2 smooth freq
    unset table
    
    set style fill solid 0.5
    
    plot $Histo u ($2/2):1:($2/2):(binwidth/2.) w boxxy ti "Horizontal Histogram"
    ### end of script
    

    Result:

    enter image description here