Функция вероятности массы (PMF): построите вероятности в виде столбцов с помощью matplotlib.pyplot.plot - PullRequest
0 голосов
/ 27 июня 2019

Попытка использовать 'matplotlib.pyplot' для построения дискретных вероятностей событий в виде столбцов на графике PMF. Вместо сложной логики функции 'Hist', я надеюсь достичь своей цели с помощью 'plot', используя drawstyle = "steps-pre":

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}

enter image description here

Посоветуйте, пожалуйста, как с помощью функции 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

enter image description here

1 Ответ

0 голосов
/ 28 июня 2019

Ваш пример данных (х, у) пар:

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, y) от (x, 0) до (x, y):

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

plotimage


Если все строки должны быть одинаковыми, укажите цвет.

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