python matplotlib 中的交互模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1940387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interactive mode in matplotlib
提问by Sharath
I want to dynamically update the scatter plot based on the y-axis data received from a socket connection. I used python matplot lib in interactive mode to do this, but during dynamic updation if i move the window to a different location or minimize the window then the plot updation stops abruptly. How to do this?
我想根据从套接字连接接收到的 y 轴数据动态更新散点图。我在交互模式下使用 python matplot lib 来执行此操作,但是在动态更新期间,如果我将窗口移动到不同的位置或最小化窗口,则绘图更新会突然停止。这个怎么做?
I have attached a sample dynamic updation code used here and the same problem appears here also.
我附上了此处使用的示例动态更新代码,此处也出现了相同的问题。
import matplotlib.pyplot as plt
import random
import time
items = [25.5,26.7,23.4,22.5,20,13.4,15.6,-12,-16,20]
x = [1,2,3,4,5,6,7,8,9,10]
plt.ion() # Interactive on
for i in range(1,100):
plt.title('graph plotting')
plt.ylabel('temperature')
plt.xlabel('time')
random.shuffle(items)
plt.plot(x,items,'ob-')
plt.axis([0, 10, -40, 40])
plt.draw()
#time.sleep(2)
plt.clf()
plt.close()
回答by Eli Bendersky
回答by Ber
For this to work, you need to have a main loop for event handling, and your own event handler to redraw the plot when the window is resized or refreshed.
为此,您需要有一个用于事件处理的主循环,以及您自己的事件处理程序,以便在调整窗口大小或刷新窗口时重新绘制绘图。
You'll find many examples for this on the web, or in the tutorials.
您可以在网络上或教程中找到许多相关示例。
I think this is best handled by using a UI toolkit (e.g. wxPython), not using matplotlib interactive mode. I had a similar questionin the past and got some good answers.
我认为这最好通过使用 UI 工具包(例如wxPython)来处理,而不是使用 matplotlib 交互模式。我过去有过类似的问题,并得到了一些很好的答案。