在地图上绘制一个矩形框

时间:2021-07-07 23:53:09

标签: python matplotlib matplotlib-basemap

我试图在我的美国地图上绘制三个矩形框,只是为了简单地显示感兴趣的区域。我已经创建了一个空的美国地图,然后实现了 matplotlibs 补丁集合来创建所需的三个框。当我使用来自 Basemap 的示例代码/地图时,我能够让这些框显示出来(我首先在那里想出了如何绘制框)。虽然,我不喜欢它使用示例代码生成的地图,我想尝试将这些框添加到我自己的地图上。当我这样做时,我只得到一张带有州界线而没有矩形框的美国地图,此时,我不确定是什么导致了我的问题。第一个代码和图显示了示例代码给我的内容,第二个代码和图来自我想要使用/创建的地图。感谢您的任何帮助!

from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax=fig.add_axes([0.1,0.1,0.8,0.8])
m = Basemap(projection='cyl',llcrnrlat=14,urcrnrlat=55, 
            llcrnrlon=-130,urcrnrlon=-60,resolution='c')
m.drawcoastlines()

patches_m = []
patches_s = []
patches_t = []
midwest = np.array([[-103,44],[-89.7,44],[-89.7,37],[-103,37]])
seus = np.array([[-76,30],[-94,30],[-94,37],[-76,37]])
txok = np.array([[-103,30],[-94,30],[-94,37],[-103,37]])

patches_m.append(Polygon(midwest))
patches_s.append(Polygon(seus))
patches_t.append(Polygon(txok))


ax.add_collection(PatchCollection(patches_m, facecolor='lightgreen', edgecolor='k', linewidths=0.5))
ax.add_collection(PatchCollection(patches_s, facecolor='lightpink', edgecolor='k', linewidths=0.5))
ax.add_collection(PatchCollection(patches_t, facecolor='lightblue', edgecolor='k', linewidths=0.5))

enter image description here

levels = np.arange(-.04,.04,.001).tolist()
cmap=cmocean.cm.balance
fig, axs = plot.subplots(ncols=1, nrows=1, axwidth = 10,     projection=crs.Mercator(), tight = False)
ax1 = axs
axs.format(suptitle=(''), titlesize = 20, coast = True, innerborders = True)

patches_m = []
patches_s = []
patches_t = []
midwest = np.array([[-103,44],[-89.7,44],[-89.7,37],[-103,37]])
seus = np.array([[-76,30],[-94,30],[-94,37],[-76,37]])
txok = np.array([[-103,30],[-94,30],[-94,37],[-103,37]])

patches_m.append(Polygon(midwest))
patches_s.append(Polygon(seus))
patches_t.append(Polygon(txok))

ax1.add_collection(PatchCollection(patches_m, facecolor='lightgreen', edgecolor='k', linewidths=0.5))
ax1.add_collection(PatchCollection(patches_s, facecolor='lightpink', edgecolor='k', linewidths=0.5))
ax1.add_collection(PatchCollection(patches_t, facecolor='lightblue', edgecolor='k', linewidths=0.5))

axs.set_extent(
[
    310, # minimum longitude
    220, # min longitude
    10, # max latitude
    56 # max latitude
],
crs=crs.PlateCarree()

) enter image description here

0 个答案:

没有答案