我试图使它能够运行一个无限循环,要求用户输入以及运行一个matplotlib简单图。有什么建议吗?目前,我的代码是:
def createGraph():
fig = plt.figure()
fig.suptitle('A Graph ', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=.9)
ax.set_xlabel('X Score')
ax.set_ylabel('Y Score')
plt.plot([1,2,3,4,5,6,7],[1,3,3,4,5,6,7], 'ro')
plt.show()
def sub_proc(q,fileno):
sys.stdin = os.fdopen(fileno) #open stdin in this process
some_str = ""
while True:
some_str = raw_input("> ")
if some_str.lower() == "quit":
return
q.put_nowait(some_str)
if __name__ == "__main__":
q = Queue()
fn = sys.stdin.fileno() #get original file descriptor
qproc = Process(target=sub_proc, args=(q,fn))
qproc.start()
qproc.join()
zproc = Process(target=createGraph)
zproc.start()
zproc.join()正如您所看到的,我正在尝试让进程使其工作,因此代码可以并行工作。最终,我想得到它,以便用户可以显示一个图形,同时能够在控制台中输入。谢谢你的帮助!!
发布于 2013-09-14 19:35:14
这是你想要的吗?
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
fig, ax = plt.subplots(1, 1)
theta = np.linspace(0, 2*np.pi, 1024)
ln, = ax.plot(theta, np.sin(theta))
plt.ion()
plt.show(block=False)
while True:
w = raw_input("enter omega: ")
try:
w = float(w)
except ValueError:
print "you did not enter a valid float, try again"
continue
y = np.sin(w * theta)
ln.set_ydata(y)
plt.draw()我想我把你的评论写得太复杂了
https://stackoverflow.com/questions/18803960
复制相似问题