Issue with legend colours in scatter plots with tranparency set 'ON'

3 views (last 30 days)
Hi,
I had set the Transparency to my scatter plot as shown in the below code. Unfrtunately, the marker colors are not visible in the legend. Is there any way to avoid this?
scatter (A(:,1),B(:,2),'MarkerEdgeAlpha',0.3,'MarkerFaceAlpha',0.4,'MarkerEdgeColor',C{1})
  2 Comments
Voss
Voss on 15 Mar 2024
The legend looks fine here:
A = [1;2;3];
B = [4 4; 5 5; 6 6];
C = {[1 0 1]};
scatter(A(:,1),B(:,2),'MarkerEdgeAlpha',0.3,'MarkerFaceAlpha',0.4,'MarkerEdgeColor',C{1})
legend
Turbulence Analysis
Turbulence Analysis on 15 Mar 2024
Hi,
That's with when MarkerEdgeAlpha and MarkerfaceAlpha being set to gerather than 0.4. But If I set the both to 0.1, the marker on the legend becomes very dull/ invisble

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Mar 2024
The legend function has at least two other undocumented outputs (those are all that I experimented with, anyway), that can change the appearance of the legend marker and other properties.
Apparently just calling it with outputs is enough to set the marker in the legend to have an 'EdgeAlpha' property of 1, since that’s all I did here —
A = randn(4,1);
B = randn(4,2);
C = {[1 0 1]};
scatter (A(:,1),B(:,2),'MarkerEdgeAlpha',0.3,'MarkerFaceAlpha',0.4,'MarkerEdgeColor',C{1})
[hl1,hl2,hl3] = legend('Location','best');
The extra outputs have been used elsewhere to change the size of the marker in the legend. Setting the 'EdgeAlpha' property to 1 in one of those outputs (I believe it was ‘hl2(1)’) set the 'MarkerEdgeAlpha' property for all the markers to 1, obviously not the desired appearance. Otherwise, the legend marker seems to inherit all the marker properties that were originally set.
It could be worthwhile to explore those extra undocumented outputs (in other functions as well) to see what other options might be available.
Use the get function to see all the properties in each handle.
.

More Answers (1)

Voss
Voss on 15 Mar 2024
Edited: Voss on 15 Mar 2024
The marker in the legend will be the same as what's in the plot. If you want them to be different, you can create a line with NaN data that's only used in the legend. Example:
A = [1;2;3];
B = [4 4; 5 5; 6 6];
C = {[1 0 1]};
scatter(A(:,1),B(:,2),'MarkerEdgeAlpha',0.1,'MarkerFaceAlpha',0.1,'MarkerEdgeColor',C{1})
h = line(NaN,NaN,'LineStyle','none','Marker','o','MarkerEdgeColor',C{1});
legend(h);

Community Treasure Hunt

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

Start Hunting!