#83 basic 2D Histograms with matplotlib

This page is dedicated to 2D histograms made with matplotlib, through the hist2D function. 2D histograms are useful when you need to analyse the relationship between 2 numerical variables that have a huge number of values. It avoids the over plotting matter that you would observe in a classic scatterplot. These 3 first examples illustrate the importance to play with the bins argument. You can explicitly tell how many bins you want for the X and the Y axis, showing a slightly different visualisation.


# libraries
import matplotlib.pyplot as plt
import numpy as np

# create data
x = np.random.normal(size=50000)
y = x * 3 + np.random.normal(size=50000)

# Big bins
plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.jet)
#plt.show()

# Small bins
plt.hist2d(x, y, bins=(300, 300), cmap=plt.cm.jet)
#plt.show()

# If you do not set the same values for X and Y, the bins aren't square !
plt.hist2d(x, y, bins=(300, 30), cmap=plt.cm.jet)

#plt.show()

Then, it is possible to change the colour palette. Please visit the matplotlib reference page to see the available palette.


# Reds
plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Reds)
#plt.show()

# BuPu
plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.BuPu)
#plt.show()

Finally, it can sometimes be useful to add a color bar on the side as a legend.


plt.hist2d(x, y, bins=(50, 50), cmap=plt.cm.Greys)
plt.colorbar()
#plt.show()

Leave a Reply

Your email address will not be published.