How do I add transparency to a plot in the Live Editor?

5 ビュー (過去 30 日間)
Martin Bergerhausen
Martin Bergerhausen 2024 年 2 月 17 日
編集済み: Adam Danz 2024 年 3 月 1 日
Hey all,
if I use the following code in the Command Window, I get a very transparent, red X:
plot([0, 1],[0, 1; 1, 0], 'Color', [1 0 0 .1])
Yet, if I use the same line in the live editor, I get a non-transparent X. How do I add transparency to a plot in the Live Editor?
Best regards,
Martin
  2 件のコメント
Matt J
Matt J 2024 年 2 月 17 日
編集済み: Matt J 2024 年 2 月 17 日
It's a bit surprising to me that your code doesn't produce an error message. Did you intend to specify the color with 4 numbers
'Color' ,[1 0 0 .1]
or was it really supposed to be [1,0,0.1] ?
Martin Bergerhausen
Martin Bergerhausen 2024 年 2 月 17 日
It is supposed to be 4 numbers. Basically, I just copied from the answer (or the comment) below: https://de.mathworks.com/matlabcentral/answers/103074-how-can-i-set-the-transparency-of-line-objects-in-matlab-7-14-r2012a#comment_2299585
It would have solved all my problems. Of course, the X in the plot above is just a simple example and I want to create a few hundreds overlapping plots in order to get some kind of density.

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

採用された回答

Adam Danz
Adam Danz 2024 年 2 月 18 日
編集済み: Adam Danz 2024 年 3 月 1 日
When using RGBA values, the alpha value is not saved so when the figure loads, the color will lack transparency. Due to how figures in a live script work, this limitation prevents the use of undocumented RGBA color-alpha values in the live editor.
The workaround is to use a patch object which supports EdgeAlpha but it may not be intuitive how to arrangement inputs to patch() so that it forms a line.
See this answer for an anonymous function that's a wrapper to patch but creates lines with specified color and transparency (alpha). This will work in live editor figures.

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2024 年 2 月 17 日
Unfortunately, the hack of using a 4-element Color does not work in LiveScript.
The work-around is to set the figure Visible property to 'on', which will result in the figure popping out to be plotted out-of-line.

Matt J
Matt J 2024 年 2 月 17 日
編集済み: Matt J 2024 年 2 月 17 日
The syntax of giving an RGBA color setting seems to be a very old and undocumented feature,
which means you cannot rely on it, especially not for newer environments like the Live Editor.
You can fake a transparency effect by rendering the line as a dense scatter plot, e.g.,
x=linspace(0,1,1e4);
y=x.^2;
scatter(x,y,'r','filled','MarkerFaceAlpha',0.015, 'MarkerEdgeColor','none', 'SizeData',1)
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 2 月 17 日
Using RGBA for color is new as of HG2, and it's use has been expanded as time goes on. But it is undocumented and so not reliable.

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


Catalytic
Catalytic 2024 年 2 月 17 日
編集済み: Catalytic 2024 年 2 月 17 日
I find that this makes the lines dim in the Live Editor, like they appear in your post -
plot([0, 1],[0, 1; 1, 0], 'Color', [1 0.8 0.8])
But, making the lines dim is not the same as making them transparent. You won't see objects behind the lines any better if you were to overlay the plot on something else, for example.
  1 件のコメント
Martin Bergerhausen
Martin Bergerhausen 2024 年 2 月 17 日
Transparency is my ultimate goal, as I want to have multiple layers.

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

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by