Skip to content

Instantly share code, notes, and snippets.

@HeinrichHartmann
Created January 15, 2016 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HeinrichHartmann/38f4f2a281e2aebe547e to your computer and use it in GitHub Desktop.
Save HeinrichHartmann/38f4f2a281e2aebe547e to your computer and use it in GitHub Desktop.
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