Search code examples
plotoctavehistogram

Octave: Creating Two Histograms with Color Blending


I am creating one histogram on top of another in Octave.

    hold on;
    hist(normalData(:, column), 10, 1, "facecolor", "g");
    hist(anomalousData(:, column), 10, 1, "facecolor", "r");
    hold off;

enter image description here

As you can see there is overlap and the red data obscures some of the green data. Is there a way around this? Perhaps by having the colors blend on the overlapping portions?


Solution

  • There is a long way around your problem. Unfortunately the plotting property for transparency "facealpha" does not work with the hist() function.

    The code below shows my work around. The default graphics toolkit may be fltk, so change it to gnuplot.

    clear all
    
    graphics_toolkit("gnuplot")
    
    A = randn(1000,1);
    B = randn(1000,1)+2;
    

    Still use hist to calculate the distributions

    [y1 x1] = hist(A,10);
    [y2 x2] = hist(B,10);
    

    Now we are going to convert the hist data into a format for plotting that will allow transparency.

    [ys1 xs1] = stairs(y1, x1);
    [ys2 xs2] = stairs(y2, x2);
    
    xs1 = [xs1(1); xs1; xs1(end)];  xs2 = [xs2(1); xs2; xs2(end)];
    ys1 = [0; ys1; 0];  ys2 = [0; ys2; 0];
    

    Plot the data with the fill function

    clf
    hold on; 
    h1=fill(xs1,ys1,"red");
    h2=fill(xs2,ys2,"green");
    

    Change the transparency to the desired level.

    set(h1,'facealpha',0.5);
    set(h2,'facealpha',0.5);
    hold off;
    

    I'd post an image if I had more reputation.