Plotting lines with quadruplets R-G-B-Alpha ?
Show older comments
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
Accepted Answer
More Answers (1)
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 Comments
@Matt J you are right. I didn't find the facts in the official documentation either as of r2023b. But few stactoverflow and MATLAB QnA forum provides answers to some of the queries.
From MATLAB r2014 or onwards people have started reporting the transparency availability.
References
@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.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


