0

I would like to plot a bar chart or histogram like this in gnuplot. enter image description here

I tried set style histogram rowstacked which is a start but it adds the columns on top of each other while I need them overlapped. Next is the issue of transparent color shading.

Thanks for your feedback.

UPDATE: user8153 asked for additional data.

The set style histogram clustered gap 0.0 is doing the cluster mode of the histogram bars. If you blur the eye it sort-of shows what I want but with overlap and transparent shading.

enter image description here

The only other histogram modes given in the docs are rowstacked and columnstacked. I never got a plot out of columnstacked so I discarded it. Now rowstacked stacks the histogram bars.

enter image description here

The overlay appearance is there but it is wrong. I don't want the stacked appearance. The histograms have to overlay.

Code :

set boxwidth 1.0 absolute
set style fill solid 0.5 noborder 
set style data histogram
set style histogram clustered gap 0.0
#set style histogram rowstacked gap 0.0
set xtics in rotate by 90 offset first +0.5,0 right
set yrange [0:8000]
set xrange [90:180]

plot 'dat1.raw' using 3 lc rgb 'orange', \
     'dat2.raw' using 3  lc rgb 'blue', \
     'dat3.raw' using 3  lc rgb 'magenta'

Thanks for your feedback.

Gert Gottschalk
  • 1,658
  • 3
  • 25
  • 37
  • What does your data look like? I assume you can just plot two regular histograms in the same figure, using a terminal that supports transparency (I like pngcairo). – user8153 May 14 '18 at 22:29

2 Answers2

3

Given a sample datafile test.dat

-10 4.5399929762484854e-05
-9 0.0003035391380788668
-8 0.001661557273173934
-7 0.007446583070924338
-6 0.02732372244729256
-5 0.0820849986238988
-4 0.20189651799465538
-3 0.4065696597405991
-2 0.6703200460356393
-1 0.9048374180359595
0 1.0
1 0.9048374180359595
2 0.6703200460356393
3 0.4065696597405991
4 0.20189651799465538
5 0.0820849986238988
6 0.02732372244729256
7 0.007446583070924338
8 0.001661557273173934
9 0.0003035391380788668
10 4.5399929762484854e-05

you can use the following commands

set style fill transparent solid 0.7
plot "test.dat" with boxes, \
     "test.dat" u ($1+4):2 with boxes

to get the following result (using the pngcairo terminal):

enter image description here

user8153
  • 4,049
  • 1
  • 9
  • 18
1

Using transparency as in user8153's solution is certainly the easiest way to visualize an overlap of two histograms. This works even if the two histogram do not have identical bins or x-data-ranges. However, the color of the overlap is pretty much bound to the colors of the two histogram and the level of transparency. Furthermore, if you want to show the overlap in the key you have to do it "manually".

Here is a solution where you can choose an independent color for the overlap area. The overlap is basically the minimum y-value from both histograms for each x-value. For this you need to compare the y-values for each x-value. This can be done in gnuplot with some "trick" by merging the two files line by line. This requires the data in a datablock (how to get it there from a file). Since this merging procedure is using indexing of datablock lines, it requires gnuplot>=5.2.0. This assumes that you have the same x-range and bins for each histogram. If this is not the case, you have to implement some further steps.

Script: (works with gnuplot>=5.2.0, Sept. 2017)

### plot overlap of two histograms
reset session

# create some random test data
set samples 21
f(x,a,b) = 1./(a*(x-b)**4+1)
set table $Data1
    plot '+' u 1:(f(x,0.01,-2)) w table 
set table $Data2
    plot '+' u 1:(f(x,0.02,4)) w table
unset table

set boxwidth 1.0
set grid y
set ytics 0.2

set multiplot layout 2,1

    set style fill transparent solid 0.3
    plot $Data1 u 1:2 w boxes lc 1 ti "Data1", \
         $Data2 u 1:2 w boxes lc 2 ti "Data2"

    set print $Overlap
        do for [i=1:|$Data1|] { print $Data1[i].$Data2[i] }
    set print
    
    set style fill solid 0.3
    plot $Data1   u 1:2 w boxes lc 1 ti "Data1", \
         $Data2   u 1:2 w boxes lc 2 ti "Data2", \
         $Overlap u 1:($2>$4?$4:$2) w boxes lc "red" ti "Overlap"

unset multiplot
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72