I'm trying to learn how to do a heatmap in Python using matplotlib. I'm going to be plotting a series of locations in a huge X,Y grid based off of an array of tuples. I found this code example which gives a perfect example of what I want to do. I can't seem to understand what the different parts of it mean though. Ultimately I want to output this on an overlay of an existing image. Thanks!
Nothing there is complicated. Anyway, here is a more simplified edition:
import pylab as pl
import numpy as np
n = 300 #number of sample data
x,y = np.random.rand(2,n) #generate random sample locations
pl.subplot(121) #sub-plot area 1 out of 2
pl.scatter(x,y,lw=0,c='k') #darw sample points
pl.axis('image') #necessary for correct aspect ratio
pl.subplot(122) #sub-plot area 2 out of 2
pl.hexbin(x,y,C=None,gridsize=15,bins=None,mincnt=1) #hexbinning
pl.scatter(x,y,lw=0.5,c='k',edgecolor='w') #overlaying the sample points
pl.axis('image') #necessary for correct aspect ratio
pl.show() #to show the plot
Sample Points:

Hexbin result:

Note that mincnt=1 avoids plotting hexagon for empty cells. A hexagon cell with dark red has more number of sample points (here 5) inside. Dark blue hexagons have only one sample inside.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With