MATLAB Answers

Marker transparency AND marker size can't be set simultaneously in legends

2 views (last 30 days)
z8080 on 5 Feb 2020
I've been trying to control both the marker size and their transparency, in my legend. It seems to me this cannot be done, at least in 2016a, as they require passing two vs just one outputs from 'legend'. The code below shows a minimal example, with comments. Any help would be appreciate on how these both aims can be achieved simultaneously.
close all
x = 1:10;
plot1 = scatter(x, 1*x, 190, 'ob');
plot1.MarkerFaceColor = [1 0 0];
hold on;
plot2 = scatter(x, 1.05*x, 190, 'sr');
plot2.MarkerFaceColor = [0 0 1];
% make markers transparent
plot1.MarkerFaceAlpha = 0.2;
plot1.MarkerEdgeAlpha = 0.2;
plot2.MarkerFaceAlpha = 0.2;
plot2.MarkerEdgeAlpha = 0.2;
% with one attributed variable, legend markers inherit transparency from plot, but marker size cannot be set
objh_icons = legend({'one','two'});
PatchInLegend = findobj(objh_icons, 'type', 'patch');
set(PatchInLegend, 'Markersize', 23);
title('One passed variable: Transparency YES, marker size NO')
% with two attributed variables, legend markers no longer inherit transparency from plot (and the commands below to set it manually don't work), but their size can now be set
[objh, objh_icons] = legend({'one','two'});
PatchInLegend = findobj(objh_icons, 'type', 'patch');
set(PatchInLegend, 'Markersize', 23);
set(PatchInLegend, 'facea', 0.2)
set(PatchInLegend, 'edgea', 0.2)
PatchInLegend(1,1).FaceAlpha = 0.2;
PatchInLegend(1,1).EdgeAlpha = 0.2;
PatchInLegend(2,1).FaceAlpha = 0.2;
PatchInLegend(2,1).EdgeAlpha = 0.2;
title('Two passed variables: Transparency NO, marker size YES')


Sign in to comment.

Answers (0)





Community Treasure Hunt

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

Start Hunting!