Use the plot function to set the curve transparency

When using plot function to draw line, why does the rgba value of the color attribute specify, the transparency of the curve does not change

Answers (2)

plot() has no documented ability to specify RGBA, or transparency in any form.
Modern versions of plot() have an undocumented ability to specify RGBA -- but there are circumstances in which the alpha component "falls away"

4 Comments

Interesting, Can you give an example of (undocumented) plot with non trivial RGBA?
When I used a four-component color description a few weeks ago, a Warning popped up that the α component was going to be depricated. I hope it will be replaced with a specific alpha componenent, however for the time being that is undetermined.
If it disappears, a ‘sort of’ work-around would be to use the patch function —
x = linspace(0, 1);
y1 = sin(2*pi*x);
y2 = cos(2*pi*x);
y3 = y1 .* y2;
figure
plot(x, y1, 'Color',[0 0 1 0.2], 'LineWidth',1)
hold on
patch([x flip(x)], [y2, flip(y2+0.015)],'r', 'FaceAlpha',0.25, 'EdgeColor','r', 'EdgeAlpha',0.2)
plot(x, y3, 'g', 'LineWidth',1)
hold off
.
Thanks, no warning I can see with R2024a
x = linspace(0, 1);
y = sin(2*pi*x);
figure
imagesc([0 1],[-1 1],peaks)
colormap jet
hold on
plot(x, y, 'Color',[1 0 1 0.2], 'LineWidth',20)
There is some discussion in Yair blog here
The edge ColorBinding undocumented property won't work now from my test
One can see in my plot example output artefact of overlapping transparent unitary rectangles at the place where the curvature is strong. Plotting points with higher density there reduces somewhat the artefact.

Sign in to comment.

Hi @Siri,

After going through documentation provided at the link below,

https://www.mathworks.com/help/matlab/ref/plot.html?searchHighlight=Plot&s_tid=srchtitle_support_results_1_Plot

the plot function is widely used for creating 2-D line plots. When specifying colors for lines using RGBA values, it is essential to understand how MATLAB interprets these values.

RGBA Color Specification in MATLAB

Color Format: In MATLAB, colors can be specified using RGB triplets or predefined color names. The RGBA format includes an additional parameter for alpha (opacity), where: R, G, B are the intensity values of red, green, and blue components respectively (each ranging from 0 to 1). A (alpha) is the transparency level, where 0 is fully transparent and 1 is fully opaque.

Using RGBA Values: While MATLAB supports RGB triplet notation for colors (e.g., plot(x, y, 'Color', [R G B]) , it does not natively support RGBA as a direct input for line plotting in its basic plot function. The alpha value does not influence line transparency in standard line plots because the plot function does not interpret an alpha channel as part of the color specification.

Transparency Control

To control transparency for line plots in MATLAB, you need to set properties on the line object returned by the plot function after creating your plot:

p = plot(x, y);
p.Color = [R G B A]; % This will not work as expected for line transparency
p.LineWidth = 2; % For example

Instead, you can manipulate transparency through a different approach by modifying the Color property with an RGB triplet and then using other graphical objects like fill or area, which respect alpha transparency:

x = linspace(0, 10, 100);
y = sin(x);
p = plot(x, y);
p.Color = [0 0.5 0.5]; % Set RGB color
set(p,'LineWidth',2); % Set line width
% To apply transparency:
set(p,'Color', [0 0.5 0.5 0.5]); % This will not affect line transparency

For actual transparency effects on lines, alternative methods or functions need to be utilized since standard line plots do not recognize RGBA values as intended.

If transparency is crucial for your visual representation (e.g., overlapping lines), consider using functions like fill or area, which allow for better control over alpha blending.

This understanding should help clarify how color and transparency work within MATLAB's plotting environment and guide you in effectively utilizing these features for your data visualization needs.

2 Comments

For a while now, plot() has had undocumented support for RGBA quads directly with the 'Color' property. See the example https://www.mathworks.com/matlabcentral/answers/2157525-use-the-plot-function-to-set-the-curve-transparency#comment_3276265
The facility has a number of restrictions.
Hi @Walter Roberson,
I do appreciate your feedback and thanks for letting us know about undocumented support for RGBA quads.

Sign in to comment.

Products

Release

R2023a

Asked:

on 5 Oct 2024

Edited:

on 6 Oct 2024

Community Treasure Hunt

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

Start Hunting!