Polyplot and Pointplot Using Geoplot Library

First, we will import Geoplot library. Next, we will load one of the sample datasets(geojson file) present in geoplot. In the below example, we are going to use “world” ,”contiguous_usa”,”usa_cities”,”melbourne” and “melbourne_schools” datasets. List of datasets present in geoplot are mentioned below:

  • usa_cities
  • contiguous_usa
  • nyc_collision_factors
  • nyc_boroughs
  • ny_census
  • obesity_by_state
  • la_flights
  • dc_roads
  • nyc_map_pluto_sample
  • nyc_collisions_sample
  • boston_zip_codes
  • boston_airbnb_listings
  • napoleon_troop_movements
  • nyc_fatal_collisions
  • nyc_injurious_collisions
  • nyc_police_precincts
  • nyc_parking_tickets
  • world
  • melbourne
  • melbourne_schools
  • san_francisco
  • san_francisco_street_trees_sample
  • california_congressional_districts

We can add our own datasets by editing the datasets.py file. Click here for some free sample datasets. 

  • If you have polygonal data, you can plot that using a geoplot polyplot.
  • If your data consists of a bunch of points instead, you can display those points using pointplot.

Syntax :

geoplot.datasets.get_path(str)

Syntax for plotting:

geoplot.polyplot(var)
geoplot.pointplot(var)

Example:

Python3




import geoplot as gplt
import geopandas as gpd
  
# Reading the world shapefile
path = gplt.datasets.get_path("world")
world = gpd.read_file(path)
gplt.polyplot(world)
  
path = gplt.datasets.get_path("contiguous_usa")
contiguous_usa = gpd.read_file(path)
gplt.polyplot(contiguous_usa)
  
path = gplt.datasets.get_path("usa_cities")
usa_cities = gpd.read_file(path)
gplt.pointplot(usa_cities)
  
path = gplt.datasets.get_path("melbourne")
melbourne = gpd.read_file(path)
gplt.polyplot(melbourne)
  
path = gplt.datasets.get_path("melbourne_schools")
melbourne_schools = gpd.read_file(path)
gplt.pointplot(melbourne_schools)


World Dataset:

USA Dataset:

USA Cities Dataset:

Melbourne Dataset:

Melbourne Schools Dataset:

We can combine these two plots using overplotting. Overplotting is the act of stacking several different plots on top of one another, useful for providing additional context for our plots:

Example:

Python3




import geoplot as gplt
import geopandas as gpd
  
# Reading the world shapefile 
path = gplt.datasets.get_path("usa_cities")
usa_cities = gpd.read_file(path)
  
path = gplt.datasets.get_path("contiguous_usa")
contiguous_usa = gpd.read_file(path)
  
path = gplt.datasets.get_path("melbourne")
melbourne = gpd.read_file(path)
  
path = gplt.datasets.get_path("melbourne_schools")
melbourne_schools = gpd.read_file(path)
  
ax = gplt.polyplot(contiguous_usa)
gplt.pointplot(usa_cities, ax=ax)
  
ax = gplt.polyplot(melbourne)
gplt.pointplot(melbourne_schools, ax=ax)


Output:

You may have noticed that this map of the United States appears to be odd. Because the Earth is a sphere, it is difficult to depict it in two dimensions. As a result, we use some type of projection, or means of flattening the sphere, whenever we take data off the sphere and place it on a map. When you plot data without a projection, or “carte blanche,” your map will be distorted. We can “correct” the distortions by picking up a projection method. Here we are going to use Albers equal-area and WebMercator projection.

Along with this, we are also going to add some other parameters such as hue, legend, cmap, and scheme.

  • The hue parameter applies a colormap to a data column.
  • The legend parameter toggles a legend.
  • Change the colormap using matplotlib’s cmap.
  • For a categorical colormap, use a scheme.

Example:

Python3




import geoplot as gplt
import geopandas as gpd
import geoplot.crs as gcrs
  
# Reading the world shapefile 
path = gplt.datasets.get_path("contiguous_usa")
contiguous_usa = gpd.read_file(path)
  
path = gplt.datasets.get_path("usa_cities")
usa_cities = gpd.read_file(path)
  
ax = gplt.polyplot(contiguous_usa, projection=gcrs.AlbersEqualArea())
gplt.pointplot(usa_cities, ax=ax, hue="ELEV_IN_FT",cmap='rainbow'
               legend=True)
  
ax = gplt.webmap(contiguous_usa, projection=gcrs.WebMercator())
gplt.pointplot(usa_cities, ax=ax, hue='ELEV_IN_FT', cmap='terrain'
               legend=True)


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