Reputation: 357
I'd like to create a stacked histogram out of numpy arrays with entries that are the desired bin heights.
For example I have twelve bins defined by:
bins = np.linspace(0,120,13)
And I have a few numpy arrays each with twelve elements (1 for each bin):
hist1 = [1.1,2.2,3.3,4.4,......,hist1[11]]
hist2 = [9.9,8.8,7.7,6.6,......,hist2[11]]
I'd like to turn into a stacked histogram. Is this possible with matplotlib?
Upvotes: 1
Views: 520
Reputation: 10329
The weights parameter will solve this for you. Put a single data point in each bucket, then control the height by specifying its weight
import numpy as np
import matplotlib.pyplot as plt
bins = np.linspace(0,120,13)
# my approximation of your example arrays
hist1 = np.arange(1.1,15, 1.1)
hist2 = np.arange(15, 1.1, -1.1)
all_weights = np.vstack([hist1, hist2]).transpose()
all_data = np.vstack([bins, bins]).transpose()
num_bins = len(bins)
plt.hist(x = all_data, bins = num_bins, weights = all_weights, stacked=True, align = 'mid', rwidth = .5)
If your bins weren't equally spaced they would start to be different thicknesses but aside from that, this should solve your problem.
Upvotes: 1