
from collections import namedtuple
import matplotlib.pyplot as plt

BCand = namedtuple('BCand', ['m', 'merr', 'pt', 'p'])

bs = []

with open('B.txt') as f:
    for line in f.readlines()[1:]:
        bs.append(BCand(*[float(v) for v in line.strip().split(',')]))

masses = [b.m for b in bs]

plt.hist(masses, 60, histtype='stepfilled')
plt.xlabel(r'$m_B / \mathrm{GeV}$')
plt.savefig('mass.pdf')


from collections import namedtuple
import matplotlib.pyplot as plt
import numpy as np

BCand = namedtuple('BCand', ['m', 'merr', 'pt', 'p'])

bs = [BCand(*b) for b in np.genfromtxt('B.txt', skip_header=1, delimiter=',')]

masses = [b.m for b in bs]

ns, bins, _ = plt.hist(masses, 60, histtype='stepfilled', facecolor='r',
                       edgecolor='none')
centers = bins[:-1] + (bins[1:] - bins[:-1]) / 2
merr = np.sqrt(ns)
plt.errorbar(centers, ns, yerr=merr, fmt='b+')
plt.xlabel(r'$m_B / \mathrm{GeV}$')
plt.savefig('mass.pdf')

Use numpy for readin and add errorbars.
Use numpy for readin and add errorbars.

  
  from collections import namedtuple
  import matplotlib.pyplot as plt
+ import numpy as np
  
  BCand = namedtuple('BCand', ['m', 'merr', 'pt', 'p'])
  
+ bs = [BCand(*b) for b in np.genfromtxt('B.txt', skip_header=1, delimiter=',')]
- bs = []
- 
- with open('B.txt') as f:
-     for line in f.readlines()[1:]:
-         bs.append(BCand(*[float(v) for v in line.strip().split(',')]))
  
  masses = [b.m for b in bs]
  
- plt.hist(masses, 60, histtype='stepfilled')
+ ns, bins, _ = plt.hist(masses, 60, histtype='stepfilled', facecolor='r',
+                        edgecolor='none')
+ centers = bins[:-1] + (bins[1:] - bins[:-1]) / 2
+ merr = np.sqrt(ns)
+ plt.errorbar(centers, ns, yerr=merr, fmt='b+')
  plt.xlabel(r'$m_B / \mathrm{GeV}$')
  plt.savefig('mass.pdf')
  
Use numpy for readin and add errorbars.
## Code Before:

from collections import namedtuple
import matplotlib.pyplot as plt

BCand = namedtuple('BCand', ['m', 'merr', 'pt', 'p'])

bs = []

with open('B.txt') as f:
    for line in f.readlines()[1:]:
        bs.append(BCand(*[float(v) for v in line.strip().split(',')]))

masses = [b.m for b in bs]

plt.hist(masses, 60, histtype='stepfilled')
plt.xlabel(r'$m_B / \mathrm{GeV}$')
plt.savefig('mass.pdf')

## Instruction:
Use numpy for readin and add errorbars.
## Code After:

from collections import namedtuple
import matplotlib.pyplot as plt
import numpy as np

BCand = namedtuple('BCand', ['m', 'merr', 'pt', 'p'])

bs = [BCand(*b) for b in np.genfromtxt('B.txt', skip_header=1, delimiter=',')]

masses = [b.m for b in bs]

ns, bins, _ = plt.hist(masses, 60, histtype='stepfilled', facecolor='r',
                       edgecolor='none')
centers = bins[:-1] + (bins[1:] - bins[:-1]) / 2
merr = np.sqrt(ns)
plt.errorbar(centers, ns, yerr=merr, fmt='b+')
plt.xlabel(r'$m_B / \mathrm{GeV}$')
plt.savefig('mass.pdf')
