How to plot a hatched data set over a contour plot?

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
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.
Hello, I have tried a couple of methods, the most success I have gotten was with hatchfill2 but it leaves behind the patch as well (so cant see where the hatching is on the original graph). So after the original contour and hold on, I have:
[c1,h1]=contourf(x,y,z,[0 0]-10);
set(h1,'linestyle','none','Tag','HatchingRegion');
ax1=gca;
hp=findobj(ax1,'Tag','HatchingRegion');
set(h1,'linestyle','none');
hh=hatchfill2(hp,'cross');
I have also tried hatchfill, which doesn't seem to work at all:
c1,h1]=contourf(x,y,z,[0 0]-10);
hold off
hPatch1=findobj(h1, 'type', 'patch');
hh1=hatchfill(hPatch1, 'cross');
set(h1,'linestyle','none');
set(hh1, 'color','black');
Any help on how to get either of them to work would be appreciated.
Thank you!
jonas
jonas am 15 Mai 2018
Bearbeitet: jonas 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
HA am 15 Mai 2018
Yes, that is what I want, unfortunately the base for the hatching is still showing up under the hatching even with fill off.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

jonas
jonas am 15 Mai 2018
Bearbeitet: jonas am 15 Mai 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 Kommentare

HA
HA am 15 Mai 2018
Hi, I am unfortunately still getting the base fill under the hatching, even with fill off, meaning I cannot see the contourf plot beneath. Thank you.
jonas
jonas am 15 Mai 2018
Bearbeitet: jonas 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
HA am 15 Mai 2018
Bearbeitet: HA am 15 Mai 2018
That one works fine, its just for the first example using my own data, if it comes to it I can probably delete the patch behind in plot editor. Unfortunately, I cannot attach a figure due to data copyright.
HA
HA am 15 Mai 2018
I am wondering if it could have something to do the function not liking nans, as my hatch matrix is the same size as as my background matrix, just with nans where I do not have a value to plot.
jonas
jonas am 15 Mai 2018
Bearbeitet: jonas 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
HA am 15 Mai 2018
Great, thank you I will give this a try!
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Glaciology finden Sie in Hilfe-Center und File Exchange

Gefragt:

HA
am 15 Mai 2018

Kommentiert:

am 8 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by