legend for transparent patch object is opaque ?

Hello,
I create a simple figure with 2 patch objects where one of the patch is opaque and the other one is transparent (with facealpha set to 0.1) :
figure
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'red')
x2 = [3 6 6 3];
y2 = [3 3 6 6];
patch(x2,y2,'green','facealpha',0.1)
legend('red opaque -> OK','green is opaque instead of transparent -> WHY ?')
The legend does not reflect the fact that the green patch is transparent. I really need my legend to show if the patch is transparent or opaque. Is there a work around ?
Thanks !

 採用された回答

Ameer Hamza
Ameer Hamza 2018 年 6 月 23 日

3 投票

The problem is actually caused by the Rendrer of the figure object. In R2018a opengl seems to make the legend entries transparent, but in prior versions, you need to switch to 'painters' renderer to get this behavior. For example, try the following
f = figure;
f.Renderer = 'painters';
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'red')
x2 = [3 6 6 3];
y2 = [3 3 6 6];
patch(x2,y2,'green','facealpha',0.1)
legend({'red opaque -> OK','green is opaque instead of transparent -> WHY ?'}, 'Box', 'off')

2 件のコメント

Walter Roberson
Walter Roberson 2018 年 6 月 23 日
Traditionally only opengl supported transparency, but R2014b added transparency for painters
Matilda
Matilda 2018 年 6 月 25 日
Yes this works !
Thanks !

サインインしてコメントする。

その他の回答 (1 件)

Yun Guo
Yun Guo 2018 年 6 月 22 日

1 投票

It is not very clear if you want green patch to be transparent, legend to be transparent, or both.
'facealpha' does work as expected. See example below.
  1. I changed the green patch's size so it overlaps with red patch to show transpareny works.
  2. I also removed legend box, so legend looks transparent.
figure
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'red');
x2 = [0.5 2 2 0.5];
y2 = [0.5 0.5 2 2];
patch(x2,y2,'green','facealpha',0.3)
l = legend('red opaque -> OK','green is opaque instead of transparent -> WHY ?');
set(l, 'Box', 'off')

5 件のコメント

Matilda
Matilda 2018 年 6 月 22 日
Hello Yun,
Thanks for your answer. I agree that the green patch is transparent (I designed it that way with 'facealpha') but my problem is that this "transparency" is not displayed by the legend. When I run the code above, I get the figure below. The arrow shows that while the patch is indeed transparent, the green area in the legend is not. Therefore if I want to plot patches with different levels of transparency, I am unable to legend them properly, they all look the same.
Let me know if you have any suggestion on how to fix this,
Thanks !
Matilda
Ameer Hamza
Ameer Hamza 2018 年 6 月 22 日
@Matilda, have you run the line
set(l, 'Box', 'off')
this is the expected output
Matilda
Matilda 2018 年 6 月 22 日
Hello,
Then it must be a bug because on my version of matlab I do not get the expected result ... You can clearly see below that, as opposed to your own plot, mine shows the legend opaque...
Matilda
Ameer Hamza
Ameer Hamza 2018 年 6 月 22 日
I am trying on R2018a. Since you have R2017b, which I cannot try at the moment. But to correctly check the opacity move the legend over the red portion like this
Matilda
Matilda 2018 年 6 月 22 日
Yes, I had already done that and it's definitely opaque (The forum won't let me insert an image anymore, says I am limited to 10 daily uploads, although how I reached that limit, I am not sure ....).
Anyways, it's definitely 100% opaque. I have both R2017b and R2015a installed on my computer and I get the same problem on both. This is so weird ... I think one of the default setting might have been changed or something similar. I might try to reinstall matlab, see what happens.
If you know of any setting that might cause this behavior, let me know! Thanks !
Mathilde

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

リリース

R2017b

質問済み:

2018 年 6 月 20 日

コメント済み:

2018 年 6 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by