No transparency in Livescript

Hi,
I use RBGA values to get transparency in plots using Matlab 2020a.
This works fine in regular code but I noticed that in Livescript, the color renders as completely opaque no matter what I set the opacity value A too. This example code,
plot([0 1],[0 1],'-','Color',[1 0 0 .25],'LineWidth',20);
hold on
plot([0 1],[1 0],'-','Color',[1 0 0 .25],'LineWidth',20);
title(sprintf("renderer = %s",get(gcf,'renderer')))
when run as regular code from the draws two red bars with transparency visible where they cross (Figure 1).
However, when run it as a Livescript it produces two 100%-opaque bars, (Figure 2). Is this a know shortcoming? Is there a workaround?

6 comentarios

I see what you mean. I get the same result on desktop R2020b and on MATLAB Online.
However, oddly when I run the code here on Answers, which uses Livescript, it does display the right answer.
plot([0 1],[0 1],'-','Color',[1 0 0 .25],'LineWidth',20);
hold on
plot([0 1],[1 0],'-','Color',[1 0 0 .25],'LineWidth',20);
title(sprintf("renderer = %s",get(gcf,'renderer')))
Duijnhouwer
Duijnhouwer el 22 de Feb. de 2021
Interesting!
What version do you have? I opened a Livescript in the Matlab Live Editor and ran "version"
It returned: '9.9.0.1576564 (R2020b) Update 4'
Is that newer than your local R2020b? Maybe the problem is fixed in the latest and greatest Matlab that may already be used on the website but has not yet made it to desktop versions
version
ans = '9.9.0.1573405 (R2020b) Update 4'
What I showed above was by using the new Run facility of MATLAB Answers. I do not know if it is generally available yet; I was one of the beta testers. For me, in the toolbar here in Answers just to the right of the paperclip there is a green Run button and a pink eraser. The new facility uses LiveScript -- but it might use different output drivers as it needs to prepare the output for display here in Answers.
However when I tested in Update 4 on my desktop, I got the result you demonstrated, with the solid lines.
I am upgrading to Update 5 at the moment and will see if there is a difference.
Walter Roberson
Walter Roberson el 22 de Feb. de 2021
Update 5:
If I run the .mlx file from outside the Livescript editor (give its name at the command line) then the figure that pops up has transparency, but when I run inside the Livescript editor, then the figure that pops up does not have transparency.
Adam Danz
Adam Danz el 23 de Feb. de 2021
Editada: Adam Danz el 4 de Abr. de 2023
-------------------------- old response as a user------------------------
I have the same results (windows 10, r2020b update 4) using opengl hardward and software. I see expected behavior outside of mlx but no transparency within mlx.
Keep in mind this style of line transparency is undocumented so that challenges the notion of expected behavior.
The three (undocumented) properties that control transparency of line objects appear to be set correctly when produced in an mlx file. Even when I set those values after the lines are rendered, there is no effect.
h(1) = plot([0 1],[0 1],'-','Color',[1 0 0 .25],'LineWidth',20);
drawnow
h(1).Edge.ColorBinding
% ans =
% 4×1 uint8 column vector
% 255
% 0
% 0
% 64
h(1).Edge.ColorType
% ans =
% 'truecoloralpha'
h(1).Edge.ColorBinding
% ans =
% 'object'
Noam
Noam el 4 de Abr. de 2023
still having this problem in Windows 2022b, very annoying

Iniciar sesión para comentar.

Respuestas (1)

Adam Danz
Adam Danz el 4 de Abr. de 2023
Editada: Adam Danz el 7 de Abr. de 2023
The RGBA color definition for lines is undocumented and does not work with MLX files. The line transparency also will not appear in regular figures if you save and load the figure.
A workaround is to use patch. Here's an anonymous function you can adapt to work like plot() or line().
lineAlphaFcn = @(x,y,style,width,color,alpha) patch('XData',[x(:);nan],'YData',[y(:);nan],'EdgeColor',color,'EdgeAlpha',alpha,'LineStyle',style,'LineWidth',width);
% x: vector of x data
% y: vector of y data
% style: linestyle (e.g. '-')
% width: linewidth (e.g. 1)
% color: color (e.g. 'r' | [1 0 0])
% alpha: transparency level 0:1
%
% Example
% h = lineAlphaFcn(x, y, '-', 2, 'r', 0.3);
Applied to OP's demo,
lineAlphaFcn([0 1],[0 1],'-',20,[1 0 0], 0.25);
hold on
lineAlphaFcn([0 1],[1 0],'-',20,[1 0 0], 0.25);
box on
Another example,
th = linspace(0,2*pi,150)';
r = linspace(0,3,10);
n = numel(r);
color = cool(n);
figure()
hold on
for i = 1:n
h = lineAlphaFcn(th, sin(th+r(i)), '-', 12, color(i,:), 0.25);
end
This is the same approach I used to generate the 10,000 partially transparent line segments using a single patch object in this polar plot of pi

5 comentarios

I see, in my case I am trying to draw transparent circles, would it be easy to adapt this code which uses rectangle to use patch?
(note: while this is working correctly here rendered 2023a, it does not work in livescript in 2022a which is that I am using)
PS. Your plot of Pi is phenomenal!
figure;
color = [0 0 1 0.5];
pos = [0 0 10 10];
rectangle('Position',pos,'Curvature',[1 1],'FaceColor', color, 'Edgecolor' ,color);
color = [1 0 0 0.5];
pos = [3 3 10 10];
rectangle('Position',pos,'Curvature',[1 1],'FaceColor', color, 'Edgecolor' ,color);
Adam Danz
Adam Danz el 4 de Abr. de 2023
> would it be easy to adapt this code
Yes. patch() requires xdata and ydata which are the verticies of the rectangle. The Position argument for rectangle defines the [x,y,width,height] so you just need to compute the x and y verticies instead.
If you get stuck, I can help more.
Noam
Noam el 5 de Abr. de 2023
Hi Adam, thank you for the reponse! My goal is actually to draw a circle, not a rectangle, I use the 'Curvature' property in the rectangle() function to draw a circle (you can see this in my last comment), would it be possible to draw a circle similarly with patch()? Thank you for the help!
Ok the following code seems to do what I want and work in livescript, thank you!
color = [0 0 1 0.5];
t = linspace(0, 2*pi);
r = 10;
x = r*cos(t) + 5;
y = r*sin(t) + 5;
figure;
patch('XData',x,'YData',y,'EdgeColor',color(1:3),'FaceColor',color(1:3),'FaceAlpha',color(4));
color = [1 0 0 0.5];
x = r*cos(t) + 3;
y = r*sin(t) + 3;
patch('XData',x,'YData',y,'EdgeColor',color(1:3),'FaceColor',color(1:3),'FaceAlpha',color(4));
Duijnhouwer
Duijnhouwer el 11 de Abr. de 2023
Editada: Duijnhouwer el 11 de Abr. de 2023
Nice, @Adam Danz! I did not know use of RGBA is undocumented. So it's fair enough it doesn't work in Live Editor. I'll use patch objects in future. Thanks!

Iniciar sesión para comentar.

Productos

Versión

R2020a

Preguntada:

el 21 de Feb. de 2021

Editada:

el 11 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by