2
votes

I plotted a set of contour lines and over them, a shapefile with a map shape similar to the ones found here.

F = TriScatteredInterp(x,y,z);
[qx, qy] = meshgrid(1:.01:10,1:.01:10); 
qz = F(qx, qy);
contour(qx, qy, qz, 10); hold on; 
plot([shp.X],[shp.Y],'k'); axis equal

However, since the countour is defined over a square region that goes outside the limits of the map (shapefile), it doesn't look nice.

Is there any way I can cut/trim/hide the contour lines that fall outside the limits of the map to have all the contour lines contained within the map?

Thanks!

1

1 Answers

1
votes

You can use the axis keyword to constrain the limits of your contour plot. If you get these limits from your shape data this ought to let you crop the image as required:

xmin = min(min(shp.X)); xmax = max(max(shp.X);
ymin = min(min(shp.Y)); ymax = max(max(shp.Y));
axis([xmin, xmax, ymin, ymax]);

alternatively you may find the clipping option for contour is sufficient.

UPDATE: The above will clip the contour plot to the bounding box of the shapefile, but if you want the contours to show up only inside the shape itself then it's a touch more complicated. You'd want to create a rectangular patch the size of your desired axes limits with a hole in the middle defined by your shape data.

If you have the mapping toolbox, which is at least possible given the nature of your question, then you can use poly2fv. You would make two polygons, one the size of your plot range

x1 = [xmin xmin xmax xmax xmin];
y1 = [ymin ymax ymax ymin ymin];

and the other defined by your shape data, then convert to faces and vertices

[f, v] = poly2fv({x1, x2}, {y1, y2});

and then plot with patch

patch('Faces', f', 'Vertices', v, 'FaceColor', 'w'); 

where you can change the FaceColor to match your plot background.

If you don't have the mapping toolbox then you might have to make the patches by hand. There are other tricks involving alpha masks but they work on image data rather than plots.