Simple Histogram in Python Matplotlib

Reading time ~2 minutes

So matplotlib is really neat and powerful yet elegant and easy to use. Here we have this simple hist with all you need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
import numpy as np
import pylab

# read file
with open('.../nums.txt', 'r') as f:
    data = f.readlines()
    nums = []
    for line in data:
        nums.append(int(line))

#find central tendency measures
mu = np.mean(nums)
median = np.median(nums)
sigma = np.std(nums)

#plot
fig, ax = plt.subplots(1)
ax.hist(nums, bins = 30, color = 'c')
ax.axvline(np.mean(nums), color='b', linestyle='dashed', linewidth=2)
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
textstr = '$\mu=%.2f$\n$\mathrm{median}=%.2f$\n$\sigma=%.2f$'%(mu, median, sigma)
ax.text(0.65, 0.95, textstr, transform=ax.transAxes, fontsize=14,
        verticalalignment='top', bbox=props)
pylab.show()

Which produces this: Histogram

PS nums.txt it’s just a file with numbers.

How to Download Multiple Instagram Photos at Once?

You want to print your Insta photos or just keep them save and archived? Out of luck - there is no obvious way to download them, unless y...… Continue reading

Polymer and Vulcanization

Published on August 08, 2016

Jeff Dean of Google

Published on August 06, 2016