Getting hatchfill to properly display a patch legend
Show older comments
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

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
More Answers (3)
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
Zafimandimby Mampiandra
on 28 Apr 2022
I cannot use the function "hatchfill2" it shows that it is not defined.
Can you please help me o solve this problem?
Thanks
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.
kailin gong
on 9 May 2023
Edited: kailin gong
on 9 May 2023
Hi, I use hatchfill2 to plot a mixed graph, but I feel confused about how to display a patch legend with other style legend, such as line.
Based on the previous mentioned code.
I add a line:
line=plot([1,2],[5,10]);
I changed the legend:
legendData = {'Bar #1', 'Bar #2', 'Line'};
Then I changed:
[legend_h, object_h, plot_h, text_str] = legendflex([bars, line], legendData, 'Padding', [2, 2, 10], 'FontSize', 18, 'Location', 'NorthEast');
It turns out an error:
Error using hatchfill2>parse_input (line 856)
Unsupported graphics handle type.
Error in hatchfill2 (line 98)
[A,opts,props] = parse_input(A,varargin);
Please help, thanks in advance.
4 Comments
Hi Kailin, that's a great question which highlights how the legendflex works.
Try plotting again but type this into the terminal: "object_h", it'll give an output like this:
Text (Bar #1)
Text (Bar #2)
Text (Line)
Group (Bar #1)
Group (Bar #2)
Line (Line)
Line (Line)
So you now know that the two legendflex indexes that you want are 4 and 5 (not 3 and 4), look for:
4: Group (Bar #1)
5: Group (Bar #2)
So once you apply the hatchfill2 to those two legend patches, like so:
hatchfill2(object_h(4), 'cross', 'HatchAngle', 45, 'HatchDensity', 60/4, 'HatchColor', 'black');
hatchfill2(object_h(5), 'single', 'HatchAngle', 45, 'HatchDensity', 40/4, 'HatchColor', 'black');
You should no longer get any error. The full code will look something like this:
% 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');
line = plot([0, 1, 2, 3], [50 30, 60, 30], 'Color', '#615285', 'LineWidth', 3)
% Keep an array of the plots so that none of the hatchfill2 lines appear in the legend
plots = [bar1, bar2, line];
% 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', 'Line'};
[legend_h, object_h, plot_h, text_str] = legendflex([plots, line], 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 third line text
% object_h(4) is the first bar's patch
% object_h(5) is the second bar's patch
% object_h(6) is the third line patch
% Set the two patches within the legend
hatchfill2(object_h(4), 'cross', 'HatchAngle', 45, 'HatchDensity', 60/4, 'HatchColor', 'black');
hatchfill2(object_h(5), '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]);

Hope this helps.
Simeon
kailin gong
on 9 May 2023
So nice of you, thanks very much. I found another problem, when I change the 'Location' to 'southwest', there is no any change for the legend location. Is there anything wrong with the legendflex function?
[legend_h, object_h, plot_h, text_str] = legendflex([plots, line], legendData, 'Padding', [2, 2, 10], 'FontSize', 18, 'Location', 'southwest');
Simeon
on 12 May 2023
Right, the positioning for legendflex is strange also, if you look here:
You'll see that:
- North --> 'anchor', [2 2], 'buffer', [0 -10]
- South --> 'anchor', [6 6], 'buffer', [0 10]
- East --> 'anchor', [4 4], 'buffer', [-10 0]
- West --> 'anchor', [8 8], 'buffer', [10 0]
- NorthEast --> 'anchor', [3 3], 'buffer', [-10 -10]
- NorthWest --> 'anchor', [1 1], 'buffer', [10 -10]
- SouthEast --> 'anchor', [5 5], 'buffer', [-10 10]
- SouthWest --> 'anchor', [7 7], 'buffer', [10 10]
- NorthOutside --> 'anchor', [2 6], 'buffer', [0 10]
- SouthOutside --> 'anchor', [6 2], 'buffer', [0 -10]
- EastOutside --> 'anchor', [3 8], 'buffer', [10 0]
- WestOutside --> 'anchor', [8 3], 'buffer', [-10 0]
- NorthEastOutside --> 'anchor', [3 1], 'buffer', [10 0]
- NorthWestOutside --> 'anchor', [1 3], 'buffer', [-10 0]
- SouthEastOutside --> 'anchor', [5 7], 'buffer', [10 0]
- SouthWestOutside --> 'anchor', [7 5], 'buffer', [-10 0]
Also, when saving the figure use ctrl+s instead of clicking the save icon, since unlike the traditional legend, the legendflex legend is its own figure, so you gotta save both of together to have the legend show up in the resulting plot.
Ralph van Zwieten
on 29 Sep 2023
Hi Simeon,
I have a related question. I am trying to apply the hatchfill to a stacked bar plot that I have, where I keep on running into several errors.
Could you maybe have a look at it?:
LO = [0.1, 0.2, 0.3, 0.4];
OLO = [0.2, 0.3, 0.4, 0.1];
RGO = [0.3, 0.4, 0.1, 0.2];
SFO = [0.4, 0.1, 0.2, 0.3];
comp = [LO; OLO; RGO];% SFO];
comp = oil_comp*100; %
oi = categorical ({'Li', 'Ol', 'Ri'});%, 'Su'});
oi = reordercats(oi, {'Li', 'Ol', 'Ri'});%, 'Su'});
str = {'Li', 'Ol', 'Ri'};
Y= oil_comp;
bar(oi, Y, 'stacked');
ylim([0 102])
ax = get(gca);
cat = ax.Children;
set(cat(4),'FaceColor',[21 96 189]/255);
set(cat(3),'FaceColor',[30 144 255]/255);
set(cat(2),'FaceColor',[130,238,253]/255);
%set(cat(1),'FaceColor',[173 216 230]/255);
set(cat(1),'FaceColor',[255 204 203]/255);
% Hatch the two bars with a texture
hatchfill2(cat(1), 'single', 'HatchAngle', 45, 'HatchDensity', 60, 'HatchColor', 'black');
hatchfill2(cat(2), 'single', 'HatchAngle', 45, 'HatchDensity', 40, 'HatchColor', 'black');
Ultimately, I would like to have the bottom box of every bar with the same hatch density , and then increasing density when going to the boxes above that.
If you know how to give every box an individual color, so having 12 instead of 4 different colors), that would be great to hear as well!
Hopefully you can help me out, thank you in advance.
Asvin Kumar
on 26 Nov 2019
0 votes
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
Leo Simon
on 28 Nov 2019
Christopher Bitikofer
on 7 Jan 2022
checking out legendflex. Thanks Leo for posting this!!!
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
