Yeet
Yeet

Reputation: 19

Plotting the PMF of the Poission Distribution of Columns from a csv Dataframe

I am trying to finish writing a function that uses plt.bar function to plot a pmf of a poisson distribution.

I am a beginner and I don't understand how plt.bar works even after looking at https://pythonspot.com/matplotlib-bar-chart

def plot_poisson(df, col='open', n_pts=100):
fig, ax = plt.subplots(figsize=(10, 5))
plt.bar
plt.xlabel("X")
plt.ylabel("P(X|\mu)")
plt.title("Poisson PMF")
return ax

Upvotes: 1

Views: 979

Answers (2)

Ritik Sharma
Ritik Sharma

Reputation: 41

Try this one:

 def plot_poisson(df, col='open', n_pts=100):
    pts=np.arange(n_pts)
    fig,ax=plt.subplots(figsize=(10,5))
    lm = df[col].mean() #get the mean value of your data
    poisdata = np.random.poisson(lm)
    ax.bar(pts,poisdata.pmf(pts))
    ax.set_xlabel("X")
    ax.set_ylabel("P(X|\mu)")
    ax.set_title("Poisson PMF")
    return ax

Upvotes: 0

Valentino
Valentino

Reputation: 7361

So if I understood correctly, you want to draw n_pts numbers from a Poisson distribution whose average number of events per interval is equal to the mean value of your data (from the dataframe column).
And then you want to plot the distibution.

If so, you can do it easily with numpy, which provides functions to draw random numbers from distributions. For example, numpy.random.poisson draws numbers from a Poisson distribution.

It's much easier to use plt.hist instead of plt.bar.
With bar you must choose a bin size and the bin positions, count by yourself how many data you have in each bin, and plot the histogram. plt.hist does all this for you.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def plot_poisson(df, col='open', n_pts=100):
    lm = df[col].mean() #get the mean value of your data
    poisdata = np.random.poisson(lm, n_pts)
    plt.hist(poisdata, density=True, alpha=0.5)
    plt.xlabel("X")
    plt.ylabel("P(X|\mu)")
    plt.title("Poisson PMF")
    plt.show()

An example picture produced by this function:

enter image description here

Upvotes: 2

Related Questions