How to plot a hatched data set over a contour plot?
Ältere Kommentare anzeigen
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 Kommentare
jonas
am 15 Mai 2018
Can you show what you have tried with hatchfill so far? I have no issue making a hatched fill over a contourf.
HA
am 15 Mai 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');
HA
am 15 Mai 2018
Antworten (1)
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 Kommentare
HA
am 15 Mai 2018
Even when you run the example in my answer? Could you attach a figure? See attachment for what it should look like.
HA
am 15 Mai 2018
I understand. It's difficult to pinpoint the issue without more information. However, I would check all handles connected to your axes to make sure you dont have duplicates.
ha = get(gca,'children')
will save three handles, one line and two contours. You can then set the fill-property of both contours to 'off', which should at least let you see the hatch pattern.
About the NaNs, I think the function can deal with them. However, you can easily set all NaNs to some arbitrary value just to make sure.
HA
am 15 Mai 2018
Niccolò Moro
am 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:

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.
Kategorien
Mehr zu Glaciology finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!