Creating Heatmap From Scratch in Python

Heatmap is frequently used to visualize event occurrence or density. There are some Python libraries or GIS software/tool that can be used to create a heatmap like QGIS, ArcGIS, Google Table Fusion, etc.


. Unfortunately, this post won't discussed how to create a heatmap using those software/tool, but more than that, we will write our own code to create a heatmap in Python 3 from scratch using Python common library.

The algorithm which will be used to create a heatmap in Python is Kernel Density Estimation (KDE). Please refer to this post (QGIS Heatmap Using KDE Explained) to get more explanation about KDE and Best Python training in ahmedabad  another post (Heatmap Calculation Tutorial) which give an example how to calculate intensity for a point from a reference point using KDE.

Importing Library

Actually, there are some libraries in Python that can be used to create heatmap like Scikit-learn or Seaborn. But we will use just some libraries such as matplotlib, numpy and math. So we are starting with importing those three libraries.


import matplotlib.pyplot as plt

import numpy as np

import math

Heatmap Dataset


Compute Density Value for Each Grid

This is the hardest part of this post. Computing the density value for each grid. We are doing this in three looping. First loop is for mesh data list or grid. Second loop for each center point of those grids and third loop to calculate the distance of the center point to each dataset point. Using the distance, then we compute the density value of each grid with kde_quartic function which already defined before. It will return a density value for each distance to a data point. Here we only consider the point with a distance within the kernel radius. We do not consider the point outside the kernel radius and set the density value to 0. Then we sum up all density value for a grid to get the total density value for the respective grid   The total density value then is stored in a list which is called intensity_list.



The last part we visualize the result using matplotlib color mesh. We also add a color bar to see the intensity value. The heatmap result can be seen in figure 2.


Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.


10 Python Articles you can read this weekend

Hello guys, it's been a long time, since I have shared any curated list of articles, so here I am back with another of my curated article, this time with Python, one of the most loved programming language of the world.


Anyway, here is my list of some of the useful Python articles you can read this weekend ---


  1. Python at Netflix by Netflix Technology Blog

I am sure you have heard about Netflix and a good chance is that you might have even watched a couple of programs. If you are a programmer and wondering how Python is used in Netflix this article will provide you some glimpse, a perfect article for weekend reading.


  1. 10 Simple hacks to speed up your Data Analysis in Python by Parul Pandey

Python Programming language is an amazing tool for analyzing data but it can be even sweeter if you know some simple hacks. This article will teach you how to make your Data Analysis with Python a breeze. Tips and Tricks, especially in the programming world, can be very useful. Sometimes a little hack can be both time and...


  1. Best Python Courses, Tutorials, and Certifications

If you are just starting with Python or want to learn Python this year then you should first read this article. It contains some of the best Python courses, tutorials, and certifications for both beginners and intermediate developers.


  1. 36 Amazing Python Open Source Projects by Mybridge

Contributing to a Python open-source project is a great way to not only improve your Python programming skill but also to establish your authority. If you are looking to contribute, here are some of the awesome Python open course projects. For the past year, the author has compared nearly 5,000 open source Python projects and selected the top 36.


  1. Jupyter Lab: Evolution of the Jupyter Notebook by Parul Pandey

This article provides a nice overview of JupyterLab, the next generation of the Jupyter Notebook, one of the most popular IDE for Python programmers.


Here is the link:


  1. What exactly can you do with Python? Here are Python's 3 main applications. by YK Sugi

If you are still on the fence about learning Python and not sure what exactly you can do with Python then this article will clear your doubt. What exactly can you do with Python? Here are Python's 3 main applications. The most common applications of Python are web development, scripting, machine learning, and data analysis


  1. 50+ Data Structure and Algorithms Interview Questions for Programmers

Another of my articles which contains some of the frequently asked coding problems which you can solve in Python to build your programming logic and coding sense.


  1. How to scrape websites with Python and BeautifulSoup by Justin Yek

You would be amazed how easy it is to scrap websites and get information using Python and BeautifulSoup, perfect articles to see the potential of Python programming in the real world. There is more information on the Internet than any human can absorb in a lifetime. What you need is not access to that...


  1. 5 Free Courses to Learn Python for Beginners

A curated list of some of the free online courses to learn Python.


And, if you don't mind spending some for learning something as valuable as Python then you can also check these two courses which I regularly recommend to all the people who want to learn Python:


Complete Python 3 Bootcamp by Jose Portilla

The Python 3 Interactive Course on CodeCademy

That's all about some of the awesome Python articles you can read this weekend. If you have any other interesting article which you think Python programmers should read, feel free to share with us as responses.