Drawing boxes on basemap with real Lat-Lon

2.1k views Asked by At

I'm facing an issue when trying to plot a box of my study area on basemap graphic. I've tried with two different options: 1. Using Polygon. 2. Using drawgreatcircle.

This is my code with Polygon:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

m = Basemap(width=6000000,height=5000000,rsphere=(6378137.00,6356752.3142),\
                resolution='l',projection='lcc',\
                lat_1=0.,lat_2=30,lat_0=15,lon_0=-75.)

lw = 1
dashes = [5,7]
graticules_color = 'black'

fig1 = plt.figure(figsize=(16,20))
fig1.patch.set_facecolor('#e6e8ec')
ax = fig1.add_axes([0.1,0.1,0.8,0.8])

m.etopo()

m.drawparallels(np.arange(-90.,90.,10.),labels=[1,0,0,0],fontsize=20,linewidth=lw, dashes=dashes, color=graticules_color)
m.drawmeridians(np.arange(-180.,180.,10.),labels=[0,0,0,1],fontsize=20,linewidth=lw, dashes=dashes, color=graticules_color)
m.drawmapboundary(fill_color='white')
m.drawcountries()

x1,y1 = m(-100,0)
x2,y2 = m(-100,30)
x3,y3 = m(-50,30)
x4,y4 = m(-50,0)

poly = Polygon([(x1,y1),(x2,y2),(x3,y3),(x4,y4)],facecolor='none',edgecolor='k',linewidth=1.5)
plt.gca().add_patch(poly)

This is the result: Box using Polygon toolbox enter image description here Basically, the box is not in the projection coordinates I'm using on the plot (or at least the lines are not following the parallels and meridians as the grid does).

Looking at the second option (with drawgreatcircle function), it works as I need for the right, left and lower lines, but not for the upper one. The additional code is:

lo1 = -100
lo2 = -50
la1 = 0
la2 = 30
#l1
m.drawgreatcircle(lo1, la1, lo1, la2,linewidth=2,color='r')
#l2
m.drawgreatcircle(lo1, la2, lo2, la2,linewidth=2,color='r')
#l3
m.drawgreatcircle(lo2, la2, lo2, la1,linewidth=2,color='r')
#l4
m.drawgreatcircle(lo2, la1, lo1, la1,linewidth=2,color='r') 

And the figure: Box with drawgreatcircle enter image description here Can anybody please help me to solve this question?

Thanks a lot!

Angela.

1

There are 1 answers

1
swatchai On

Add this snippet code to yours. It will plot the line you want.

lons = np.linspace(-100, -50, 15)
lats = np.ones(len(lons))*30
m.plot(lons, lats, color="r", linewidth=1.5, latlon=True)