MATLAB Answers

Getting hatchfill to properly display a patch legend

26 views (last 30 days)
Leo Simon
Leo Simon on 5 Sep 2019
Answered: Leo Simon on 28 Nov 2019
I'm using hatchfill to create patterned hatches, as the above example illustrates.
However the legend shows a line rather than a patch. Is there any way to make it show a patch?
The attached image shows the output of the code below. The legend icon for the hatch patch should
look like the icon for the regular hatch
tmp.png
obj.X = 1 + [0, 0 , 1 , 1 ]
obj.Y = 1 + [0, 1 , 1 , 0 ]
obj.FaceColor = 'b';
obj.FaceAlpha = 0.4;
obj2.X = [0, 0 , 1/2 , 1/2 ]
obj2.Y = [0, 1/2 , 1/2 , 0 ]
obj2.FaceColor = 'g';
obj2.FaceAlpha = 0.4;
h = patch(obj);
k = patch(obj2);
hatchObj= hatchfill(h);
set(gca,'XLim',[0,3]);
set(gca,'YLim',[0,3]);
legend('hatch patch','patch')

  0 Comments

Sign in to comment.

Accepted Answer

Leo Simon
Leo Simon on 28 Nov 2019
It turns out that the problems I described in my original question can all be resolved by using the legendflex and hatchfill2 packages.
Here's an extension of my original code, and the picture that it generates.tmp.png
clear all;close all
%parameters of the first patch object
obj(1).X = 1 + [0, 0 , 1 , 1 ];
obj(1).Y = 1 + [0, 1 , 1 , 0 ];
obj(1).FaceColor = 'b';
obj(1).FaceAlpha = 0.2;
HatchColor{1} = [0.4,0.4,0.4];
HatchAngle{1} = 20;
HatchType{1} = 'single';
HatchDensity{1} = 20;
%Parameters of the first patch object
obj(2).X = 0.5*[0, 0 , 1 , 1 ];
obj(2).Y = 0.5*[0, 1 , 1 , 0 ];
obj(2).FaceColor = 'g';
obj(2).FaceAlpha = 0.2;
HatchColor{2} = [1,0,0];
HatchAngle{2} = 20;
HatchDensity{2} = 10;
HatchType{2} = 'cross';
%make the patches
for ii=1:2;
h(ii) = patch(obj(ii));
end;
%hatch the patches
for ii=1:2;
hHatch(ii) = hatchfill2(h(ii),HatchType{ii},...
'HatchAngle',HatchAngle{ii},...
'HatchDensity',HatchDensity{ii},...
'HatchColor',HatchColor{ii});
end;
%Make the legend
Legend = {'blue square','green square'};
[legend_h,object_h,plot_h,text_str] = legendflex(h,Legend,...
'anchor',{'se','se'},...
'buffer',[-90, 60 ],...
'fontSize',20);
%object_h is the handle for the lines, patches and text objects
%hatch the legend patches to match the patches
for ii=1:2;
hHatch(ii+2) = hatchfill2(object_h(ii+2),HatchType{ii}, ...
'HatchAngle',HatchAngle{ii},...
'HatchDensity',HatchDensity{ii},...
'HatchColor',HatchColor{ii});
end;
%I couldn't use hatchfill2 to set the FaceAlpha's so I did it manually
for ii= 1:2
object_h(ii+2).FaceAlpha = obj(ii).FaceAlpha;
end;

  0 Comments

Sign in to comment.

More Answers (1)

Asvin Kumar
Asvin Kumar on 26 Nov 2019
I presume you’re using the hatchfill function from File Exchange.
Internally, it calls the line function to create the hatch patterns. See lines 171 and 176 of the source code. It follows that the legend would only show line and patch. With this hatchfill function it won’t be possible to change the legend.

  1 Comment

Leo Simon
Leo Simon on 28 Nov 2019
Thanks Asvin, I've switched to hatchfill2 and combined it will legendflex. This combination enables me to do pretty much whatever I need. I've answered my own question below.

Sign in to comment.

Sign in to answer this question.