geoplot

repository·master·Indexed 22 days ago

https://github.com/residentmario/geoplot

A high-level Python library for geospatial data visualization that extends cartopy and matplotlib to provide a seaborn-like experience. It supports various plot types including pointplot, polyplot, choropleth, kdeplot, cartogram, sankey, quadtree, and voronoi. The library includes the geoplot.crs module for a wide range of coordinate reference systems and geoplot.datasets.get_path for accessing built-in datasets.

Tokens
7.5K
Snippets
24
Records
33
Agent score
78%

What's inside geoplot

  1. Overview of geoplot features

    master

    geoplot is a high-level Python geospatial plotting library designed as an extension to cartopy and matplotlib. It aims to provide a seaborn-like experience for geospatial data.

    Key features include:

    • High-level plotting API: Provides easy access to standard cartographic map types for most common use cases.
    • Native projection support: Simplifies the process of choosing and applying appropriate map projections for different types of geospatial depictions.
    • Compatibility with matplotlib: Leverages the matplotlib ecosystem, making it easy to integrate with other Python visualization tools.
  2. Verify Coordinate Order (Longitude-Latitude vs Latitude-Longitude)

    master
    Geospatial libraries like shapely (used by geopandas) use the "modern" (x, y) or (longitude, latitude) order. However, many datasets use the "historical" (y, x) or (latitude, longitude) order. geopandas does not automatically detect this. After setting your CRS, always verify that your coordinates are in the correct order by inspecting the data to ensure they haven't been swapped.
  3. Understand GeoDataFrames and Coordinate Reference Systems (CRS)

    master
    A GeoDataFrame is an extension of a pandas DataFrame that includes a geometry column. When working with geospatial data, it is critical to verify the Coordinate Reference System (CRS), which defines how spatial coordinates are mapped to locations on Earth. You can check the CRS using the .crs attribute. Common identifiers include EPSG numbers (e.g., epsg:4326 for WGS84 latitude/longitude). If a dataset has an incorrect CRS, you can manually set it or convert it using .to_crs().
  4. Convert CSV/JSON data to a GeoDataFrame

    master

    If you have a standard pandas DataFrame containing latitude and longitude columns (e.g., from a CSV), you can convert it to a GeoDataFrame by using shapely.geometry.Point and the pandas.apply method.

    import pandas as pd
    import geopandas as gpd
    from shapely.geometry import Point
    
    # Load standard DataFrame
    nyc_collisions_sample = pd.read_csv(gplt.datasets.get_path('nyc_collisions_sample'))
    
    # Create geometry column from lat/long
    collision_points = nyc_collisions_sample.apply(
        lambda srs: Point(float(srs['LONGITUDE']), float(srs['LATITUDE'])),
        axis='columns'
    )
    
    # Initialize GeoDataFrame
    nyc_collisions_sample_geocoded = gpd.GeoDataFrame(nyc_collisions_sample, geometry=collision_points)
  5. Stack plots with different data on a single projected axis

    master

    You can stack multiple plots (e.g., a polyplot of regions and a pointplot of cities) on top of each other by passing the axis object (ax) of the first plot to the subsequent plotting calls.

    By default, geoplot sets the plot extent to the total_bounds of the last stacked plot. If you want to constrain the view to a specific area (like the contiguous US instead of the whole country), use the extent parameter with the total_bounds of your base geometry.

    # Create the base projected map
    ax = gplt.polyplot(
        contiguous_usa, 
        projection=gcrs.AlbersEqualArea()
    )
    
    # Stack a pointplot on the same axis, constraining the extent
    gplt.pointplot(cities, ax=ax, extent=contiguous_usa.total_bounds)
  6. Apply a map projection to a plot

    master

    By default, geoplot produces unprojected plots that treat coordinates as if they were on a flat Cartesian plane. To use a map projection, pass a geoplot.crs object to the projection parameter of a plotting function (e.g., polyplot, pointplot).

    geoplot relies on cartopy for its projections. You can find a full list of available projections in the Cartopy documentation.

    import geoplot.crs as gcrs
    import geoplot as gplt
    
    # Apply Albers Equal Area projection to a polyplot
    gplt.polyplot(contiguous_usa, projection=gcrs.AlbersEqualArea())
  7. Join Non-Spatial Data to Geometries

    master

    To map data that lacks geometry (e.g., statistics by state), you must join that data against a GeoDataFrame that contains the corresponding shapes. This is typically done by setting a common index (like a state name) on both objects and using the .join() method.

    import geoplot.crs as gcrs
    
    # obesity is a standard DataFrame, contiguous_usa is a GeoDataFrame
    result = contiguous_usa.set_index('state').join(obesity.set_index('State'))
    
    # Plot the joined result
    gplt.cartogram(result, scale='Percent', projection=gcrs.AlbersEqualArea())