Search code examples
pythonmatplotlibdensity-plotcdf

How to use loop and range function in python to visualize plot?


Im trying to understand How to use loop and range function in python( matplotlib package ) to visualize the cdf of the Poisson distribution in a single density plot??

𝜆 = 3,4,5,6,7

import numpy as np
import matplotlib.pyplot as plt

plt.gcf().set_size_inches(12,6)

for l in range(3,8,1):
   seq = np.arrange(0,20)

How can i build the rest of the code ?


Solution

  • import numpy as np You define the range of values to calculate the cdf outside the loop, and for every iteration, you have the values as x, and the cdf as y:

    import matplotlib.pyplot as plt
    from scipy.stats import poisson
    
    Seq = np.arange(0,20)
    plt.gcf().set_size_inches(12,6)
    
    for l in range(3,8,1):
        plt.plot(Seq,poisson.cdf(Seq,l),label=l)
    plt.legend(loc="lower right")
    

    enter image description here