Hi, I run in some issues when I try to set the transparency of an area plot. I can set the transparency of the histogram but not of the area plot. Is this a bug? Version: R2015a 8.5.0.197613.
This is the code I used:
clc;clear;
figure
h = histogram(1:10);
h.FaceAlpha = 0.2;
grid
figure
a = area(1:10);
grid
a.FaceAlpha = 0.2;
This is the warning I get: "No public field FaceAlpha exists for class matlab.graphics.chart.primitive.Area."
Thanks

 Réponse acceptée

Star Strider
Star Strider le 30 Mai 2016
Your code works correctly for me in R2016a.
As an alternative, you can use the patch function:
figure(2)
a = patch([1:10 10:-1:1], [1:10 zeros(1,10)], 'b');
grid
a.FaceAlpha = 0.2;
Note that you have to change your code to define the patch object, here adding a zeros vector to complete it.

4 commentaires

Lorenzo
Lorenzo le 31 Mai 2016
Hi, patch works fine. I suppose there is a problem with my version, since area and bar plot seem to lack a facealpha option.
Thanks!
My pleasure!
It may not be a problem, that feature was likely added in a later version.
Also, in a more general expression, the patch call for the sort of plot you want is:
a = patch([1:10 flip(1:10)], [1:10 zeros(1,10)], 'b');
Note that you need to provide a ‘return path’ in your x-vector in patch, the reason for the flip call (you can also use fliplr). The y-vector has to have upper and lower border limits that match the x-vector.
Lorenzo
Lorenzo le 1 Juin 2016
Modifié(e) : Lorenzo le 1 Juin 2016
Thanks again! In the end I sticked with the area plot without alpha.. I did not want to change all the data to patches, but a first try showed good results.
Star Strider
Star Strider le 1 Juin 2016
My pleasure.
You can also see if the fill function implements the ‘alpha’ parameter. It requires that you define a closed polygon for your data but may be easier to use with your data.
One option is to create your own function that implements the patch function and then call it in your plots. That way you would not have to change all your data, you simply call your function in each plot. You could probably do that with an anonymous function.

Connectez-vous pour commenter.

Plus de réponses (1)

John BG
John BG le 30 Mai 2016
In R2016a
your code works fine for both graphs.
Yet, you may want to control transparency separately:
clc;clear;
figure(1);
h = histogram(1:10);
h.FaceAlpha = 0.2;
grid
figure(2);
a = area(1:10);
grid
a.FaceAlpha = 0.8;
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John

Catégories

En savoir plus sur Graphics Object Properties dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by