Getting hatchfill to properly display a patch legend

149 views (last 30 days)
Leo Simon
Leo Simon on 5 Sep 2019
Commented: Simeon on 28 Apr 2022
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')

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;
  2 Comments
Simeon
Simeon on 18 Mar 2022
Hi, this issue occurs because legendflex.m comes with a getpos.m, which you also need inside your directory.

Sign in to comment.

More Answers (2)

Simeon
Simeon on 18 Mar 2022
Edited: Simeon on 18 Mar 2022
Here's an example.
With its corresponding code.
% Requirements to get this working:
% hatchfill2.m: https://github.com/hokiedsp/matlab-hatchfill2/blob/master/hatchfill2.m
% legendflex.m: https://github.com/kakearney/legendflex-pkg/blob/master/legendflex/legendflex.m
% getpos.m: https://github.com/kakearney/legendflex-pkg/blob/master/setgetpos_V1.2/getpos.m
hold on;
bar1 = bar(1, 100, 'FaceColor', 'white');
bar2 = bar(2, 50, 'FaceColor', 'white');
% Keep an array of the plots so that none of the hatchfill2 lines appear in the legend
bars = [bar1, bar2];
% Hatch the two bars with a texture
hatchfill2(bar1(1), 'cross', 'HatchAngle', 45, 'HatchDensity', 60, 'HatchColor', 'black');
hatchfill2(bar2(1), 'single', 'HatchAngle', 45, 'HatchDensity', 40, 'HatchColor', 'black');
% Draw the legend
legendData = {'Bar #1', 'Bar #2'};
[legend_h, object_h, plot_h, text_str] = legendflex(bars, legendData, 'Padding', [2, 2, 10], 'FontSize', 18, 'Location', 'NorthEast');
% object_h(1) is the first bar's text
% object_h(2) is the second bar's text
% object_h(3) is the first bar's patch
% object_h(4) is the second bar's patch
%
% Set the two patches within the legend
hatchfill2(object_h(3), 'cross', 'HatchAngle', 45, 'HatchDensity', 60/4, 'HatchColor', 'black');
hatchfill2(object_h(4), 'single', 'HatchAngle', 45, 'HatchDensity', 40/4, 'HatchColor', 'black');
% Some extra formatting to make it pretty :)
set(gca, 'FontSize', 18);
set(gca, 'XMinorTick','on', 'XMinorGrid','on', 'YMinorTick','on', 'YMinorGrid','on');
xlim([0, 3]);
ylim([0, 110]);
I hope this helps someone out there!
  2 Comments
Simeon
Simeon on 28 Apr 2022
Hi Zafimandimby, Try running the hatchfill.m file so that Matlab recognizes its functions, then run your plotting script to use those functions.

Sign in to comment.


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.
  2 Comments
Christopher Bitikofer
Christopher Bitikofer on 7 Jan 2022
checking out legendflex. Thanks Leo for posting this!!!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!