简体   繁体   English

matplotlib中的交互模式

[英]Interactive mode in matplotlib

I want to dynamically update the scatter plot based on the y-axis data received from a socket connection. 我想根据从套接字连接收到的y轴数据动态更新散点图。 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. 我在交互模式下使用python matplot lib来执行此操作,但在动态更新期间,如果我将窗口移动到其他位置或最小化窗口,则绘图更新会突然停止。 How to do this? 这个怎么做?

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()

This page contains a couple of examples of dynamic plots with matplotlib and wxPython. 该页面包含几个带有matplotlib和wxPython的动态图的示例。 And here is a version with PyQt. 是PyQt的一个版本。

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 (eg wxPython ), not using matplotlib interactive mode. 我认为这最好通过使用UI工具包(例如wxPython )来处理,而不是使用matplotlib交互模式。 I had a similar question in the past and got some good answers. 我过去也有过类似的问题并得到了一些好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM