Setting overlap colours in plot with transparent layers

22 views (last 30 days)
I have a plot with 3 filled surfaces, generated by commands such as
band(i_condition) = fill(xconf,yconf_CI95, colours(i_condition, :), 'FaceAlpha',alpha, 'LineStyle','-');
colours = [
213,94,0; % M
86,180,233; % L
166,166,166; % M&L
] / 255;
A legend is added using
conditions = {'M', 'L', 'M&L'};
[lh,icons] = legend(band, conditions, 'FontSize',23, 'units','normalized', 'Location', 'Best');
PatchInLegend = findobj(icons, 'type', 'patch');
set(PatchInLegend, 'facea', alpha)
The result looks like this
As can be seen, the parts of the bands that overlap are coloured rather ambiguously, and it's hard to tell them apart from the 3 conditions whose colours are specified. I assume the combinations of colours for the overlaps are computed automatically by Matlab, but is there a way to override those, or in some other way control them, so that the overlap areas end up being more distinctively coloured?
Also, is there a way for those overlap colours to themselves feature in the legend, so that it's 100% clear for the reader?
Thanks for any help!

Accepted Answer

DGM on 16 Aug 2022
Edited: DGM on 16 Aug 2022
When using alpha properties to visualize the intersection of graphics objects, there isn't really a way to make the intersection color independent of the contributing colors. The intersection is simply a weighted mean of the two colors.
You might be able to do something like this example, but adjusting your workflow from using fill() to building point lists for using patch() might be a bit of a hoop to jump through.
Alternatively, if you use fill(), you might be able to get better visual contrast by starting with more visually-distinct colors to begin with. Regardless of the selected colors, you could construct the legend for the overlap area without explicitly needing to:
x = [1 3 4 3 1 0];
y = [0 0 2 4 4 2];
color = [1 0 0; 0 1 0; 0 0 1];
alpha = [0.3; 0.3; 0.3];
hold on
fh(1) = fill(x,y,color(1,:),'FaceAlpha',alpha(1));
fh(2) = fill(x+2,y,color(2,:),'FaceAlpha',alpha(2));
fh(3) = fill(x+1,y+2,color(3,:),'FaceAlpha',alpha(3));
fh = fliplr(fh);
% create legend entries for intersection regions without actually generating the intersection poly directly
color12 = (color(1,:)*alpha(1) + (1-alpha(1)))*(1-alpha(2)) + color(2,:)*alpha(2);
color23 = (color(2,:)*alpha(2) + (1-alpha(2)))*(1-alpha(3)) + color(3,:)*alpha(3);
color31 = (color(3,:)*alpha(3) + (1-alpha(3)))*(1-alpha(1)) + color(1,:)*alpha(1);
color123 = ((color(1,:)*alpha(1) + (1-alpha(1)))*(1-alpha(2)) + color(2,:)*alpha(2))*(1-alpha(3)) + color(3,:)*alpha(3);
pint12 = patch('xdata',x(1)*[1 1 1],'ydata',y(2)*[1 1 1],'FaceColor',color12); % dummy objects
pint23 = patch('xdata',x(1)*[1 1 1],'ydata',y(2)*[1 1 1],'FaceColor',color23);
pint31 = patch('xdata',x(1)*[1 1 1],'ydata',y(2)*[1 1 1],'FaceColor',color31);
pint123 = patch('xdata',x(1)*[1 1 1],'ydata',y(2)*[1 1 1],'FaceColor',color123);
% set up legend
legend([fh pint12 pint23 pint31 pint123],'p1','p2','p3','p1 ∩ p2','p2 ∩ p3','p3 ∩ p1','p1 ∩ p2 ∩ p3');
  1 Comment
z8080 on 17 Aug 2022
Excellent, thanks so much for this very helpful answer.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!