Plotting lines with quadruplets R-G-B-Alpha ?

18 ビュー (過去 30 日間)
Matt J
Matt J 2024 年 2 月 17 日
編集済み: Hassaan 2024 年 2 月 19 日
It appears that in recent Matlab, I am able to append a 4th number to the RGB triplet, which appears to set either a transparency (alpha) or brightness value for a line plot. Is this documented anywhere? And if so, in what property is the alpha=0.4 value stored?
figure
h1=plot(1:5,'Color',[1,0,0]); hold on
h2=plot(2:6,'Color',[1 0 0 0.3]); hold off

採用された回答

Adam Danz
Adam Danz 2024 年 2 月 18 日
編集済み: Adam Danz 2024 年 2 月 18 日
> Is this documented anywhere?
No, the 1x4 RGBA alpha-color option is not documented at this time. One reason is that the alpha channel is not saved when saving and loading the figure. This also poses a problem in live scripts where the alpha value is not preserved. Another issue with partially transparent lines is that if the line width is thick enough, you'll see parts of the line fold over on itself at sharp curves. You can see this by increasing the linewidth in the demo below to something larger like 30.
Line alpha can be achieved by using patch but it's a bit tricky to get the inputs right so here's an anonymous function that's merely a wrapper to this process.
lineAlphaFcn = @(x,y,color,alpha,varargin) ...
patch('XData',[x(:);nan],...
'YData',[y(:);nan],...
'EdgeColor',color,...
'EdgeAlpha',alpha, ...
varargin{:});
x = 0 : .1 : 9;
y = sin(x);
h = lineAlphaFcn(x,y,'b',0.4,'LineWidth',3);
Related topics
  • See also this thread that shares this approach and this one that uses datetime values.
  • I also used this approach to create this visualization of first 10,000 dp of pi which contains a single "line" (patch).
  • Discussion in "ideas" channel.

その他の回答 (1 件)

Hassaan
Hassaan 2024 年 2 月 17 日
編集済み: Hassaan 2024 年 2 月 19 日
You can set the transparency (alpha) of lines and other graphical objects by specifying a fourth element in the color vector. This additional value represents the alpha value, ranging from 0 (fully transparent) to 1 (fully opaque).
Documentation:
While there isn't a dedicated documentation page as of r2023b specifically mentioning this feature:
  • plot: The color can be specified as a three-element RGB vector or a four-element RGBA vector, where the fourth element controls the alpha value.
  • line: Similar to the plot function, the 'Color' property can accept an RGBA vector.
  • Color: The color can take a four-element vector can be used to represent RGBA colors.
Property where alpha is stored:
The alpha value isn't explicitly stored in a separate property but is part of the overall color information. When you create a line object using an RGBA vector, the color and alpha information are combined and stored internally by MATLAB. You can access and modify the color, including the alpha value, using various properties like:
  • set(h, 'Color', [r g b alpha]): Sets the color of the line object h using an RGBA vector.
figure
h1 = plot(1:5, 'Color', [1 0 0 0.5]); % Set alpha to 0.5 (50% transparent)
hold on
h2 = plot(2:6, 'Color', [0 1 0 0.3]); % Set alpha to 0.3
hold off
p1 = plot(rand(10,1),'r-','LineWidth',5); hold on
p2 = plot(rand(10,1),'r-','LineWidth',2);
p1.Color(4) = 0.35;
p2.Color(4) = 0.85;
Note
  • May have side effects in some cases
I recommend checking the latest MATLAB documentation or contacting MathWorks support for clarification on how to work with RGBA values in the context of line plots and other graphical objects.
References
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  4 件のコメント
Adam Danz
Adam Danz 2024 年 2 月 18 日
@Hassaan this is the problem with using chat bots to write your answers for you. Please verify the chat bots answers before posting.
Hassaan
Hassaan 2024 年 2 月 19 日
編集済み: Hassaan 2024 年 2 月 19 日
@Adam Danz Have you even cared to read my full answer and justificaiton. Did you see the references I cited.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

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

カテゴリ

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