# How to create a 2d histogram with matplotlib ?

Published: May 14, 2019

Examples of how to create a 2d histogram with matplotlib

### Using the matplotlib hist2d function

To create a 2d histogram in python there are several solutions: for example there is the matplotlib function hist2d.

````from numpy import c_`

`import numpy as np`
`import matplotlib.pyplot as plt`
`import random`

`n = 100000`

`x = np.random.standard_normal(n)`
`y = 3.0 * x + 2.0 * np.random.standard_normal(n)`
```

Note: if the data are stored in a file (called data.txt for example):

````-1.97965874896 -7.16247661299`
`-0.326184108313 -3.30336677657`
`0.804581568977 2.01236810129`
`0.956767993891 5.73449356815`
`0.640996608682 4.80528729039`
`0.516617563432 1.89773430157`
`-0.263865123489 -0.708296452938`
`-0.282288527909 -0.938531482069`
`2.40868958562 10.4996832992`
`-1.9819139465 -7.84691502438`
`.`
`.`
`.`
```

there is the numpy function loadtxt():

````x, y = np.loadtxt("data.txt",unpack=True)`
```

Plot the 2d histogram:

````plt.hist2d(x,y)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_01.png", bbox_inches='tight')`

`plt.close()`
```

### Get histogram parameters

It is possible to get the histogram parameters:

````h, xedges, yedges, image = plt.hist2d(x,y)`

`print(h)`
`print('----------')`

`print(xedges)`
`print('----------')`

`print(yedges)`
`print('----------')`

`plt.close()`
```

### Change the bins size

The option bin can be used ti change the bins size:

````plt.hist2d(x,y,bins=50)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_02.png", bbox_inches='tight')`

`plt.close()`
```

Another example:

````x_min = np.min(x)`
`x_max = np.max(x)`

`y_min = np.min(y)`
`y_max = np.max(y)`

`x_bins = np.linspace(x_min,x_max,50)`
`y_bins = np.linspace(y_min,y_max,20)`

`plt.hist2d(x,y,bins=[x_bins,y_bins])`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_03.png", bbox_inches='tight')`

`plt.close()`
```

### Change color scale

The option cmap can be used to change the color scale (see Choosing Colormaps in Matplotlib)

````plt.hist2d(x,y,bins=50, cmap=plt.cm.jet)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_04.png", bbox_inches='tight')`

`plt.close()`
```

### Add a color bar

Add the line plt.colorbar() to create a color bar

````plt.hist2d(x,y,bins=50, cmap=plt.cm.jet)`

`plt.colorbar()`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_08.png", bbox_inches='tight')`

`plt.close()`
```

### Filter the data

Another example:

````data = c_[x,y]`

`for i in range(100):`
`    x_idx = random.randint(0,n-1)`
`    data[x_idx,0] = -999`

`data = data[data[:,0]!=-999]`

`plt.hist2d(data[:,0],data[:,1],bins=50, cmap=plt.cm.jet)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_05.png", bbox_inches='tight')`

`plt.close()`
```

### Using the matplotlib hexbin function

Another solution using the matplotlib function hexbin:

````plt.hexbin(x,y,bins=50, cmap=plt.cm.jet)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_06.png", bbox_inches='tight')`

`plt.close()`
```

### Using the numpy histogram2d function

Another solution the numpy function histogram2d

````heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)`
`#extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]`

`plt.imshow(heatmap,origin='lower')`
`#plt.imshow(heatmap,origin='lower', extent=extent)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_07.png", bbox_inches='tight')`

`plt.close()`
```

### Example of python code

````from numpy import c_`

`import numpy as np`
`import matplotlib.pyplot as plt`
`import random`

`n = 100000`

`x = np.random.standard_normal(n)`
`y = 3.0 * x + 2.0 * np.random.standard_normal(n)`

`# read data from a file:`

`#x, y = np.loadtxt("data.txt",unpack=True)`

`#for i in range(10):`
`#   print(x[i],y[i])`

`#----------------------------------------------------------------------------------------#`
`# Using matplotlib hist2d function`

`plt.hist2d(x,y)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_01.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Get 2d histogram model paramaters`

`h, xedges, yedges, image = plt.hist2d(x,y)`

`print(h)`
`print('----------')`

`print(xedges)`
`print('----------')`

`print(yedges)`
`print('----------')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Change bins size`

`plt.hist2d(x,y,bins=50)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_02.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Change bins size (custume size)`

`x_min = np.min(x)`
`x_max = np.max(x)`

`y_min = np.min(y)`
`y_max = np.max(y)`

`x_bins = np.linspace(x_min,x_max,50)`
`y_bins = np.linspace(y_min,y_max,20)`

`plt.hist2d(x,y,bins=[x_bins,y_bins])`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_03.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Change the color bar`

`plt.hist2d(x,y,bins=50, cmap=plt.cm.jet)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_04.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Add color bar`

`plt.hist2d(x,y,bins=50, cmap=plt.cm.jet)`

`plt.colorbar()`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_08.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Filter the data`

`data = c_[x,y]`

`for i in range(100):`
`    x_idx = random.randint(0,n-1)`
`    data[x_idx,0] = -999`

`data = data[data[:,0]!=-999]`

`plt.hist2d(data[:,0],data[:,1],bins=50, cmap=plt.cm.jet)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_05.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Using matplotlib hexbin function`

`plt.hexbin(x,y,bins=50, cmap=plt.cm.jet)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_06.png", bbox_inches='tight')`

`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# Using numpy hexbin function`

`heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)`
`#extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]`

`plt.imshow(heatmap,origin='lower')`
`#plt.imshow(heatmap,origin='lower', extent=extent)`

`plt.title("How to plot a 2d histogram with matplotlib ?")`

`plt.savefig("histogram_2d_07.png", bbox_inches='tight')`

`plt.close()`
```

Image

of