pythonfolium

Plot points on offline map in Python


I'm using Spyder and have a couple of coordinates that I want to plot on a world map. Currently I use Folium, save data as an HTML-file and then open it in a webbrowser. My question is: is it possible to plot coordinates on a map without the use of the webbrowser? Or anything external for that matter? I was hoping there would be a package that enables me to plot everything and see it in Spyder itself.

Does anyone has an idea?


Solution

  • check out the cartopy module that provides numerous map projections as well as the ability to enhance your projection with numerous features (eg. land mass, oceans, coastlines, borders, etc)

    It's not likely to be in your Python distribution so install it with

    pip install cartopy

    you'll also need matplotlib.pyplot but since you're plotting already I'll assume you have that.

    It works by selecting a projection (a crs - coordinate reference system) for your map (eg Mercator, Platte Carre (flat plate - basically rectangular), etc.) Then select the crs that matches your data. The basic process is

    Here is a simple of example of plotting 4 lon/lat locations in Africa using a scatter plot of long/lat pairs

    import cartopy
    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    
    
    map_crs = ccrs.PlateCarree()
    data_crs = ccrs.PlateCarree()
    
    
    def fancify(axes):
        """add features to cartopy axes"""
        axes.stock_img()
        axes.add_feature(cartopy.feature.LAND)
        axes.add_feature(cartopy.feature.OCEAN)
        axes.add_feature(cartopy.feature.COASTLINE)
        axes.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
        axes.add_feature(cartopy.feature.LAKES, alpha=0.95)
        axes.add_feature(cartopy.feature.RIVERS)
    
    ax1 = plt.subplot(1, 1, 1, projection=map_crs)  # create map axes
    fancify(ax1)                                    # add some features to map
    ax1.set_extent([0, 60, 0, 60], map_crs)         # min/max long and lat
    
    ax1.scatter([22, 11, 31, 30,], [8, 22, 13, 9])  # longitutes , latitudes
    plt.show()
    

    Some Random Locations In Africa