python - Plotting a histogram from pre-counted data in Matplotlib -
i'd use matplotlib plot histogram on data that's been pre-counted. example, have raw data
data = [1, 2, 2, 3, 4, 5, 5, 5, 5, 6, 10]
given data, can use
pylab.hist(data, bins=[...])
to plot histogram.
in case, data has been pre-counted , represented dictionary:
counted_data = {1: 1, 2: 2, 3: 1, 4: 1, 5: 4, 6: 1, 10: 1}
ideally, i'd pass pre-counted data histogram function lets me control bin widths, plot range, etc, if had passed raw data. workaround, i'm expanding counts raw data:
data = list(chain.from_iterable(repeat(value, count) (value, count) in counted_data.iteritems()))
this inefficient when counted_data
contains counts millions of data points.
is there easier way use matplotlib produce histogram pre-counted data?
alternatively, if it's easiest bar-plot data that's been pre-binned, there convenience method "roll-up" per-item counts binned counts?
you can use weights
keyword argument np.histgram
(which plt.hist
calls underneath)
val, weight = zip(*[(k, v) k,v in counted_data.items()]) plt.hist(val, weights=weight)
assuming only have integers keys, can use bar
directly:
min_bin = np.min(counted_data.keys()) max_bin = np.max(counted_data.keys()) bins = np.arange(min_bin, max_bin + 1) vals = np.zeros(max_bin - min_bin + 1) k,v in counted_data.items(): vals[k - min_bin] = v plt.bar(bins, vals, ...)
where ... ever arguments want pass bar
(doc)
if want re-bin data see histogram separate list denoting frequency
Comments
Post a Comment