0
更新图这是我的代码到现在为止(使用蟒蛇的Python 3.4分发,Spyder的IDE):的Python matplotlib - 不关闭窗口
import numpy as np
import pylab as pl
import cmath
from fractions import Fraction
import matplotlib.offsetbox as offsetbox
#Gravity
g = 9.8
#Function to solve quadratic
def solver(k,b,m):
#Solutions
alpha = (-b/(2*m))
sol1 = (alpha + (cmath.sqrt(((b/(2*m))**2)-(k/m))))
sol2 = (alpha - (cmath.sqrt(((b/(2*m))**2)-(k/m))))
#Only 2 decimal places
alpha = "%.2f" % alpha.real
alpha = float(alpha)
sol1 = "%.2f" % sol1.real
sol1 = float(sol1)
sol2 = "%.2f" % sol2.real
sol2 = float(sol1)
#Particular solution
yp = (g*m)/k
yp = "%.2f" % yp
yp = float(yp)
#Calculating the discriminant
if ((b/2*m)**2) > (k/m):
print("y = C1*(e^%s*t) + C2*(e^%s*t) + %s"%(sol1, sol2, yp))
t = np.linspace(0, 50, 10000)
yc = np.exp(sol1*t) + np.exp(sol2*t)
y = yc + yp
pl.plot(t,y, '-g', label="y = C1*(e^%s*t) + C2*(e^%s*t) + %s"%(sol1, sol2, yp), linewidth=2)
a = pl.legend(loc='upper right',prop={'size':14})
#Extra text
txt=offsetbox.TextArea("Sobre amortiguado")
box = a._legend_box
box.get_children().append(txt)
box.set_figure(box.figure)
#Show plot
pl.show()
elif ((b/2*m)**2) == (k/m):
print("y = C1*(e^%s*t) + C2*t*(e^%s*t) + %s"%(sol1, sol2, yp))
t = np.linspace(0, 50, 10000)
yc = np.exp(sol1*t) + t*np.exp(sol2*t)
y = yc + yp
pl.plot(t,y, '-g', label="y = C1*(e^%s*t) + C2*t*(e^%s*t) + %s"%(sol1, sol2, yp), linewidth=2)
a = pl.legend(loc='upper right',prop={'size':14})
#Extra text
txt=offsetbox.TextArea("Criticamente amortiguado")
box = a._legend_box
box.get_children().append(txt)
box.set_figure(box.figure)
#Show plot
pl.show()
elif ((b/2*m)**2) < (k/m):
beta = (cmath.sqrt((k/m)-((b/(2*m))**2)))
beta = "%.2f" % beta.real
beta = float(beta)
print("y = e^(%s*t)*[C1*cos(%s*t) + C2*sen(%s*t)] + %s"%(alpha, beta, beta, yp))
t = np.linspace(0, 50, 10000)
yc = (np.exp(alpha*t))*(np.cos(beta*t) + np.sin(beta*t))
y = yc + yp
pl.plot(t,y, '-g', label="y = e^(%s*t)*[C1*cos(%s*t) + C2*sen(%s*t)] + %s"%(alpha, beta, beta, yp), linewidth=2)
a = pl.legend(loc='upper right',prop={'size':14})
#Extra text
txt=offsetbox.TextArea("Sub amortiguado")
box = a._legend_box
box.get_children().append(txt)
box.set_figure(box.figure)
#Show plot
pl.show()
#End function definition
#Execution
k = float(Fraction(input("k: ")))
b = float(Fraction(input("b: ")))
m = float(Fraction(input("m: ")))
solver(k,b,m)
我所试图做的是首先要求输入用户,然后用解决方案绘制图表;但在此之后,我希望用户能够更改其输入,以便更新图形(不关闭绘图窗口)并查看新解决方案以获取新输入。我试着把所有的东西放在while循环中,但是调出图的pl.show()阻塞了进一步的执行直到窗口关闭。
我读过一些关于使用pl.draw()和pl.ion(),但尝试了好几个小时不能得到任何的那上班,不知道我在做什么错在
感谢后提前任何帮助!
编辑:这一整天都在工作,所以我可能不会马上检查答案,但我会在一些睡眠之后;希望以后能找到一些好的答案!
我认为这是重复的。 http://stackoverflow.com/questions/4098131/update-a-plot-in-matplotlib – Repiklis 2015-04-03 09:20:06
@Repiklis我看到之前发布,但它并没有帮助 – SilverKnight 2015-04-03 19:08:17