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

Raw data

2D histogram

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()

# 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)

# Mask zeros
Hmasked = np.ma.masked_where(H==0,H) # Mask pixels with a value of zero

# Plot 2D histogram using pcolor
fig2 = plt.figure()
cbar = plt.colorbar()


7 thoughts on “2D histogram

  1. Pingback: Plot 2D Histogram of Pixel Values from Two Images | spectraldifferences

  2. 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.

  3. 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?

  4. 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.
    Please help. I would be really grateful. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s