Add legend item for overlapping semitransparent patch regions

5 views (last 30 days)
I am trying to figure out if there is a way to create a new legend item for region of overlap between two semitransparant patch objects. Looking at the semitransparent example on the patch page, it shows how patches can overlap. Below, I took the example and changed it so that the red patch is a lot less visible. I would now want a legend that says something like "condition 1", "condition 2" and "conditions 1 and 2". I want to add this to make the plot more clear to someone with colorblindness who might have trouble inferring the overlap.
Ideally adding this 3rd legend object this could be done without searching for the overlap region and making a 3rd patch. Im not sure if this is actually possible though currently in Matlab.
For the time being, I plan on just using this custom hatchfill function to avoid color issues.
v1 = [2 4; 2 8; 6 6.5];
f1 = [1 2 3];
figure
patch('Faces',f1,'Vertices',v1,'FaceColor','red','FaceAlpha',.3);
v2 = [2 4; 2 8; 8 8];
f2 = [1 2 3];
patch('Faces',f2,'Vertices',v2,'FaceColor','blue','FaceAlpha',.5);

Accepted Answer

Shivam Singh
Shivam Singh on 20 Aug 2021
As per my knowledge, MATLAB does not have a direct function to implement the legend of overlapping semi-transparent patch regions, because I think that finding overlap is not the intended property of "patch".
But you can figure out the overlapping shape and add legend to it. The following code might help you:
v1 = [2 4; 2 8; 6 6.5];
f1 = [1 2 3];
p1=patch('Faces',f1,'Vertices',v1,'FaceColor','red','FaceAlpha',.3);
v2 = [2 4; 2 8; 8 8];
f2 = [1 2 3];
p2=patch('Faces',f2,'Vertices',v2,'FaceColor','blue','FaceAlpha',.5);
%Finding intersection of two shapes V1 and V2
polyout = intersect(polyshape(v1),polyshape(v2));
%Creating a patch of the overlapping shape of p1 and p2
p_inter=patch(polyout.Vertices(:,1),polyout.Vertices(:,2),'k');
legend("p1","p2","p_intersection");
You can refer to the documentation of polyshape, intersect, patch for more information.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by