Feed on
Posts
Comments


to make a partially transparent histogram in matlab, use the facealpha setting.

for instance, the following code produces the histogram seen above:

figure
hist(data1,20)
h = findobj(gca,’Type’,'patch’);
set(h,’FaceColor’,'r’,'EdgeColor’,'w’,'facealpha’,0.75)
hold on
hist(data2,20)
h = findobj(gca,’Type’,'patch’);
set(h,’facealpha’,0.75);
ylabel(‘counts’)
xlabel(‘gene-tree-length/species-tree-length’)
legend(‘no HGT’,'HGT’)
title(‘p = 0.5′);


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

15 Responses to “how to make a transparent histogram in matlab”

  1. on 27 Mar 2011 at 5:52 pm Chris Schwartz

    Thank you, this was quite helpful.

  2. on 05 Jun 2011 at 2:35 pm Jefferson

    Exactly what I was looking for, but when I declare the second (and third etc) h and change facecolor it happens to all the histograms, not just the last one.

  3. on 01 Jul 2011 at 9:41 pm toto

    yes. that is work in my chart. thank for sharing

  4. on 19 Jul 2011 at 12:34 pm Srini

    Thanks for the trick :-)

  5. on 26 Aug 2011 at 11:14 am kevho1

    Thanks, this is great.
    Do you know how to draw a histogram with errorbars, the errorbars depending on the number of points in each bin of the histogram?
    If you couuld help, that would be great

  6. on 08 Sep 2011 at 2:01 pm anon

    Very helpful.

    Jefferson:

    Unless I am mistaken, findobj will return a vector with handles to all objects that fit the bill. If you want to modify this such that you can use multiple different colors or alphas, refer to each separately. Something like:

    figure
    hist(data1,20)
    hold on
    hist(data2,20)
    h = findobj(gca,’Type’,’patch’);
    set(h(1),’FaceColor’,’r’,’EdgeColor’,’w’,’facealpha’,0.75)
    set(h(2),’FaceColor’,’b’,’EdgeColor’,’w’,’facealpha’,0.75)
    ylabel(‘counts’)
    xlabel(‘gene-tree-length/species-tree-length’)
    legend(‘no HGT’,’HGT’)
    title(‘p = 0.5′);

  7. on 13 Jul 2012 at 10:08 am Tudor

    I would also like to know how to add errorbars, please :) Thanks!

  8. on 16 Jan 2013 at 7:55 am bork

    thanks, it’s perfect

  9. on 08 May 2013 at 8:36 am ab

    agree with anon, you wanna plot the data first then get the handles to the objects for changing the patch options

  10. on 01 May 2014 at 1:56 pm Adam

    Is anyone else having trouble with the transparency in Matlab 2014a?

  11. on 12 Jun 2014 at 12:04 pm Alex

    http://www.mathworks.nl/help/matlab/ref/alpha.html

    Adam please check the link above

  12. on 07 Oct 2014 at 3:55 am spencer diamonds guarantees

    spencer diamonds guarantees

    how to make a transparent histogram in matlab » from the desk of stinkpot

  13. on 24 Nov 2014 at 7:48 am Agoston

    I get problems with Matlab 2014a. Some patch areas are added to the histogram. Is it a version bug?

  14. on 04 Apr 2016 at 5:46 am Bharat R Iyengar

    Hi Lawrence,

    This is really nice but I think this is dependent on OpenGL and fails if it is not supported. Plus, the figures are not exported to EPS/PDF. If you save the figure as EPS, Matlab saves it as a raster image.

    In general, I am not very happy with Matlab’s vector graphics support. There are multiple issues. EPS/PDF files have messed up co-ordinates. This you realize when you position the figures in LaTeX. EPS files generated by image and imagesc get distorted when opened in a different application such as inkscape. So, transparent histograms using ‘facealpha’ are good as long as you are happy with raster images.

    Thanks again.

  15. on 17 Sep 2016 at 9:33 pm Ali

    Thanks, helped me a lot. Do you have an idea how to add Kernel density function on the histograms? Thanks again

Did I get this wrong? Let me know!

Trackback URI | Comments RSS