Search code examples
pythonplotpoint

Draw a plot and show points on Python


I am using Python to draw a 2-axis plot.

X-axis is 'Q'(integer); Y-axis is 'Z(Q)', a function of 'Q'.

I would like Q to be integer from 0, 1, 2, ..., 10 for example. And I have Z(Q) already defined and it runs okay.

I want to see the plot of Z(Q) versus Q with each point (0, 1, 2, ..., 10) on the plot.

Can I write:

for Q in range(0, 10):
   pl.plot(Q,Z(Q))

And it returns error.

Error Message:

name 'Q' is not defined.

How could I change the code in defining Q?

Thank you!

Appendix - Entire Code:

import numpy as np
import scipy.stats as stats
from scipy.stats import poisson, norm

mu = 4.68
cs = 100
co = 300
G = poisson(mu)
p = G.pmf(np.arange(3*mu))

def Z(Q):
    ES = sum(i*p[i] for i in range(len(p)))
    return co*max((Q-ES), 0) + cs*max((ES-Q), 0)

Qstar = np.ceil(poisson.ppf(co/(cs+co), mu))
print(Qstar)


from scipy.integrate import quad
import pylab as pl


for Q in range(0, Qstar):
    pl.plot(Q,Z(Q))

And this is my full code, the last two lines do not return any result, the rest is fine. Thank you!


Solution

  • The problem is the numpy data conversion, this can not be used in the range () function, I have corrected the code considering the above.

    import numpy as np
    import scipy.stats as stats
    from scipy.stats import poisson, norm
    
    mu = 4.68
    cs = 100
    co = 300
    G = poisson(mu)
    p = G.pmf(np.arange(3*mu))
    
    def Z(Q):
        ES = sum(i*p[i] for i in range(len(p)))
        return co*max((Q-ES), 0) + cs*max((ES-Q), 0)
    
    Qstar = np.ceil(poisson.ppf(co/(cs+co), mu))
    print(Qstar)
    
    Qstar = int(np.float64(Qstar).item())
    
    
    from scipy.integrate import quad
    import pylab as pl
    
    
    for Q in range(0, Qstar):
        pl.plot(Q,Z(Q), '*')
    
    pl.show()
    

    enter image description here

    Lines:

    import numpy as np
    import scipy.stats as stats
    from scipy.stats import poisson, norm
    
    mu = 4.68
    cs = 100
    co = 300
    G = poisson(mu)
    p = G.pmf(np.arange(3*mu))
    
    def Z(Q):
        ES = sum(i*p[i] for i in range(len(p)))
        return co*max((Q-ES), 0) + cs*max((ES-Q), 0)
    
    Qstar = np.ceil(poisson.ppf(co/(cs+co), mu))
    print(Qstar)
    
    Qstar = int(np.float64(Qstar).item())
    
    
    from scipy.integrate import quad
    import pylab as pl
    
    x = []
    
    y = []
    
    for Q in range(0, Qstar):
        x.append(Q)
        y.append(Z(Q))
    
    pl.plot(x, y)
    
    pl.show()
    

    enter image description here