Hello, I have 2 different bar plot on a graph and I would like to put transparancy in order to see all the data even if my curves overlap. I try this code :
b1 = bar(x1,y1);%,'FaceColor',[0.2,0.2,0.5]);
set(get(b1,'Children'),'FaceAlpha',0.3)
hold on
b2 = bar(x1,y1,'FaceColor',[0,0.7,0.7]);
set(get(b2,'Children'),'FaceAlpha',0.2)
But it doesn't work. Also, I would like to calculate the probability of data which are in common between the 2 courb.
Thanks in advance,
Best regards

1 Kommentar

pfb
pfb am 28 Apr. 2015
what do you exactly mean "it does not work"?
I'm asking because in my case it "sort of works". The bars do become transparent. Only, when they overlap, they produce a different color depending on which one is on top.
I see that you plot y1 vs x1 in both of your plots... But probably you re-define those between the two plots...

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Brendan Hamm
Brendan Hamm am 28 Apr. 2015

1 Stimme

A) The bar series will only have a 0x0 GraphicsPlaceholder as a child (so no properties for the bars Children) B) The barseries does not have a property FaceAlpha (as it is not using the Patch object).
I would suggest a different course of action, and this will depend on the version you are using. We can use x1 and y1 to get back the relative number of counts in each bin.
vals = repelem(x1,y1); % Repeats element of x1(i), y1(i) times (for each i)
In versions >= 2014b we can use this to create a histogram, which will have a Face alpha:
h = histogram(vals);
h.FaceAlpha = 0.2;
Prior versions require us to use hist.
hist(vals,x1) % Hist does not return a handle, but creates an axes with a child of type Patch
patch1 = findobj(gca,'type','Patch'); % The child object of axes is a Patch Object
set(patch1,'FaceAlpha',0.2);

6 Kommentare

Sarah Guiffray
Sarah Guiffray am 29 Apr. 2015
But I can't use histogram because I want to plot data in function of my x axis. So, it is impossible to put transparancy on bar series ?
Brendan Hamm
Brendan Hamm am 30 Apr. 2015
Why would histogram not work for this? It is really doing a very similar thing to using bar. If you can say bar(x,y), then you have discrete x locations and count values, y, defined at those locations. If you use the methods I show above you are doing the same thing, placing the height (count) y at location x.
If there is something I am missing please give an example of why this will not work with actual data, so that I might help.
Sarah Guiffray
Sarah Guiffray am 3 Mai 2015
Bearbeitet: Sarah Guiffray am 3 Mai 2015
First at all, my x-axis is not uniform, it could be x = [-5 -4 -1 3 4 5 6 7 10]. And my y is not the number of each x but the probability. For example, I have x(1) = -5. And I have 3 times this value and the total of number is 100, so my y(1)=10/100.
Brendan Hamm
Brendan Hamm am 6 Mai 2015
Bearbeitet: Brendan Hamm am 6 Mai 2015
In versions 2014b or later the bar does not have patch children, so FaceAlpha is not available. If you have the count data used to create the probabilities the above with histogram works with the count data. Otherwise you need to manually create the patches. So here is a function which does it.
Example:
x1 = [-5 -4 -1 3 4 5 6 7 10];
y1 = rand(size(x1));
y1 = y1/sum(y1);
p1 = createPatches(x1,y1,0.4,'r',0.4);
y2 = rand(size(x));
y2 = y2/sum(y2);
p2 = createPatches(x1,y2,0.4,'b',0.4);
Enjoy!
Sarah Guiffray
Sarah Guiffray am 6 Mai 2015
Thank you very much ! The last question : how can I put a legend for each patch ? beacause, when I put a legend, I have p1 and p2 in the same color ...
Ok, I made some minor changes.
1. The ptchs return variable is now an array of patches
2. There is a second output ptchGrp which contains an hggroup, which is the way you can group multiple plot objects together. You will need this to change the legend values.
x1 = [-5 -4 -1 3 4 5 6 7 10];
y1 = rand(size(x1));
y1 = y1/sum(y1);
[p1,g1] = createPatches(x1,y1,0.4,'r',0.4);
y2 = rand(size(x1));
y2 = y2/sum(y2);
[p2,g2] = createPatches(x1,y2,0.4,'b',0.4);
Use the hggroup for creating the legend and get second output:
[leg,legIcon] = legend([g1 g2],'Y1','Y2');
legIcon contains the patch objects of the legend
legPatch = findobj(legIcon,'type','patch');
Set the FaceAlpha for the legend's patches:
legPatch(1).FaceAlpha = p1(1).FaceAlpha;
legPatch(2).FaceAlpha = p2(2).FaceAlpha;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Juan Ignacio Bravo Pérez-Villar
Bearbeitet: Juan Ignacio Bravo Pérez-Villar am 18 Mai 2016

8 Stimmen

Hello, I found a simpler way to do it that works for me on Matlab 2015b
b1 = bar(x1,y1,'r');
b1.FaceAlpha = 0.5;
hold on
b2 = bar(x2,y2,'FaceColor',[0,0.7,0.7]);
b2.FaceAlpha = 0.5;
Hope its useful for somebody.
Regards,
Chad Greene
Chad Greene am 3 Mai 2015

0 Stimmen

This works find for me in 2012b:
x1 = [-5 -4 -1 3 4 5 6 7 10];
y1 = rand(size(x1));
b1 = bar(x1,y1);%,'FaceColor',[0.2,0.2,0.5]); %RXLEV
set(get(b1,'Children'),'FaceAlpha',0.3)
hold on
b2 = bar(x1,y1,'FaceColor',[0,0.7,0.7]);
set(get(b2,'Children'),'FaceAlpha',0.2)
If it's not properly transparent, try
set(gcf,'renderer','opengl')

1 Kommentar

Sarah Guiffray
Sarah Guiffray am 5 Mai 2015
Bearbeitet: Sarah Guiffray am 5 Mai 2015
I do the same but it does not work .. I do not know why. (I have the version R2014b)

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by