Color area outside of polygon/shapefile?

Hi,
I have a shapefile of the U.S.. I am simply looking to fill the outside of this shapefile with a particular color. I'm sure there is a simple solution but cannot find..
Thank you!

Risposte (2)

Hi Ryan,
Perhaps you're looking for setm with the 'ffacecolor' option.
worldmap('world')
geoshow('landareas.shp')
setm(gca,'ffacecolor',rgb('ocean blue'))
Above I used rgb to get the rgb values of ocean blue.

8 Commenti

Hi Chad,
Is there another workaround? That last line of code doesn't seem to be working for me.. even if instead of rgb('something') I used a vector [ 1 1 1].
[1 1 1] is white, so maybe you're not seeing it? [0 0 1] is blue, so maybe try that?
I wish it were that easy.. basically these are a few lines of code and an image of what I am trying to do...
latlim = [lat(1,1) lat(end,1)];
lonlim = [lon(1,1) lon(1,end)];
mapp = shaperead('Shapefiles/USA_adm1.shp', 'UseGeoCoords',true);
mappCty= shaperead('Shapefiles/USA_adm2.shp', 'UseGeoCoords',true);
pcolorm(lat,lon,flipud(drydays))
geoshow(mapp,'facecolor','none','LineWidth',1.5)
geoshow(mappCty,'facecolor','none','LineWidth',0.5)
setm(mapp,'ffacecolor',[0 0 1])
It then reports this error:
"Error using setm (line 39) Expected H to be a handle to a MATLAB graphics object."
Is there a step I am missing in this process?
Right, you're trying to set mmap, which is a shapefile. Try
setm(gca,'ffacecolor',[0 0 1])
We're only setting the color of the background axes.
Thanks Chad. I appears that my problem is my data consumes the entire domain and is just not confined to the U.S. borders. Any idea on how to suppress this data outside of the borders?
pcolorm(lat,lon,flipud(drydays))
Like you said, the setm(gca,'ffacecolor',[0 0 1]) sets the background but then when I overlay the data this background disappears.
Okay, so maybe you are trying to make some of your gridded data transparent so you can see the underlying background color? If yes, set the gridded data to NaN where you want to see the background. Here' I'm using landmask to get a logical matrix containing ones for all land areas. I'll set the gridded data to NaN for every grid point that is not land.
Z = flipud(drydays);
land = landmask(lat,lon);
Z(~land) = NaN;
pcolorm(lat,lon,Z)
Thanks for this Chad! Its a step in the right direction however I noticed that the resolution isn't very good on it. I may have to find another solution.
If you have a shapefile, you can simply determine which pixels in your lat/lon grid fall within the bounds of the shapefile's polygon using inpolygon. If your shapefile contains multiple polygons, use Kelly Kearney's inpolygons.

Accedi per commentare.

Erin Browne
Erin Browne il 19 Mar 2019
Hi I am wondering instead of setting the ocean color how would you do that for the land masses?

Richiesto:

il 9 Mar 2016

Risposto:

il 19 Mar 2019

Community Treasure Hunt

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

Start Hunting!

Translated by