Choropleth in Geoplot

A choropleth takes data that has been aggregated on some meaningful polygonal level (e.g. census tract, state, country, or continent) and uses color to display it to the reader. It’s a well-known plot type, and it’s perhaps the most general-purpose and well-known of the spatial plot types. A basic choropleth requires polygonal geometries and a hue variable. Change the colormap using matplotlib’s cmap. The legend parameter toggles the legend.

Syntax:

geoplot.choropleth(var)

Example:

Python3




import geoplot as gplt
import geopandas as gpd
import geoplot.crs as gcrs
  
# Reading the world shapefile 
boroughs = gpd.read_file(gplt.datasets.get_path('nyc_boroughs'))
  
gplt.choropleth(boroughs, hue='Shape_Area'
                projection=gcrs.AlbersEqualArea(), 
                cmap='RdPu', legend=True)


Output:

To pass the keyword argument to the legend, use the legend_kwargs argument. To specify a categorical colormap, use a scheme. Use legend_labels and legend_values to customize the labels and values that appear in the legend. Here we are going to use mapclassify which is an open-source python library for Choropleth map classification. To install mapclassify use:

  • mapclassify is available in on conda via the conda-forge channel:

Syntax:

conda install -c conda-forge mapclassify

  • mapclassify is also available on the Python Package Index:

Syntax:

pip install -U mapclassify

Example:

Python3




import geoplot as gplt
import geopandas as gpd
import geoplot.crs as gcrs
import mapclassify as mc
  
# Reading the world shapefile 
contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa'))
  
scheme = mc.FisherJenks(contiguous_usa['population'], k=5)
  
gplt.choropleth(
    contiguous_usa, hue='population', projection=gcrs.AlbersEqualArea(),
    edgecolor='white', linewidth=1,
    cmap='Reds', legend=True, legend_kwargs={'loc': 'lower left'},
    scheme=scheme,   legend_labels=[
        '<3 million', '3-6.7 million', '6.7-12.8 million',
        '12.8-25 million', '25-37 million'
    ]
)


Output:

Working with Geospatial Data in Python

Spatial data, also known as geospatial data, GIS data, or geodata, is a type of numeric data that defines the geographic location of a physical object, such as a building, a street, a town, a city, a country, or other physical objects, using a geographic coordinate system. You may determine not just the position of an object, but also its length, size, area, and shape using spatial data.

To work with geospatial data in python we need the GeoPandas & GeoPlot library

GeoPandas is an open-source project to make working with geospatial data in python easier. GeoPandas extends the data types used by pandas to allow spatial operations on geometric types. Geometric operations are performed shapely. Geopandas further depends on fiona for file access and matplotlib for plotting. GeoPandas depends on its spatial functionality on a large geospatial, open-source stack of libraries (GEOS, GDAL, and PROJ). See the Dependencies section below for more details. 

Required dependencies:

  • numpy
  • pandas (version 0.24 or later)
  • shapely (interface to GEOS)
  • fiona (interface to GDAL)
  • pyproj (interface to PROJ; version 2.2.0 or later)

Further, optional dependencies are:

  • rtree (optional; spatial index to improve performance and required for overlay operations; interface to libspatialindex)
  • psycopg2 (optional; for PostGIS connection)
  • GeoAlchemy2 (optional; for writing to PostGIS)
  • geopy (optional; For plotting, these additional for geocoding)

packages may be used:

  • matplotlib (>= 2.2.0)
  • mapclassify (>= 2.2.0)

Geoplot is a geospatial data visualization library for data scientists and geospatial analysts that want to get things done quickly. Below we’ll cover the basics of Geoplot and explore how it’s applied. Geoplot is for Python 3.6+ versions only.

Note: Please install all the dependencies and modules for the proper functioning of the given codes.

Similar Reads

Installation

Installing can be done through Anaconda:...

Reading Shapefile

First, we will import the geopandas library and then read our shapefile using the variable “world_data”. Geopandas can read almost any vector-based spatial data format including ESRI shapefile, GeoJSON files and more using the command:...

Plotting

...

Selecting Columns

If you want to check which type of data you are using then go to the console and type “type(world_data)” which tells you that it’s not pandas data, it’s a geopandas geodata. Next, we are going to plot those GeoDataFrames using plot() method....

Calculating Area

...

Removing a Continent

If we see the “world_data” GeoDataFrame there are many columns(Geoseries) shown, you can choose specific Geoseries by:...

Visualizing a specific Country

...

Coordinate Reference System

We can calculate the area of each country using geopandas by creating a new column “area” and using the area property....

Using Color Maps (cmap)

...

Adding a legend

We can remove a specific element from the Geoseries. Here we are removing the continent named “Antarctica” from the “Name” Geoseries....

Resizing the legend

...

Polyplot and Pointplot Using Geoplot Library

We can visualize/plot a specific country by selecting it. In the below example, we are selecting “India” from the “NAME” column....

Choropleth in Geoplot

...

KDE Plot in Geoplot

We can check our current Coordinate System using Geopandas CRS i.e Coordinates Reference System. Also, we can change it to a projection coordination system. The Coordinate Reference System (CRS) is represented as a pyproj.CRS object. We can check current CRS using the following syntax....

Sankey in Geoplot

...

Contact Us