Python Forum

Full Version: Help rendering a pylab plot
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm just learning Python and I'm wondering if someone could help me get the chart to render properly in the following code, i.e. plot the sequence of data points. I have put print statements so I can see if the calculations are correct which they are.

Thanks

from pylab import *

# Returns the summer tterature of seawater (in degrees Celsius) according to Mackenzie’s model.
def some_function(ff, dd):
    if dd >=0 and dd <=200:
        tt = (22/-90)*ff+24
    elif dd >=200 and dd <=1000:
        st = (22/-90)*(ff)+24
        gg = (st-2)/-800
        tt = gg*dd+(gg*-1000+2)
    else:
        tt = 2.0
    return tt

ff = float(25)
for dd in range (0, 1200, 100):
    tt1 = some_function(ff, dd)
    plot(dd,tt1)
    print(dd)
    print(tt1)
title("Something")
xlabel("x label")
ylabel("y label")
show()
Just vectorize your function and plot it:

from pylab import *
 
# Returns the summer tterature of seawater (in degrees Celsius) according to Mackenzie’s model.
@np.vectorize
def some_function(ff, dd):
    if dd >=0 and dd <=200:
        tt = (22/-90)*ff+24
    elif dd >=200 and dd <=1000:
        st = (22/-90)*(ff)+24
        gg = (st-2)/-800
        tt = gg*dd+(gg*-1000+2)
    else:
        tt = 2.0
    return tt
 
ff = float(25)
dd = np.arange(0, 1200, 100)
tt1 = some_function(ff, dd)
plot(dd,tt1)
title("Something")
xlabel("x label")
ylabel("y label")
show()