Search code examples
pythonmatplotlibplotheatmapbinning

Matplotlib: How to increase size of bin in Heatmap (hexbin)?


Matplotlib: How can I set the size of the bins created using hexbin? The problem i have is that the bins are just much too small so that the graphic looks somehow weird.


Solution

  • Using pylab_examples example code: hexbin_demo.py, you can easily play around with all of the optional arguments to see how they affect the same dataset. Here is what gridsize does:

    enter image description here


    I just copy and pasted the code from the demo (including the data creation part) then made subplots with different values for the same parameter like this

    plt.subplots_adjust(hspace=0.5)
    plt.subplot(132)
    plt.hexbin(x, y, cmap=plt.cm.YlOrRd_r)
    plt.axis([xmin, xmax, ymin, ymax])
    plt.title("Gridsize 100 (default)")
    cb = plt.colorbar()
    cb.set_label('counts')
    
    plt.subplot(133)
    plt.hexbin(x, y, cmap=plt.cm.YlOrRd_r, gridsize = 200)
    plt.axis([xmin, xmax, ymin, ymax])
    plt.title("Gridsize 200")
    cb = plt.colorbar()
    cb.set_label('counts')
    
    plt.subplot(131)
    plt.hexbin(x, y, cmap=plt.cm.YlOrRd_r, gridsize = 10)
    plt.axis([xmin, xmax, ymin, ymax])
    plt.title("Gridsize 10")
    cb = plt.colorbar()
    cb.set_label('counts')
    
    plt.show()
    plt.close()