概率质量函数 (PMF):将概率绘制为具有 matplotlib.pyplot.plot 的列
Probability Mass Function (PMF): plot probabilities as columns with matplotlib.pyplot.plot
尝试使用 'matplotlib.pyplot' 将离散事件概率绘制为 PMF 图中的列。我希望使用 drawstyle="steps-pre" 通过 'plot' 实现我的目标,而不是 'hist' 函数的复杂逻辑:
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()
这不起作用,如 Iris 数据所示:
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}
请告知如何使用 'plot' 函数获得与 '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
您的示例数据 (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}
从 (x,0) 到 (x,y) 为每个 (x,y) 对画一条线:
for x,y in data.items():
plt.plot((x,x),(0,y))
plt.show()
plt.close()
如果所有线条都应该相同,请指定颜色。
plt.plot((x,x),(0,y), 'black')
尝试使用 'matplotlib.pyplot' 将离散事件概率绘制为 PMF 图中的列。我希望使用 drawstyle="steps-pre" 通过 'plot' 实现我的目标,而不是 'hist' 函数的复杂逻辑:
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()
这不起作用,如 Iris 数据所示:
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}
请告知如何使用 'plot' 函数获得与 '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
您的示例数据 (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}
从 (x,0) 到 (x,y) 为每个 (x,y) 对画一条线:
for x,y in data.items():
plt.plot((x,x),(0,y))
plt.show()
plt.close()
如果所有线条都应该相同,请指定颜色。
plt.plot((x,x),(0,y), 'black')