Python图形绘制

时间:2013-06-10 15:09:03

标签: python graphics matplotlib gaussian

tdondur x坐标,y坐标s。但我不能。如果你能帮忙的话谢谢大家。现在只绘制了tdondur值。如何绘制s的值。

from math import e, exp,pow 
import pylab
from gaussxw import gaussxwab

Kb=1.3806505*10**-23
V=10**-3
p=6.022*10**28
Qd=428.0


def f(x):
    isisigasi=9*V*p*Kb*(T/Qd)**3
    return(isisigasi*x**4*(exp(x))/(exp(2)-1)**2)

tut=[] 

Tdondur=range(5,501)
tut=[None for T in Tdondur] 

for tsirala, T in enumerate (Tdondur):
    N=50
    a=0
    b=Qd/T
    x,w=gaussxwab(N,a,b)
    s=0.0
    for k in range (N):
        s+=w[k]*f(x[k])    
    print(T,"'in Isı Sığası : ",s)
    print("----------------------------------------------------------")


pylab.plot(Tdondur)
pylab.title('Isı Sığasının Sıcaklığa Göre Değişimi')
pylab.xlabel('Sıcaklık (T)')
pylab.ylabel('Isı Sığası')
pylab.gcf().savefig('isisigasi.png')
pylab.show()

1 个答案:

答案 0 :(得分:1)

这里的问题是你在循环的每次迭代中计算s的新值,但是你永远不会保存你计算的内容。相反,在进入循环之前为s创建一个数组,并在循环中设置s[k]

from math import e, exp,pow 
import pylab
from gaussxw import gaussxwab

Kb=1.3806505*10**-23
V=10**-3
p=6.022*10**28
Qd=428.0


def f(x):
    isisigasi=9*V*p*Kb*(T/Qd)**3
    return(isisigasi*x**4*(exp(x))/(exp(2)-1)**2)

tut=[] 

Tdondur=range(5,501)
tut=[None for T in Tdondur] 

s = [0. for i in Tdondur]  # initialize s before entering the loop
for tsirala, T in enumerate (Tdondur):
    N=50
    a=0
    b=Qd/T
    x,w=gaussxwab(N,a,b)
    for k in range (N):
        s[k]+=w[k]*f(x[k])    # work on the values within the loop

pylab.plot(Tdondur, s)
#s_np = pylab.array(s)
#pylab.plot(Tdondur, pylab.log10(s_np))
pylab.title('title')
pylab.xlabel('T')
pylab.ylabel('Y')
#pylab.gcf().savefig('')
pylab.show()

这是结果(注意x轴被放大):

enter image description here

通常对于这样的数据,您需要绘制数据的log,为此,请在读取后取消注释两行:

s_np = pylab.array(s)
pylab.plot(pylab.log10(s_np))

(请注意,上面这些行使用的是numpy,它是用pylab导入的,对于这类问题比列表推导更容易。)

enter image description here