How to plot a hatched data set over a contour plot?
24 views (last 30 days)
Show older comments
I have contour plot, that I would like to add another layer to, this layer displays a region of significance, therefore it would make sense to have it as a hatched area over my original data set. I have tried a couple of the 'hatchfill' functions for the file exchange but they do not seem to work (or at least I am probably doing them wrong).
In addition to this I also have plots that I created using the 'pcolorps' function from the Antarctic toolbox (file exchange), If anyone has had experience adding hatched markings to these plots that would also be great, as I think it may be slightly different.
4 Comments
jonas
on 15 May 2018
Edited: jonas
on 15 May 2018
Just to clarify. You have a contour plot and you want to plot a transparent hatched pattern on top of the original contour plot, over a defined region on the surface? For example:
contourf(1:10,1:10,rand(10,10))
hp=patch([4 6 6 4],[4 4 6 6],'r')
hatchfill(hp);
Something like that?
EDIT: in your first example which almost works, try setting 'fill' to 'off'
hh = hatchfill2(hp,'cross','LineWidth',1,'Fill','off');
Answers (1)
jonas
on 15 May 2018
Edited: jonas
on 15 May 2018
If you want to hatch an area based on your data, then try something like this:
figure;hold on
[x,y]=meshgrid(1:49,1:49)
z=peaks;
[~,h1]=contourf(x,y,z)
[~,h2]=contourf(x,y,z,[1.5 1.5])
set(h2,'linestyle','none','Tag','HatchingRegion','fill','off');
hp = findobj(h2,'Tag','HatchingRegion');
hh = hatchfill2(hp,'cross','LineWidth',1,'Fill','off');
h1 is the handle of your 'background' and h2 is the handle of your hatched plot. The vector [1.5 1.5] sets the 'z-value' for your hatch.
If you instead want to hatch a defined region, then just use patch instead:
figure;
contourf(x,y,z)
hp=patch([4 20 20 4],[20 20 6 6],'r')
hf=hatchfill2(hp);
hp.FaceColor='none'
Hope it helps
7 Comments
Niccolò Moro
on 8 Jun 2020
Hi Jonas,
I am far from a matlab pro and trying to implement your option in a similar context. I have a contour map on top of which I'd like to hatch all values of another contour higher than a certain threshold.
This is the current plot:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/312248/image.png)
I have population data (with the same meshgrid as the already plotted contour). I'd like to mark regions with high population density and regions with even higher densities.
I keep getting the error "Unsupported graphics handle type." on the fourth last of those lines:
At the moment this is the code:
[~, h2]=contourf(xll, yll, concTotal, [1.5 1.5], 'linecolor', 'none'); % tbh i don't understand what the [1.5 1.5] does. Is this where I could set the thresholds?
set(h2,'linestyle','none','Tag','HatchingRegion','fill','off');
hp = findobj(h2,'Tag','HatchingRegion');
hh = hatchfill2(hp,'cross','LineWidth',1,'Fill','off');
[~, hContour]=contourf(xll, yll, concTotal, levels, 'linecolor', 'none'); % this is the already plotted contour
colormap
colorbar
Thanks in advance for some help.
See Also
Categories
Find more on Contour Plots 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!