繁体   English   中英

将元素添加到 matplotlib/cartopy map 事后

[英]Add element to matplotlib/cartopy map a posteriori

我正在使用以下代码获取 map 的空版本。


import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs


cstm=[31.69347, -116.88353, 31.96507, -116.57810]

def map_canvas(cstm, land = True, land_color='darkgreen'):
        fig, mppng = plt.subplots(subplot_kw=dict(projection=ccrs.PlateCarre()))

        mppng.set_extent([min(cstm[1], cstm[3]), max(cstm[1], cstm[3]), min(cstm[0], cstm[2]), max(cstm[0], cstm[2])], crs = ccrs.PlateCarre())

        if land:
            mppng.add_feature(cfeature.GSHHSFeature(scale='i'), facecolor = land_color, edgecolor = 'black', linewidth = .5, zorder = 10)

        return fig, mppng

有什么方法可以向 map 后验(即函数之外)添加元素,例如:

fig, mppng = map_canvas()
mppng.scatter(31.79872, -116.70722)
fig.savefig('Custom1.png')

哪个会在 31.79872N-116.70722E 处添加一个点到现有的空 map? 到目前为止,它正在生成空的 map 好吧,但是一旦生成,我就无法向其中添加任何内容。

谢谢。

您需要使用 (longitude, latitude) 序列。 因此,相关的代码行必须是:-

mppng.scatter(-116.70722, 31.79872, s=120, color="red")

s=120, color="red" 是选项。

输出

我还补充说:-

mppng.gridlines(crs=ccrs.PlateCarree(), draw_labels=True)
mppng.set_title("a_posteriori_title")

得到这个 plot。

读者可以尝试运行的基于OP问题的完整代码:

import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs

# This will be used several times
my_proj = ccrs.PlateCarree()
# lat_min, Lon_min, Lat_max, Lon_max
cstm=[31.69347, -116.88353, 31.96507, -116.57810]

def map_canvas(cstm, land=True, land_color='darkgreen'):
        fig, mppng = plt.subplots(subplot_kw=dict(projection=my_proj))
        mppng.scatter(31.77, -116.75, s=120, color="red", transform=my_proj)

        mppng.set_extent(
            [min(cstm[1], cstm[3]), max(cstm[1], cstm[3]), min(cstm[0], cstm[2]), max(cstm[0], cstm[2])], 
            crs=my_proj)

        if land:
            # may cause data download from the web
            mppng.add_feature(cfeature.GSHHSFeature(scale='i'), 
                              facecolor=land_color, edgecolor='black', 
                              linewidth=.5, zorder=10)
        return fig, mppng

# This generates figure and axes objects
fig, mppng = map_canvas(cstm)

# Try to add more fetures a_posteriori
# mppng.scatter(31.79872, -116.70722) # This has wrong order of coordinates
mppng.scatter(-116.70722, 31.79872, s=120, color="red")

# Add more features on the axes
mppng.gridlines(crs=ccrs.PlateCarree(), draw_labels=True)
mppng.set_title("a_posteriori_title")
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM