A guide to creating filled maps (or chloropleths) with Python
First, big thanks to Stephan Hügel for his excellent guide to mapping with Python. To create this guide, I heavily borrowed from Stephan's work, but tried to provide more contextualizing comment to ensure that a less advanced user can create their own filled map.
For this guide, I focus on creating a filled map of Sri Lanka. I use some basic demographic data plus a shapefile that includes the first level of administration (for example, in the United States this is a state) to demonstrate how to create and manipulate a filled map with Python.
The shapefile I obtained is from the Spatial Data Repository where you can find detailed shapefiles for most countries.
How this works
I highly recommend cloning this repository and using SL Map.ipynb to step through the guide as an iPython Notebook. If you're not familiar with iPython notebook or don't have it, one of the easiest ways to get started is to download the Anaconda distribution of Python from Continuum Analytics. To run iPython notebook open up a command line prompt and navigate to the directory that contains this repository. Then execute
Alternatively, you can go to my site where I have hosted a static version you can read through: brandonrose.org/pythonmap
Apologies in advance, it took some trial and error to install Basemap and fiona. I did my best to outline what I tried and what worked for me. I successfully installed on both Mac OSX and Ubuntu.
- mpl_toolkits.basemap (Basemap)
Up front, you are going to need to install some packages you might not have used before. I had to install pysal, descartes, shapely, and Basemap. Install Basemap with:
pip install basemap --allow-external basemap --allow-unverified basemap
On Ubuntu I was able to install Basemap using:
sudo apt-get install python-matplotlib sudo apt-get install python-mpltoolkits.basemap
If those options don't work you probably need to install GDAL. If you're on a Mac, go to the kyngchaos site and use the GDAL 1.11 complete installer.
If you're using Ubuntu, you should be able to install via:
sudo apt-get install gdal-bin
If you're using Windows...good luck! Let me know if you're successful getting GDAL installed and if so how you managed it.
For installing Fiona you will probably need to install
sudo apt-get install python-dev libxml2-dev libxslt-dev
and on Ubuntu potentially
sudo apt-get install g++
Installing on Ubuntu
If you still have problems installing Basemap on Ubuntu try loading Anaconda and executing the below bash command. This seems to circumvent many of the issues mentioned above.
conda install --yes ipython setuptools numpy scipy gdal pillow basemap nose
Feel free to hit me up on github or on twitter @brandonmrose