Python 3.x 蟒蛇3x+;MatPlotLib-更新图表?

Python 3.x 蟒蛇3x+;MatPlotLib-更新图表?,python-3.x,matplotlib,Python 3.x,Matplotlib,我对python和matplotlib语言都是新手,正在为我丈夫做一些事情 我希望你们能帮我 我想使用Open拉入一个文件,读取它,并用它的值更新一个图形 听起来很简单,对吧?实际上没有这么多 以下是我到目前为止打开并绘制该文件的内容。这很好,因为它是图表文件的一次 import matplotlib.pyplot as plt fileopen = open('.../plotresults.txt', 'r').read() fileopen = eval(fileopen) ##becau

我对python和matplotlib语言都是新手,正在为我丈夫做一些事情

我希望你们能帮我

我想使用Open拉入一个文件,读取它,并用它的值更新一个图形

听起来很简单,对吧?实际上没有这么多

以下是我到目前为止打开并绘制该文件的内容。这很好,因为它是图表文件的一次

import matplotlib.pyplot as plt
fileopen = open('.../plotresults.txt', 'r').read()
fileopen = eval(fileopen) ##because the file contains a dict and security is not an issue.
print(fileopen)  ## So I can see it working
for key,value in fileopen.items():
    plot1 = value
    plt.plot(plot1, label=str(key))
plt.legend()
plt.show()
现在,我想制作图表的动画或对其进行更新,以便查看数据的更改。我曾尝试使用matplotlib的动画功能,但它的先进性超出了我目前的知识

有没有一个简单的方法来更新这个图表,比如说每5分钟更新一次

注: 我尝试使用Schedule,但它破坏了程序(可能是Schedule和打开matplotlib图形之间存在冲突??)


任何帮助都将不胜感激。

不幸的是,如果不使用matplotlib的动画功能或matplotlib OO界面,您将浪费时间尝试获得干净的解决方案

作为肮脏的黑客,您可以使用以下方法:

from threading import Timer

from matplotlib import pyplot as plt
import numpy

# Your data generating code here
def get_data():
    data = numpy.random.random(100)
    label = str(data[0]) # dummy label
    return data, label

def update():
    print('update')
    plt.clf()
    data, label = get_data()
    plt.plot(data, label=label)
    plt.legend()
    plt.draw()
    t = Timer(0.5, update) # restart update in 0.5 seconds
    t.start()

update()
plt.show()
然而,它通过
定时器
来剥离第二个线程。因此,要终止脚本,必须在控制台上按两次
Ctrl-C

如果在
pyplot
机器的范围内有一种更干净的方法以这种简单的方式完成这项工作,我自己也会感兴趣


以斜体编辑。

最好明确地获取对对象和对象的引用,而不是依赖pyplot状态机。此示例将检查使用pyplot机器可以走多远。关于OO接口:向线程开启器解释:)。编辑了我的答案。