Search code examples
matlabhistogram2d

add semi-transparency to 2d histogram


I am trying to make a plot like this one:

enter image description here

I want a 2D histogram with bar color proportional to the height and semi-transparent bars. I tried to put together the examples provided here

x = randn(100, 2);

figure
hist3(x, [20 20]);


colormap(hot) % heat map
grid on
view(3);
%bar color
set(get(gca,'child'),'FaceColor','interp','CDataMode','auto');
%semitransparency
set(gcf,'renderer','opengl');

An example of the result is this:

enter image description here

The semi-transparency is absent.

I do not know if it is a problem of my Matlab version (R2014a) or if ---more probably--- I am messing up something. Maybe the axis handles?

Next Outside this question: I would also like to add a transparent surface interpolating the histogram values (I got also some problems with this). I think I saw something like this on SO recently, but I cannot find it anymore. Does anyone have some hits?


Solution

  • You didn't actually change the surface transparency in your example. All you did was change renderers. Here's the extra line you would need:

    set(get(gca,'child'),'FaceAlpha',0.8);
    

    A value of 1 would be opaque, 0 invisible.

    You may also want to change the edge line transparency too:

    set(get(gca,'child'),'EdgeAlpha',0.2);