Geographic Data Visualization with Python

Dawei
4 min readFeb 23, 2019

--

I think Python is not a good tool for geographic data visualization. After all, there are many software like ArcGIS or QGIS that are more suitable to do this job. Nevertheless, if you are a meteorologist or who dealing with the huge NetCDF data, or you need to draw a lot of maps just of one pattern, python can be really powerful to do that. You can take advantage of the python’s great data processing power, to create a workflow that process data first and then do the visualization. In this article, I want to introduce how to use bastmap - a matplotlib toolkits - to do the geographic data visualization.

Installation

plot a heat map

Let’s say you have some precipitation data around Japan to plot. (Commonly it will be a NetCDF data, but this is not a tutorial for manipulating NetCDF data, so I transformed it to csv. ) At first, read precipitation data.

Now we have a DataFrame with columns of lat, lon, and precipitation data r1h.

To make life easier, let’s give these variables alias.

Then we can create the heat map.

It will give you some warming, just ignore it. Now we have 2 maps. The left is a heat map, the right is a contour map. Note that our date is not mesh data, so we need tri=True to draw it on an unstructured triangular grid.

##Add some background Let’s add some background for the heat map. There are many kinds of background inside the package that you can use it directly. The code to generate these backgrounds is below. The method name is self-explained. For more information, you can refer this page.

Normal background

If you zoom in to some small area, the background get blurry, because the default resolution is set to “c” which means “crude”. You can change the resolution when initialing the Basemap. Here is a demo for the different resolution of filled continent in a coast. (Be careful, it took about 1 minute to draw the "full resolution" image. )

Image background

You can also add the satellite image as background. You can use the following method.

The arcgisimag is the recommended way to draw an image background. Because it can zoom in with a good quality image. The following image shows the difference between arcgisimag and others.

arcgisimag is zoom in according to the scale because this method post a request to the ArcGIS server to get the tile image of the current frame, so the image quality can changes as the scale changed.
arcgisimag also have other type of services you can choose. You can get more information from there. I create the demo of several services below.

##plot shp file Finally, let's plot shp file. You can download the demo shp file from there.

After you load the shp file. You can access the polygon data by calling m.objectName. And the property of every polygon can be access by calling m.objectName_info.

It prints:

In this example we can see that m.city_boundary is a list of list of tuple. And m.city_boundary_info is a list of dictionaries that contain the attribute of each polygon. You can use this information to draw any polygon you want.

Summary

In this post, I showed some basic method to plot geographic data. As you can see, we must do every operation by write python code. If you don’t know which piece of code you should use, you need to search for Google or skim the library document. The learning curve is huge, but once you are familiar with it, you can plot geographic data very efficiently.

Useful resources:

  1. Basemap tutorial
  2. matplotlib basemap toolkit official document

Originally published at gist.github.com.

--

--