簡體   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