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

44 次查看(过去 30 天)
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 个评论
jonas
jonas 2018-5-15
编辑:jonas 2018-5-15
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 2018-5-15
Yes, that is what I want, unfortunately the base for the hatching is still showing up under the hatching even with fill off.

请先登录,再进行评论。

回答(1 个)

jonas
jonas 2018-5-15
编辑:jonas 2018-5-15
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 个评论
Niccolò Moro
Niccolò Moro 2020-6-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by