简体   繁体   English

概率质量函数(PMF):使用matplotlib.pyplot.plot将概率绘制为列

[英]Probability Mass Function (PMF): plot probabilities as columns with matplotlib.pyplot.plot

Trying to use 'matplotlib.pyplot' to plot discreet event probabilities as columns in PMF graph. 尝试使用“ matplotlib.pyplot”将离散事件概率绘制为PMF图中的列。 Instead of complicated logic of 'hist' function I hope to achieve my goal with 'plot' using drawstyle="steps-pre" : 我希望不是使用'hist'函数的复杂逻辑,而是希望使用drawstyle =“ steps-pre”通过'plot'实现我的目标:

def plot_pmf(self):
    """" Plot PMF """
    x,y = list(self.pmf_v.keys()), list(self.pmf_v.values())
    #_=plt.plot(x,y, marker='.', linestyle='none') # plot with dots
    _=plt.plot(x,y, drawstyle="steps-pre") # plot with columns?
    _=plt.margins(0.02)
    _=plt.title(self.title)
    _=plt.xlabel(self.x_label)
    _=plt.ylabel(self.y_label)
    plt.show()

Which does not work, as Iris data shows: 虹膜数据显示,这不起作用:

x = setosa["sepal_length"]
sf = StatsFun(x,"Setosa Sepal Length","length", "probability")
pmf = sf.pmf()
print(pmf)
sf.plot_pmf()

{5.1: 0.16, 4.9: 0.08, 4.7: 0.04, 4.6: 0.08, 5.0: 0.16, 5.4: 0.1, 4.4: 0.06, 4.8: 0.1, 4.3: 0.02, 5.8: 0.02, 5.7: 0.04, 5.2: 0.06, 5.5: 0.04, 4.5: 0.02, 5.3: 0.02}

在此处输入图片说明

Please advise how to get with 'plot' function the result similar to the image below created by 'plt.hist(data, weights=weights, bins = 100)' and which works only because of 'bins = 100': 请告知如何使用“绘图”功能获得与“ plt.hist(data,weights = weights,bins = 100)”创建的下图类似的结果,并且仅由于“ bins = 100”而有效:

data = setosa["sepal_length"]
weights = np.ones_like(np.array(data))/float(len(np.array(data)))
print(weights, sum(weights))
#plt.hist(data, bins = 100) # does the same as next line
plt.hist(data, weights=weights, bins = 100)
plt.show()

[0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02
 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02
 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02
 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02] 1.0000000000000004

在此处输入图片说明

Your example data (x,y) pairs: 您的示例数据(x,y)对:

data = {5.1: 0.16, 4.9: 0.08, 4.7: 0.04, 4.6: 0.08,
        5.0: 0.16, 5.4: 0.1, 4.4: 0.06, 4.8: 0.1,
        4.3: 0.02, 5.8: 0.02, 5.7: 0.04, 5.2: 0.06,
        5.5: 0.04, 4.5: 0.02, 5.3: 0.02}

Draw a line for each (x,y) pair from (x,0) to (x,y): 为从(x,0)到(x,y)的每对(x,y)画一条线:

for x,y in data.items():
    plt.plot((x,x),(0,y))
plt.show()
plt.close()

图


If all the lines should be the same, specify a color. 如果所有行都相同,请指定颜色。

plt.plot((x,x),(0,y), 'black')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM