Geographic Plotting with Python- Folium
I was working on this interesting dataset in Kaggle about Los Angeles Traffic Collisions. This data had details about when the accident occurred, area, zip code, latitude, longitude and many other details as below.
Since I had the latitude and longitude details I wanted to try to plot it on the map with how many traffic collisions had occurred in each of those positions. So, I created a data frame with the essential details as below.
The size and color were for the respective size and color depending on how many accidents occurred. The folium part was very simple. Installation included the following command. It installs all the underlying packages for you.
pip3 install folium
Below is the code snippet for the folium map. First I created a folium map with just 1 latitude and longitude point and a zoom start level that you want. And then for every row in the pandas data frame you you keep adding the point as a circle marker with a specific size (radius), fill color, opacity (to keep underlying points also visible), popup for any data while hovering on the point. The best part about folium is you can save it as a html page and zoom in/out, share it to anyone and they can play with the data as well.
The map looked like this.
I also created a heat map on folium. The code snipped looks like the one below. I really liked the heat map because it can also be plotted with the latitude and longitude and was very accurate.
Conclusion
Folium is by far the best geographic mapping tool with python. The coding part is very simple and you can use the latitude & longitude to plot points anywhere in the world. It can also be used to create a wide variety of graphs provided you have a correct geoJSON file mapping. Below are some useful links that can help you start with folium. You can find my code in github.
- https://python-visualization.github.io/folium/
- https://nbviewer.jupyter.org/github/python-visualization/folium/tree/master/examples/
- https://nbviewer.jupyter.org/github/python-visualization/folium_contrib/tree/master/notebooks/
- https://www.kaggle.com/daveianhickey/how-to-folium-for-maps-heatmaps-time-data
Thanks for reading through! If you find this blog useful or have any questions, don’t hesitate to reach me out. You can leave your comments below as well.