Skip to content
Advertisement

how to turn dictionary of integers into a heat map using matplotlib

I am wondering how I can make a heatmap using matplotlib. I cant really explain it so ill just show you an example. here is an example grid

JavaScript

it is stored in a dictionary like this

JavaScript

I was wondering how I can convert this dictionary into a heat map using matplotlib.

Advertisement

Answer

Here’s a very barebones example:

JavaScript

basic heatmap plot

Note that I assume you’re plotting in a square grid (hence how n is calculated). You’d need to wrangle your data a bit more if this isn’t the case.

The main takeaway however, is that you need to figure out a way to store your z values as a 2D array. In this case, we used the dictionary keys to get the row, col pair where the value should go, and ended up with the array:

JavaScript

Of course, now you’ll need to style the plot. I suggest taking a look at the docs.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement