# 2D histogram

Sometimes there is too much data in a scatter plot. Therefore, it is hard to see if there are “points over points”. In this case, 2D histograms are very useful.

Raw data

2D histogram

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

# Create some random numbers
n = 100000
x = np.random.randn(n)
y = (1.5 * x) + np.random.randn(n)

# Plot data
fig1 = plt.figure()
plt.plot(x,y,'.r')
plt.xlabel('x')
plt.ylabel('y')

# Estimate the 2D histogram
nbins = 200
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)

# H needs to be rotated and flipped
H = np.rot90(H)
H = np.flipud(H)

# Plot 2D histogram using pcolor
fig2 = plt.figure()
plt.xlabel('x')
plt.ylabel('y')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')

```

## 7 thoughts on “2D histogram”

1. This is awesome. I was trying to create 2D histograms that look exactly like this and now I don’t have to use ROOT anymore. They’re beautiful plots.

2. Isn’t rotate and flip H exactly the same as transposing H?

If the dimensions for x and y are different and you don’t transpose H, the plot doesn’t show at all, instead an error appears.
for example:
“Dimensions of H (24, 10) are incompatible with x (25) and/or x (11)”

Am I not getting the point here or is this simply a bug or a lousy implementation?

3. Hi,

I tried the exact same code here because I wanted my program to do the exact same thing. However, it did not quite make a good image. I wish i could have posted it here. Anyways, so information –
My Y axis goes from 10^(-2) to 10^(3) (Logscale)
My X axis goes from 10^(-5) to 10^(19)(Logscale)
The only other noticeable thing seems to be that my Counts are from 8000 – 64000.