c186282
c186282

Reputation: 101

Place a ring on a cartopy plot projection

I want to place a ring on a map project. I would like to set the inner and outer radius and place the center anywhere I want.

The needed imports:

import cartopy
import cartopy.crs as ccrs
from cartopy import geodesic
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

from shapely import Polygon

I thought this would be real easy with the RotatedPole projection. All I would need to do is make two circles around the north pole then transform to where I would like to place my ring. Here is my ring function with some added inputs for testing:

The trans key is just for testing. When set to False then I skip the RotatedPole transformation and add some "junk" to the latitude values +fac*long**2 in the ring data such that I get more than just lines when I make a normal plot.

def ring(outer,inner,lat: float=0.0,long: float = 0.0,trans: bool=True):
    fac = 0 if trans else 0.0005
    npts = 8
    endpoint=True
    ring = np.array([[long,90-outer+fac*long**2] for long in      np.linspace(-180,180,npts,endpoint=endpoint)])
    ring = np.append(ring,[[0,90]],axis=0)
    ring = np.append(ring,np.array([[long,90-inner-fac*long**2] for long in np.linspace(-180,180,npts,endpoint=endpoint)]),axis=0)
    ring = np.append(ring,[[0,90]],axis=0)
 
    codes = 3*np.ones(2*(npts+1),dtype=np.uint8)
    codes[0] = 1
    codes[1] = 2
    codes[npts] = 79
    codes[npts+1] = 1
    codes[npts+2] = 2
    codes[-2]=2
    codes[-1]=79
    rp = matplotlib.path.Path(ring,codes)


    #print(rp)
    rotated_pole = ccrs.RotatedPole( pole_latitude = lat,
                                    pole_longitude = long )
    if trans:
        patch = matplotlib.patches.PathPatch(rp,
                                         facecolor='red', edgecolor='black',
                                         transform=rotated_pole
                                            )
    else:
        patch = matplotlib.patches.PathPatch(rp,
                                         facecolor='red', edgecolor='black'
                                            )
    return patch

Then I try to view my ring:

fig = plt.figure()
ax = plt.axes( projection=ccrs.PlateCarree() )
ax.add_patch(ring(20,10,lat=0,trans=True))
ax.set_global()
# Not sure about the next line I was in a notebook
plt.show()

I normally get garbage if anything at all.

Just to make sure that I have made my path correctly I can make a plot without the projections and transformations:

fig,ax = plt.subplots()
ax.add_patch(ring(20,10,lat=0,trans=False))
ax.plot([-180,180],[0,100])
plt.show()

This obviously is not a ring but it let's me know the issue is not with making a path.

Upvotes: 0

Views: 37

Answers (0)

Related Questions