Python Matplotlib多处理
我正试图使它成为这样,我可以运行一个无限循环,请求用户输入,同时运行一个matplotlib简单图。有什么建议吗?目前我的代码有:Python Matplotlib多处理,python,matplotlib,multiprocessing,python-multithreading,user-interaction,Python,Matplotlib,Multiprocessing,Python Multithreading,User Interaction,我正试图使它成为这样,我可以运行一个无限循环,请求用户输入,同时运行一个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
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()
正如您所看到的,我正在尝试让进程来实现这一点,因此代码可以并行工作。最终,我希望获得它,以便用户可以显示图形,同时能够在控制台中输入。谢谢你的帮助 这就是你想要的吗
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()
我想我在评论中给了你一条太复杂的道路你正在重新发明轮子,使用已经存在于你可以嵌入matplotlib的gui框架中的主事件循环。谢谢你,你能把我链接到gui框架中对我有帮助的东西吗?我有点费劲想知道怎么做谢谢老板,但是哪一个可以让用户简单地输入呢?在我的实际程序中,我有多个图形,它们也使用文本框,但我想要一些简单的东西,用户可以直接写进去,以便设置变量的状态。它使打开图形无阻塞,因此在显示图形时,您可以在终端中做任何您想做的事情(想想MATLAB风格的界面)。是的,这太棒了,除了一件事。看起来,当你可以从控制台输入数据时——非常感谢——图形本身被冻结了。有没有办法解决这个问题?我希望用户能够在我的图表上移动和填充。你说的“冻结”是什么意思?对我来说,缩放、平移和各种图形编辑小部件都能按预期工作。每当我将光标放在图形上时,我都会遇到旋转的轮子,因此我无法与图形交互。您使用的是哪个版本、操作系统和后端?OSX 10.6.8、python 4.2.1,很抱歉,我不知道如何找到后端。努比?