Is there a library that would help me to neatly plot a probability mass function of a sample in python, like this:
Asked
Active
Viewed 9,308 times
9
-
1Take a look at here: http://stackoverflow.com/questions/15415455/plotting-probability-density-function-by-sample-with-matplotlib – CentAu Feb 12 '15 at 17:59
-
1I am also looking for it. – Belter Aug 22 '17 at 13:36
2 Answers
10
Via matplotlib.pyplot's stem module
matplotlib.pyplot.stem(*args, **kwargs)
from matplotlib.pyplot import stem
stem(y, linefmt='b-', markerfmt='bo', basefmt='r-')
stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')
or closer to the metal
#!/usr/bin/env python
from pylab import *
x = linspace(0.1, 2*pi, 10)
markerline, stemlines, baseline = stem(x, cos(x), '-.')
setp(markerline, 'markerfacecolor', 'b')
setp(baseline, 'color','r', 'linewidth', 2)
show()

hello_there_andy
- 2,039
- 2
- 21
- 51
4
I think this one is neat enough,
This is the code:
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
xk = np.arange(7)
pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2)
custm = stats.rv_discrete(name='custm', values=(xk, pk))
fig, ax = plt.subplots(1, 1)
ax.plot(xk, custm.pmf(xk), 'ro', ms=8, mec='r')
ax.vlines(xk, 0, custm.pmf(xk), colors='r', linestyles='-', lw=2)
plt.title('Custom made discrete distribution(PMF)')
plt.ylabel('Probability')
plt.show()
Ref, https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.html

Belter
- 3,573
- 5
- 42
- 58