Octave: Creating Two Histograms with Color Blending

落花浮王杯 提交于 2019-12-06 02:41:03

问题


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;

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?


回答1:


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.




回答2:


I´ve found in the web a very simple way to do this (it´s not mine, and I don´t really know if this is allowed), the webpage is this

https://chi3x10.wordpress.com/2008/03/10/histograms-of-two-set-of-data-with-different-color-in-matlab/

the code is this

 hist(data1);
 hold on;
 hist(data2);
 hist(data3);
h = findobj(gca,’Type’,’patch’);
display(h)

set(h(1),’FaceColor’,’r’,’EdgeColor’,’k’);
set(h(2),’FaceColor’,’g’,’EdgeColor’,’k’);
set(h(2),’FaceColor’,’b’,’EdgeColor’,’k’);


来源:https://stackoverflow.com/questions/14530944/octave-creating-two-histograms-with-color-blending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!