Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple (inefficient) example of how to create a histogram with python
from matplotlib import pyplot as plt
import numpy as np
X = np.genfromtxt("DataSets/RequestRates.csv", delimiter=",")[:,1]
bins = [500, 700, 800, 900, 1000, 1500, 1800, 2000, 2200]
bin_count = len(bins) - 1
sample_counts = [0] * bin_count
for x in X:
for i in range(bin_count):
if (bins[i] <= x) and (x < bins[i + 1]):
sample_counts[i] += 1
bin_widths = [ float(bins[i] - bins[i-1]) for i in range(1, bin_count) ]
bin_heights = [ count/width for count, width in zip(sample_counts,bin_widths) ]
plt.bar(bins[:bin_count-1], width=bin_widths, height=bin_heights);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment