在图像上绘制多个点

Plot multiple points on image

我在 GeoDataFrame 中保存了各种点 gdf。 我想在 matplotlib 通过 stock_img() 附带的(世界的)图像上绘制这些点。

我以为这很简单,但我只在图像上绘制了一个点,而不是所有点(应该有105个)。数据可以在这里下载https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Introduction+to+Geospatial+Data+in+Python/florence.csv

可重现的例子:

import geopandas as gp
import pandas as pd
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

data = {'Lat':{0:12.9,2:12.9,4:13.2,6:13.6,8:13.7,10:13.8,12:14.2,14:14.5,16:14.8,18:16.0,20:17.0,22:17.9,24:18.3,26:18.9,28:19.7,30:20.7,32:22.0,34:23.4,36:24.6,38:25.1,40:25.0,42:24.8,44:24.5,46:24.6,48:24.4,50:24.6,52:25.0,54:25.9,56:26.4,58:27.1,60:28.0,62:28.5,64:29.4,66:30.4,68:31.5,70:32.5,72:33.1,74:33.6,76:33.9,78:34.1,80:34.1,82:34.0,84:33.9,86:33.7,88:33.6,90:33.6,92:33.6,94:33.7,96:34.0,98:35.5,100:38.5,102:39.8,104:42.6},'Long':{0:-18.4,2:-19.4,4:-20.9,6:-21.4,8:-22.7,10:-24.7,12:-25.5,14:-26.7,16:-27.8,18:-30.2,20:-33.2,22:-35.9,24:-38.7,26:-41.0,28:-42.5,30:-43.9,32:-45.7,34:-47.2,36:-48.6,38:-49.8,40:-51.8,42:-53.2,44:-54.3,46:-55.2,48:-56.3,50:-57.7,52:-60.0,54:-62.4,56:-64.6,58:-66.2,60:-67.9,62:-69.5,64:-70.7,66:-71.8,68:-73.2,70:-74.3,72:-75.1,74:-76.0,76:-76.4,78:-77.2,80:-77.9,82:-78.4,84:-78.8,86:-79.3,88:-79.5,90:-79.8,92:-80.1,94:-80.8,96:-81.8,98:-82.1,100:-82.9,102:-80.2,104:-71.9}}


df = pd.DataFrame.from_dict(data)

# Invert longitude for plotting in western hemisphere
df['Long'] = 0 - df['Long']

# convert to GeoDataframe and create Coordinates from lng/lat column
gdf = gp.GeoDataFrame(
    df, 
    geometry=gp.points_from_xy(
        df.Long, 
        df.Lat
    )
)

fig, ax = plt.subplots(figsize = (20, 16)) 
ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img()
gdf.plot(ax = ax, markersize = 5, color = 'r', zorder = 10)

这是print(gdf)

    AdvisoryNumber              Date   Lat  Long  Wind  Pres  \
0                1  08/30/2018 11:00  12.9 -18.4    30  1007   
1               1A  08/30/2018 14:00  12.9 -19.0    30  1007   
2                2  08/30/2018 17:00  12.9 -19.4    30  1007   
3               2A  08/30/2018 20:00  13.1 -20.4    30  1007   
4                3  08/30/2018 23:00  13.2 -20.9    35  1007   
..             ...               ...   ...   ...   ...   ...   
100             73  09/17/2018 11:00  38.5 -82.9    25  1008   
101             74  09/17/2018 17:00  39.0 -81.3    25  1008   
102             75  09/17/2018 23:00  39.8 -80.2    25  1008   
103             76  09/18/2018 05:00  41.3 -75.9    25  1006   
104             77  09/18/2018 11:00  42.6 -71.9    25  1006   

                   Movement                        Type      Name  \
0     W at 12 MPH (280 deg)  Potential Tropical Cyclone       Six   
1     W at 12 MPH (280 deg)  Potential Tropical Cyclone       Six   
2      W at 9 MPH (280 deg)  Potential Tropical Cyclone       Six   
3     W at 11 MPH (280 deg)  Potential Tropical Cyclone       Six   
4     W at 13 MPH (280 deg)  Potential Tropical Cyclone       Six   
..                      ...                         ...       ...   
100   NE at 15 MPH (40 deg)         Tropical Depression  FLORENCE   
101  ENE at 14 MPH (60 deg)       Post-Tropical Cyclone  Florence   
102    NE at 9 MPH (35 deg)       Post-Tropical Cyclone  Florence   
103   E at 13 MPH (100 deg)       Post-Tropical Cyclone  Florence   
104  ENE at 25 MPH (70 deg)       Post-Tropical Cyclone  Florence   

             Received Forecaster                    geometry  
0    08/30/2018 10:45      Avila  POINT (-18.40000 12.90000)  
1    08/30/2018 13:36      Avila  POINT (-19.00000 12.90000)  
2    08/30/2018 16:36      Avila  POINT (-19.40000 12.90000)  
3    08/30/2018 19:44      Beven  POINT (-20.40000 13.10000)  
4    08/30/2018 22:42      Beven  POINT (-20.90000 13.20000)  
..                ...        ...                         ...  
100  09/17/2018 10:59        NaN  POINT (-82.90000 38.50000)  
101  09/17/2018 16:48       Roth  POINT (-81.30000 39.00000)  
102  09/17/2018 22:48       Roth  POINT (-80.20000 39.80000)  
103  09/18/2018 04:53     Hurley  POINT (-75.90000 41.30000)  
104  09/18/2018 11:14     Carbin  POINT (-71.90000 42.60000)  

这对我有用。

import cartopy
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd

# create df
df = pd.read_csv('florence.csv')
# create gdf and define CRS as WGS 84 so Cartopy knows where to plot points
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Long, df.Lat), crs=4326)

# need negative longitude for Western Hemi
gdf['Long'] = gdf['Long'] * -1

proj = ccrs.PlateCarree(central_longitude=0)

fig, ax = plt.subplots(subplot_kw=dict(projection=proj), figsize=(16,16))
# this is the key part to get your extent correct so all dots are plotted
ax.set_extent([gdf['Long'].min() - 1,
               gdf['Long'].max() + 1,
               gdf['Lat'].min() - 1,
               gdf['Lat'].max() + 1], 
              crs=ccrs.PlateCarree())

fig.canvas.draw()
fig.tight_layout()

ax.scatter(gdf['Long'], gdf['Lat'], color='red')
ax.stock_img()

plt.show()