0

I am trying to display a GeoDataFrame containing points, using cartopy. I first tried to follow the indications given in the documentation of geopandas. However, the points are not displayed:

Resulting map using the geopandas documentation

Interestingly, if I use the Pandas internal plotting method, the points are indeed displayed, but the map extent is modified:

enter image description here

Here is the code:

import geopandas as gpd
import cartopy as ctpy
import matplotlib.pyplot as plt

map_data = '../data/stations.gpkg'


def plot_gpd(stations):
    # Reproj
    crs = ctpy.crs.Mollweide()
    crs_proj4 = crs.proj4_init
    geom_reproj = stations.geometry.to_crs(crs_proj4)
    # Plot
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=crs)
    ax.add_feature(ctpy.feature.COASTLINE)
    geom_reproj.plot(ax=ax, markersize=3)
    plt.savefig('plot_gpd.png')


def plot_ctpy(stations):
    # Reproj
    crs = ctpy.crs.Mollweide()
    crs_proj4 = crs.proj4_init
    geom_reproj = stations.geometry.to_crs(crs_proj4)
    # Plot
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=crs)
    ax.add_feature(ctpy.feature.COASTLINE)
    ax.add_geometries(geom_reproj, crs=crs)
    plt.savefig('plot_ctpy.png')


stations = gpd.read_file(map_data)
plot_gpd(stations)
plot_ctpy(stations)

Is there something I am doing wrong? Why is ax.add_geometries not working?

This related question is unfortunately not answering my problem: Add geopandas to cartopy

  • 1
    Cartopy basically only plots polygons, although this is not really clear from the functions. So plotting it with geopandas is fine, or plotting it with matplotlib itself is also an option: stackoverflow.com/questions/25340427/… – joris Jun 7 '18 at 13:22
1

The answer that Joris linked to proposes to display the geometries with ax.scatter(), which basically does the same than gdf.plot(), just more verbose. The solution is to use the .plot() method from geopandas. To fix the extent issue, one can force a global extent by using ax.set_global().

World plot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.