在python中整齐地绘制PMF

时间:2015-02-12 17:54:29

标签: python matplotlib plot

是否有一个库可以帮助我在python中整齐地绘制样本的概率质量函数,如下所示:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:4)

通过matplotlib.pyplot的词干模块

  

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-')

或更接近金属

#!/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()

enter image description here

Here

答案 1 :(得分:1)

我认为这个很整洁,

enter image description here

这是代码:

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()

参考,https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.html