Mask area outside of imported shapefile (basemap/matplotlib)

2024/10/10 10:26:45

I'm plotting data on a basemap of the eastern seaboard of the U. S. and Canada through Matplotlib. In addition to the base layer (a filled contour plot), I overlayed a shapefile of this focus region atop the data using Matplotlib's readshapefile tool.

I'm wondering how I can mask all the gridded data outside of the shapefile. I can obviously do a maskocean command through Matplotlib, but I'd still be left with the filled contours west of the St. Lawrence. Does anyone now how to do this? I haven't had much luck searching online.

def make_map(lon,lat,param):fig, ax = plt.subplots()ax.axis('off')x1 = -83.x2 = -57.y1 = 37.y2 = 50.projection='merc'resolution='h'm = Basemap(projection=projection, llcrnrlat=y1, urcrnrlat=y2, llcrnrlon=x1,urcrnrlon=x2, resolution=resolution)x,y = m((lon-360.),lat)m.ax = axmy_cmap = cm.get_cmap('coolwarm')pp = m.contourf(x, y, param, 30, cmap=my_cmap, extend='both')  m.drawmapscale(-67, 39.5, -70, 43.5, 500, fontsize=8, barstyle='fancy') return fig, m, x, ydef drawstates(ax, shapefile='../StateProv_UTMrp'):shp = m.readshapefile(shapefile, 'states',zorder = 1, drawbounds=True)for nshape, seg in enumerate(m.states):poly = Polygon(seg, facecolor='w',alpha=0.0, edgecolor='k')ax.add_patch(poly)fig, m, x, y = make_map(lon, lat, param)
drawstates(m.ax)
Answer

I think this article I just found could give you some help. But I am not sure it is a full answer.

http://basemaptutorial.readthedocs.org/en/latest/clip.html

https://en.xdnf.cn/q/69908.html

Related Q&A

Python Glob.glob: a wildcard for the number of directories between the root and the destination

Okay Im having trouble not only with the problem itself but even with trying to explain my question. I have a directory tree consisting of about 7 iterations, so: rootdir/a/b/c/d/e/f/destinationdirThe …

Get datetime format from string python

In Python there are multiple DateTime parsers which can parse a date string automatically without providing the datetime format. My problem is that I dont need to cast the datetime, I only need the dat…

Generating an optimal binary search tree (Cormen)

Im reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in…

Pydub from_mp3 gives [Errno 2] No such file or directory

I find myself in front of a wall here, simply trying to load an audio file into pydub for converting it keeps on throwing a "[Errno 2] No such file or directory" error.Naturally I have spent …

Compile Python 3.6.2 on Debian Jessie segfaults on sharedmods

Im trying to compile Python 3.6.2 on a Debian Jessie box with the options./configure --prefix="/opt/python3" \ --enable-optimizations \--with-lto \ --enable-profiling \ --enable-unicode=ucs4 …

Escape space in filepath

Im trying to write a python tool that will read a logfile and process itOne thing it should do is use the paths listed in the logfile (its a logfile for a backup tool)/Volumes/Live_Jobs/Live_Jobs/*SCAN…

How to save web page as text file?

I would like to save a web page (all content) as a text file. (As if you did right click on webpage -> "Save Page As" -> "Save as text file" and not as html file)I have tried …

PIL Image.size returns the opposite width/height

Using PIL to determine width and height of imagesOn a specific image (luckily only this one - but it is troubling) the width/height returning from image.size is the opposite. The image: http://storage.…

Django Rest Framework: Register multiple serializers in ViewSet

Im trying to create a custom API (not using models), but its not showing the request definition in the schema (in consequence, not showing it in swagger). My current code is:views.pyclass InfoViewSet(v…

Which is most accurate way to distinguish one of 8 colors?

Imagine we how some basic colors:RED = Color ((196, 2, 51), "RED") ORANGE = Color ((255, 165, 0), "ORANGE") YELLOW = Color ((255, 205, 0), "YELLOW") GREEN = Color ((0, 128…