Introduction to Map Plotting with GeoPandas
Explore how to visualize spatial data using GeoPandas by leveraging Matplotlib as the core plotting engine. Understand the interplay between GeoPandas and supporting packages for creating detailed static maps and begin handling basic point and polygon plotting with customizable styling options.
We'll cover the following...
Introduction
Plotting with GeoPandas is based on the Matplotlib library. Matplotlib provides a wide variety of visualization options, and by using it as a foundation, GeoPandas can create intricate maps with relative ease. The relationship between GeoPandas and Matplotlib is symbiotic: GeoPandas handles the geospatial data manipulation (i.e., coordinates, projections, etc.) and Matplotlib handles the actual plotting.
Although Matplotlib provides the foundation for plot rendering, other packages are used in the process. For example, the conversion from Shapely geometries, which are actually stored in a GeoDataFrame, to Matplotlib paths (disconnected segments) and patches (closed 2-D shapes with defined facecolor) is provided by the Descartes package. Moreover, mapclassify is used for the optimal classification of numerical values to create choropleth maps.
These packages are fundamental and they are accessed directly by GeoPandas, being transparent to the user. Besides those, there are other packages that can be used for interactive plotting (e.g., Folium), to overlay raster imagery (e.g., Contextily), or to serve as a higher-level plotting API such as Geoplot. The following figure brings two examples of plots created with Geoplot and Contextily respectively. The Geoplot library was used in the left figure to plot the countries on an Ortographic projection, while Contextily was used in the right figure to plot to include a raster of rain anomaly over a specific basin.
Understanding GeoPandas plotting will not only introduce us to the specifics of the GeoPandas API, but will also broaden our overall understanding of the Python visualization ecosystem.
We'll focus on basic plotting of points and polygons.
Plotting basics
The basic plotting command is the plot() method, which is available for GeoSeries or GeoDataFrames. In both cases, GeoPandas will adjust the axes units according to the defined CRS and its limits to cover the whole region to be displayed. As default, it will display all the geometries present within the GeoSeries/GeoDataFrame.
As already mentioned, GeoPandas uses Matplotlib as the main engine for drawing the maps, so it needs a canvas (i.e., pyplot.Axes instance). This can be passed through the ax argument or created automatically. In both cases, the axes with the drawing is returned by the plot() method.
In the next example, in line 7, the axes are created automatically by GeoPandas and returned from the plot() method. Note that we are free to style the plot by using standard Matplotlib arguments, such as facecolor and edgecolor, as used in line 7, and also methods like set_ylabel and set_facecolor, as used in lines 8 to 10.
Multiple plots
It's worth noting that the GeoPandas’ plot() method can draw different types of graphs through the kind argument. However, the default, as seen in the previous example, is 'geo', which is used for mapping. Besides, it can accept all the different plot kinds available in Matplotlib, such as scatter, pie, bar, and so on. Additionally, multiple subplots are also supported.
To demonstrate this, in the next example, we are going to focus on the GDP of world countries. In lines 7 and 10 we open the countries dataset and create a column to represent GDP per capita for each country, respectively. Then we create a Matplotlib figure with two axes (top and bottom rows) in line 13, and we plot the countries in the first row axes with the color scale representing the GDP per capita (line 14).
Finally, in the second row, we draw a scatterplot with the countries’ GDP on the Y-axis and the total population on the X-axis (line 15). Additionally, a colorbar with the GDP per capita has also been added with logarithmic color scale for improved visualization (lines 18 and 19).