transparency issue using "plot" function
7 views (last 30 days)
Show older comments
Hi:
I have two set of data, using the same function "plot", and implement 0.1 transparency, however, they looks very different, for the dataset 2, the transparency looks not as clear as the dataset 1, particular between time period from 9:30 am to 11:00 am, even though it has a less data density.
is there any reason leading this issue? below is the code to reproduce this issue:
figure('color','w')
plotObj1=plot(data1.time,data1.data,'color','b','LineStyle','-','LineWidth',1);
plotObj1.Color(4)=0.1;
title('data 1, transparency looks ok')
figure('color','w')
plotObj2=plot(data2.time,data2.data,'color','b','LineStyle','-','LineWidth',1);
plotObj2.Color(4)=0.1;
title('data 2, transparency looks bad')
Thanks!
Yu
0 Comments
Answers (1)
Adam Danz
on 10 Apr 2023
Applying transparency to line objects is not documented and leads to issues in some circumstances (e.g. live scripts and loading saved fig files). The recommended workaround is to use a patch object.
In this solution, an anonyous wrapper converts the plot(x,y) values to patch(x,y,...). Due to the non-uniform density of the line, some parts will appear darker than others.
load data1
load data2
lineAlphaFcn = @(x,y,color,alpha) patch([x(:);NaT(1,'TimeZone',x.TimeZone)],[y(:);NaN],'r','EdgeColor','b','EdgeAlpha',0.08, 'LineWidth',1,'FaceColor','none');
figure()
plotObj1=lineAlphaFcn(data1.time,data1.data);
title('data 1, transparency looks ok')
figure()
plotObj2=lineAlphaFcn(data2.time,data2.data);
title('data 2, transparency looks bad')
3 Comments
Adam Danz
on 10 Apr 2023
The transparency of the edges is working as expected. If many objects with the same transparency overlap, the section of overlap will naturally be darker. Look at the line in the second figure around 12:00 where the density is lowest. The line is quite faded from standard blue due to the EdgeAlpha.
You can decrease the EdgeAlpha value but at some point the line will be difficult to see in the sections with low density.
Walter Roberson
on 10 Apr 2023
The whole point of transparency is to blend together the new graphic with whatever graphic is "underneath" it. If you have a lot of lines in one place, then it is like putting a bunch of layers of colored tape: the more layers you put in, the less light gets through.
If what you want is a light color even in places where there is high density, then you should not be using transparency for that: instead you should just be using a non-transparent line of a lighter color.
See Also
Categories
Find more on Lighting, Transparency, and Shading 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!