Get Started

GeoJSON and plotting with geopandas

1. Plotting with GeoJSON

GeoJSON is a newer format for geospatial data released by the Internet Enginering Task Force in 2016. Unlike shapefiles, GeoJSON is a single file, making it easier to work with.

2. Neighborhoods GeoJSON

The raw GeoJSON file is easier to interpret than a shape file. Here are the first few lines of the raw GeoJSON file that you will use to plot Nashville neighborhoods in this chapter. Notice that the geometry is a MultiPolygon. GeoJSON supports multipart geometries, including MultiPoint, MultiLineString, and MultiPolygon. Multipart geometries is a topic beyond the scope of this course. Reading in the geojson file with geopandas, you can see that the properties of the geojson file (name and geometry) become the columns of the GeoDataFrame.

3. Geopandas dependencies

Vector data is familiar by now. It's the type of geospatial data made up of points, lines, and polygons. Raster data, on the other hand, can be thought of as a grid. Topographical maps are an example of raster graphics. Geopandas makes it easy to work with geospatial data in python. Two supporting libraries that help with this are Fiona and GDAL. Fiona provides a bridge from the Open GIS Simple Features Reference (called OGR for short) to python and geopandas. We can leverage the work that OGR and GDAL - the Geospatial Data Abstraction Library - do to translate raster and vector geospatial data formats.

4. Comparing raster and vector graphics

On the left is a raster image of Corfu, an island off the coast of Greece. It is composed of numerous tiny grid tiles that give the image a 3-dimensional texture. The image on the right is a vector image of Corfu, made up of points, lines, and polygons. In this course, you will be working with vector graphics.

5. Colormaps

Now that you know more about how geopandas renders vector data, let's turn to choosing colors for your geopandas plots. Choosing the right color when plottting your map requires a bit of thought. Matplotlib provides around 100 different colormaps. When you map regions without any kind of quantitative relationship, a qualitative color map is the correct choice. Here are matplotlib's built-in qualitative colormaps.

6. Plotting with color

Non-spatial features in the data can add meaning to polygon plots. Here are the first 3 rows of a GeoDataFrame for the Nashville School Districts. Recall that you can color the polygons and create a legend to give information by specifying the column and legend arguments to the plot() function. The plot on the left uses ‘last_name’ to identify each district by the school board member who represents it. The plot on the right uses 'district' to identify each region by its number. Choose a column that suits the purpose of your map. The plot on the left uses the default colormap, while the one on the right uses the Set2 colormap for a softer palette. Both are qualitative colormaps. If you specify a colormap without specifying a column, pyplot still renders each region in a different color, but does not use any value in the data to do so.

7. The `legend_kwds` argument to `.plot()`

You can also style plot legends by passing a dictionary of keywords to geopandas .plot(). Here you see the difference in plotting the council_districts with and without the legend_keywords argument. In this case, bbox_to_anchor moves the legend off of the plot, and ncol creates a multicolumn legend so that it doesn't extend below the plot.

8. Let's practice!

You've been introduced to a new file type for geospatial data - GeoJSON, learned a bit about geopandas under the hood, and seen how to select the right colormap for differentiating regions by a qualitative property. Now it's time to practice.