Python – Making Maps with Cartopy

Cartopy is a Python package designed for geospatial data processing in order to produce maps and other geospatial data analyses. Making a map with Python and Cartopy is actually easier than you might think, but still it has its own tricks. Once you get the idea how it is done, hopefully it becomes a bit easier (I am still waiting for this moment myself!). As I was really fascinated with what the library can do, I have decided to write it down. Hopefully, it will help you as well.

These are the topics in the article:

  • Higlight a single country
  • What data cartopy has per country?
  • How can we make a heat map of the “richest countries”?
  • Countries with multiple territories
  • How to put a label over a country?
  • Where are the rivers and the lakes?
  • Coloring a country with two colors, dividing it into North and South
  • Coloring a small country / city
  • GitHub link to the Jupyter Notebook with all examples

Higlight a single country

Pretty much, every country has its shape, written on the Cartopy library and could be referred by name. For example, if you want to show Bulgaria in green on the map, this is ok:

What data cartopy has per country? How can we make a heat map of the “richest countries”?

How does it do it? If you add print(country)  somewhere in the code, you would see this information, per country (of course, not formatted in such a nice way):

As you see, there are the names of the country in 20+ languages, information for the subregion, economy, formal name, income gdp, wikipedia data aid (Q219) and other stuff. Thus, making a heat map of “rich countries” should be easy – there are 5 types of countries in the library, based on how much their GRP is:

Thus a loop with 5 condition results pretty much in a map, split by the GRP:

Not going to speak about the code, it is rather self-explanatory (or take a look at the github link of the Jupyter notebook at the end and run it).

Countries with multiple territories

The story is a bit different, if you put one of these countries – ["Denmark", "France", "Israel", "United Kingdom", "United States of America"], as these have some territories, which are under other jurisdiction. Thus, highlighting the “main country” in blue and the territory under other jurisdiction in green would result in this:

The code to achieve the map above is a bit different, and in it we differentiate between country.attributes['NAME'] and country.attributes['SOVEREIGNT']:

How to put a label over a country?

Writing over a country was something that took me quite some time to achieve. At the end, I decided it would be easier, if I asked in StackOverflow and I got quite a good answer – the trick in writing over a country is to retrieve the centroid of the country.geometry and to plot the text at that location. If a good map size and font is used, you may write the name of every country in the library. The colors are a bit repeated, because of the code I have created only allows about 10 different colors. Anyway, the borders are visible.

Where are the rivers and the lakes?

Probably you have seen the  ax.add_feature(cartopy.feature.LAKES, alpha=0.95) , ax.add_feature(cartopy.feature.RIVERS) on the top of the first code and you are wondering, that you could not find any river or a lake on the maps so far. Well, true. As far as you are coloring the whole country, somehow the lakes and the rivers are covered as well. Anyway, it is always a possibility to show them, running this:

Coloring a country with two colors, dividing it into North and South

If you want to split a country in two, the centroid could be helpful again. The code below divides Brazil into south and north, based on its centre. Somehow useful, if you need it for some reasons:

Coloring a small country / city

There are some small countries, which are not visible initially on the “admin_0_countries” map. That’s why, there are other maps, in which these are visible. Make sure to adjust the resolution correctly and you would see green points for Singapore, Monaco, Malta and Lichtenstein. Scotland is also visible on the map, and as far as it does not fall in the condition country.geometry.area < 2, it does not have a green point, but is colored completely:

The whole code is available in GitHub as a JupyterNotebook. I hope it works:

Tagged with: , , , , , ,