In [1]:
import numpy as np
from tqdm import tqdm
import matplotlib # Needed for font size spec, color map transformation function bla bla
import matplotlib.pyplot as plt
%matplotlib inline
matplotlib.rc('font', size=16)
plt.rcParams['figure.figsize'] = (12.0, 10.0) # resize plots
from multihist import Hist1d, Histdd
In [2]:
# Create histograms just like from numpy...
m = Hist1d([0, 3, 1, 6, 2, 9], bins=3)
# ...or add data incrementally:
m = Hist1d(bins=100, range=(-3, 4))
m.add(np.random.normal(0, 0.5, 10**4))
m.add(np.random.normal(2, 0.2, 10**3))
# Get the data back out:
print(m.histogram, m.bin_edges)
In [3]:
# Access derived quantities like bin_centers, normalized_histogram, density, cumulative_density, mean, std
plt.plot(m.bin_centers, m.normalized_histogram, label="Normalized histogram", linestyle='steps')
plt.plot(m.bin_centers, m.density, label="Empirical PDF", linestyle='steps')
plt.plot(m.bin_centers, m.cumulative_density, label="Empirical CDF", linestyle='steps')
plt.title("Estimated mean %0.2f, estimated std %0.2f" % (m.mean, m.std))
plt.legend(loc='best')
plt.ylim(0, 1)
plt.xlim(-3, 4)
plt.show()
In [4]:
# Slicing and arithmetic behave just like ordinary ndarrays
print("The fourth bin has %d entries" % m[3])
m[1:4] += 4 + 2 * m[-27:-24]
print("Now it has %d entries" % m[3])
In [5]:
# Of course I couldn't resist adding a canned plotting function:
m.plot()
plt.show()
In [6]:
# Create and show a 2d histogram. Axis names are optional.
m2 = Histdd(bins=100, range=[[-5, 3], [-3, 5]], axis_names=['x', 'y'])
m2.add(np.random.normal(1, 1, 10**6), np.random.normal(1, 1, 10**6))
m2.add(np.random.normal(-2, 1, 10**6), np.random.normal(2, 1, 10**6))
m2.plot()
plt.show()
In [7]:
# x and y projections return Hist1d objects
m2.projection('x').plot(label='x projection')
m2.projection(1).plot(label='y projection')
plt.ylim(0, None)
plt.legend()
plt.show()
In [8]:
from scipy.stats import norm
# Demonstrate percentiles
m2.plot()
median = m2.percentile(50, 'y')
plt.plot(median.bin_centers, median, color='white', linestyle='steps-mid')
sigma_high = m2.percentile(100 * norm.cdf(1), 'y')
plt.plot(sigma_high.bin_centers, sigma_high, color='orange', linestyle='steps-mid')
sigma_low = m2.percentile(100 * norm.cdf(-1), 'y')
plt.plot(sigma_low.bin_centers, sigma_low, color='blue', linestyle='steps-mid')
plt.show()
In [9]:
sigma_low.bin_centers
Out[9]:
In [10]:
sigma_low.histogram
Out[10]:
In [ ]: