Clear Filters
Clear Filters

Use the plot function to set the curve transparency

17 views (last 30 days)
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)

Walter Roberson
Walter Roberson on 5 Oct 2024
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
Bruno Luong
Bruno Luong on 5 Oct 2024
Edited: Bruno Luong on 5 Oct 2024
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)
Bruno Luong
Bruno Luong on 6 Oct 2024
Edited: Bruno Luong on 6 Oct 2024
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.


Umar
Umar on 5 Oct 2024

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
Walter Roberson
Walter Roberson on 5 Oct 2024
Edited: Walter Roberson on 5 Oct 2024
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.
Umar
Umar on 5 Oct 2024
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

Community Treasure Hunt

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

Start Hunting!