File Exchange

image thumbnail


version (58.3 KB) by Kesh Ikuma
Fills an area with hatching or speckling (Updating original hatchfill by Neil Tandon)


Updated 11 Jan 2018

View Version History

View License

This submission updates the popular submission (#30733) by Neil Tandon to place hatching pattern on patch objects. The main algorithm to form hatching pattern remains mostly the same as the original while more formatting options are exposed to the user and hatchfill2 brings the support for HG2 graphics system (since R2014b).
* Supported base graphics objects: PATCH, HGGROUP, AREA, BAR, and CONTOUR objects are additionally supported. Pre-HG2 (pre-R2014b) MATLAB releases are no longer supported.
* Supports 3D surfaces. Note: Hatching would not come out correctly on a curved surface.
* Four types of hatching patterns:
- 'single' single lines
- 'cross' double-crossed hatch
- 'speckle' speckling inside the patch boundary (partial support)
- 'outspeckle' speckling outside the boundary (partial support)
* User-definable options are now set via parameter name/value pair input argument interface. Options include HatchAngle, HatchDensity, HatchOffset, HatchColor, HatchLineStyle, HatchLineWidth, SpeckleMarkerStyle, and SpeckleFillColor
* Active formatting - the hatch line object listens to the changes in the patch object or axes to keep the hatching true to user options all the time (sans print support at this time)
* Basic graphics object properties (Visible, Clipping, HitTest, Interruptible, BusyAction, and UIContextMenu) of the hatch line object are synchronized to the base patch object. Only Visible property can be de-linked by setting HatchVisible option to either 'on' or 'off'.

* Speckling multi-face PATCH and HG2 CONTOUR is not implemented correctly. If anyone needs this feature fixed, post a request and I'll see what I can do.

* Print support - implementing "printmode" similar to lateximage
* Multi-face speckle support - eliminating all the interior edges in gethgdata
* Legend support - HG2 is making this really challenging if not impossible. Users suggest using legendflex (FEX#31092 or at See the comments below by Drecalde & SeungHyoung Ryu for how-to. Thanks guys!!

Cite As

Kesh Ikuma (2021). Hatchfill2 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (67)

Jingli Xie

legend doesn't work well

Jingli Xie

how to patch dot

loai taouzlak

Tri Nguyen

It is a great tool. Thanks very much for you contribution.

However, please give a warning for this part instead of just ignoring a suspicious result.
%%% Line 420 - 423
[v,T,islog] = transform_data(ax,V(f,:),[]); % transform the face
if isempty(v) % face is not hatchable

In my case, `Vertices` contains some nan, but `Faces` doesn't. Then it would fail to filter out those nan values from `Vertices`, which leads to an empty `v`. The code run smoothly but no hatch is showing. It was very frustrating for me to know what's happening. If you can add warning, I believe it helps a lot.

when I make zoom on the hatched plot I lost the transparency of the background plot. Is that normal? Thank you!

Kristoffer Clasén

Great function! Long shot; Is there anyone that has a suggestion on how to convert line into patch or similar in this function to support "thicker" hatch lines that will blend nicely with the boundaries? Increasing linewidth on a tilted hatch on a barplot looks hideous


does it work with the rectangle function in Matlab? I tried but I got an error. Should this work?

Peirong Fan

Benjamin Larin

Samuel de Vries

mengying zhou

@ Danny Thanks for the wonderful work!!! it saves me a day!!


Thanks~ It is very useful.

Nathan Matsuda

Valentín Barral

I made a simple function to add a simple line to the legend entries. Of course it can be improved, but for my case is enough.

function hatchLegendEntry(legendObjects, entryIndex)
%hatchLegendEntry Applies a basic hatch to a legend entry in a bar diagram.
% Changes the square to two triangles.

obj = legendObjects(entryIndex);

l = findall(obj, 'type', 'patch');
v = get(l, 'vertices');

v2 = zeros(8, 2);

v2(1,:) = v(1,:);
v2(2,:) = v(2,:);
v2(3,:) = v(3,:);
v2(4,:) = v(1,:);

v2(5,:) = v(1,:);
v2(6,:) = v(4,:);
v2(7,:) = v(3,:);
v2(8,:) = v(1,:);

faces = zeros(2,4);
faces(1,:) = [1 2 3 4];
faces(2,:) = [5 6 7 8];

set(l, 'vertices', v2);
set(l, 'faces', faces);


An example:

[legend1, legendObjects]= legend(axes1,'show');
indexBar = 3;
indexBarLegendEntry = 12;

liu mengyu

Dear Developer:
Thanks a lot for your amazing work! When I use this function to hatch my bar figure with speckles, I found that the speckles will exceed the boundary of each bar and the speckles are randomly distributed in the bar. This two things make the figure looks ugly. So I wonder if there have any methods to solve my problems.


Good function, allowed me to generate hatching on a 3D plane patch. Biggest issue was it pushed the the patch to the origin even though the patch was parallel to the y-z plane.

This was easy enough to fix using the get(gca,'XData') and set(gca,'XData') once I have defined all non-NaN values to the x values I wanted.

Hamid Karani

Great job!

skywalker connor

Wenqiang Liu


This is very lovely function. This is well working, and I expect I will use this forever. The following code is an example of drawing bar with legend using legendflex.

h = bar(rand(10,3));
[~,legend_h,~,~] = legendflex(h,{'ex1','ex2','ex3'});

Kesh Ikuma

@Jiangpeng Cui - Since I don't have Mapping Toolbox, I cannot test with your code. Please attach FIG file to your MATLAB Answers post (or email me)

Jiangpeng Cui

A very powerful tool. But how to add hatching on a geo map? The code below does work.
[~,h] = contourfm(significant,R,[1 1]);
hp = findobj(gca,'Tag','HatchingRegion');

Kesh Ikuma

@Lydia - Depends on its underlying graphical object type. I'm not familiar with m_contourf, which doesn't appear to be a builtin MATLAB function. If you email me a saved figure which contains an m_contourf plot, I can look into it.

Lydia Keppler

Very cool, thanks! Is there a way to use this on a 2D map with coordinate gridlines? (using m_contourf instead of contourf)

Kesh Ikuma

@geopap - my bad, not PlotChildren, but EntryContainer property of legend object contains the handles to the legend objects

Kesh Ikuma

@geopap - Oh, now I see what you were talking about. Sorry for misunderstanding you. I don't know if I'll have time to work on this, but I've gained some understanding of the legend object since the inception of hatchfill2. If you are savvy with Matlab graphics, you can poke around at the legend's "PlotChildren" property, which contains the Bar objects on the legend. You should be able to run "hatchfill2" on these objects and put the resulting hatching line handles as the children of legend's "DecorationContainer" group object. This should do the trick as long as you don't change anything after that... Give it a try (and it would be great if you'd report back with what you've done!!)


You are right Kesh Ikuma, though, without a legend. Any working legend example would help!

Kesh Ikuma

BTW, the demo script has a bar plot example which works.


Can anybody give a working example with bars using legend? I could not use the Drecalde & SeungHyoung Ryu instructions... THANKS

Kesh Ikuma

Just uploaded Rev 7 with 3D surface support and major revamp on hatching engine. It should fix the bugs reported by @Roland and @Andrea. Post/email if you encounter any issues.

Andrea Jenney

I am having the same issue mentioned in the latter part of Matt Hill's comment. I have a contour that extends to the edge of the figure, but hatchfull shades in the inverse of the target area for these contour regions. Is there any way to work around this?


Obviously, from MATLAB version R2017b on the face property StripData is empty. This results in an error, when using hatchfill2 on bar-plots. I fixed the issue as follows (see line 773):

if ishghandle(A,'bar')
I = 1:4:( size(V, 1) + 1);
I = double(A.Face.StripData);

Not sure if this workaround covers all cases. It works for me so far. Good work anywa. Best, Roland


Matt Hills

Matt Hills

Is there a way of preventing the hatching from filling an interior contour that is below the limit we're asking for? For example, hatching the area of a donut shaped region constrained by 0.2 fills in the centre of the donut, although that area is < 0.2.

This doesn't happen with fill on, but with that setting, the background color used by fill covers up the figure I'm hatching over.

A related issue, with fill off, the code seems to struggle to hatch regions that span the entire width of the figure. It seems to hatch areas at random.. Is it required to manually set a contour along the axes? Or can fill be transparent? I can provide example code if it helps.

Thanks for any help, it's a really useful tool.

Kesh Ikuma

@Guan - What do you mean by "Map axes"? Send me a sample code illustrating what you want to hatch.

Guan Jian

Thank you, and is there any version for Map axes?

Inder Nath

Bob photonics

Doesn't work for patches in 3D, it creates an empty ZData matrix. That's a same because that's what I wanted to use it for to represent cross-sections


Thank you. Very clear and does exactly as expected.

Roberto Agromayor

Does exactly what it is supposed to

Jot We

Carlos Viegas

I figured out how to apply the hatchfill2 over legend_flex. In my case, my legend is plotted separatedly from main plot, this way things get easier:



hold off;

hp = findobj(gca,'type','patch');

I hope it can help others.


A Jenab

Kesh Ikuma

@Hanna - Your code with my data seems to be working just fine. Which the Matlab version are you using? If you wish, you can contact me directly, attaching your data so I can debug the issue.

Hannah Nissan

I'm having some problems with hatchfill2 with contourf. I find it doesn't always fill the same area as I specify in the contour.

here's my code:
[c1,h1]=contourf(lon,lat,var,[1 1],'LineStyle','none');

It looks like there could be some problem with the function here? Or can anyone spot something I've done wrong?


Kesh Ikuma

@Drecalde & SeungHyoung-Thanks for figuring out how to use legendflex with hatchfill2. Much appreciate it!

@Luke-I was able to retrieve the contour data from contour object in the new version (v2.3/r6). Please check it out

@Evan-If your NaN issue was with contour object, it should be fixed in the new version. If you encountered it with other origin object type, please post more details. Thanks.

SeungHyoung Ryu

@Drecalde, @Kesh Ikuma, and @Jing_KU. Drecalde's methods works well in my case with a little bit of change. By using legendflex that [legend_h,object_h,plot_h,text_strings]=outlegendflex({a,b,c}).
then maybe object_h has several elements and there exist bar class object; maybe object_h(4),(5),(6) in this case. and each of them has patch object as a children.
So hatchfill(object_h(4).Children,~~~) will change the image of legend labeld with "a".

SeungHyoung Ryu

Evan Kalina

I was attempting to plot a hatched region with a matrix that contained NaNs, and I had 'Fill' set to 'off'. The hatches kept disappearing from my plot. I replaced the NaNs with zeroes (which were below my hatching threshold), and the problem was fixed. FYI to anyone who encounters a similar difficulty. Great program overall.

Luke Davis

Definitely interested in a "contour2patch" function if you'd still consider writing one. Seems fairly critical to the usage.

Luke Davis

Extremely useful function, thanks. Is the "hatchspacing" argument in "points" units, or data units?

Casey Hilgenbrink



@Kesh Ikuma and @Jing_KU. I solved the issue of legends using legendflex.m from filexchange.
i take the second output from legendflex
and apply hatchfill2 to it.
Hope this work for you

Kesh Ikuma

@Jing_KU - Unfortunately, no, not yet. If you are using R2014a or earlier, there is an easy way I think, and I can help you with that. Otherwise, it's a long shot to make it work. Shoot me an email if you are interested in discussing further.


Did you solve the problem with the legend?
I cannot find a solution for showing filled patch in legend.

Really looking forward to your reply.



I still encounter the problem that hatches disappear when 'Fill' is set to 'off'.

Wieger IJntema

Using ylim worked! is also is much faster if ylim is defined.

Kesh Ikuma

@Wieger - I have my theory about the slowness, but you need to wait on that front. Meanwhile, the y-axis issue that you are having. Could it be possible to send me (or post here) the code that produces the symptom? (Find my contact under my profile.)

Wieger IJntema

Some how it very slow on my PC but that is not really a problem.

My y axes are getting set to 100 and -100. Any idea how to solve that?


sophisticated and useful. thank you.


Now correct files. Tested ok (R2015b).

Kesh Ikuma

@Mathew - Thank you for pointing that out *facepalm* It should be fixed now.


Kesh, You have uploaded the wrong files to the FEX.

Kesh Ikuma

@Bill - Try the latest version I just upped. FYI, there is still a problem---if Fill is 'off' hatching will not be redrawn when the axes is resized.

One option that may serve you well is contour2patch function. If you are interested, I can probably cook one up for you. Let me know.

Bill Wang

In MATLAB 2015b, the function seems not working. When I ran the demo (Example 1), it turns out that the hatched area is no longer transparent. However, when I tried set(h2,'fill','off'), both the hatches and the background color disappear. I will a lot appreciate if you can help solve the issue. Thanks in advance!

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Hatchfill, lateximage

Inspired: stipple

Community Treasure Hunt

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

Start Hunting!