You can calculate the total volume by multiplying each value in h
with the width and height of its corresponding bin:
import matplotlib.pyplot as plt
import numpy as np
h, xedges, yedges, _ = plt.hist2d(np.random.randn(1000).cumsum(), np.random.randn(1000).cumsum(),
bins=(20, 30), density=True)
total_volume = np.sum(h * np.diff(xedges).reshape(-1, 1) * np.diff(yedges).reshape(1, -1))
print("total_volume =", total_volume) # prints "total_volume = 1.0"
The volume of the histogram without density=True
is the size of one bin multiplied by the number of samples. The width of all bins is xedges[-1]-xedges[0]
. The height is yedges[-1]-yedges[0]
. The area of one bin is the area of all divided by the number of bins (20*30=600
in the example).
import matplotlib.pyplot as plt
import numpy as np
h, xedges, yedges, _ = plt.hist2d(np.random.randn(1000).cumsum(), np.random.randn(1000).cumsum(),
bins=(20, 30), density=False)
total_volume = np.sum(h * np.diff(xedges).reshape(-1, 1) * np.diff(yedges).reshape(1, -1))
print("total volume :", total_volume)
print(" predicted :", (xedges[-1] - xedges[0]) * (yedges[-1] - yedges[0]) / 600 * 1000)
This prints for example:
total volume : 4057.2494712526022
predicted : 4057.2494712526036
So, just a very small rounding error.