0

I am new to both the python and matplotlib languages and working on something for my husband.

I hope you guys can help me out.

I would like to pull in a file using Open, read it, and update a graph with it's values.

Sounds easy enough right? Not so much in practice.

Here is what I have so far to open and chart the file. This works fine as it is to chart the file 1 time.

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

Now I would like to animate the chart or update it so that I can see changes to the data. I have tried to use matplotlib's animation feature but it is advanced beyond my current knowledge.

Is there a simple way to update this chart, say every 5 minutes?

Note: I tried using Schedule but it breaks the program (maybe a conflict between schedule and having matplotlib figures open??).

Any help would be deeply appreciated.

1 Answer 1

1

Unfortunately you will just waste time trying to get a clean solution without either using matplotlib's animation feature or using the matplotlib OO interface.

As a dirty hack you can use the following:

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

It spins off however a second thread by Timer. So to kill the script, you have to hit Ctrl-C twice on the console.

I myself would be interested if there is a cleaner way to do this in this simple manner in the confines of the pyplot machinery.

Edits in italic.

Sign up to request clarification or add additional context in comments.

2 Comments

It would be better to explicitly get a reference to and Axes object and us the OO interface, rather than rely on the pyplot state machine.
This example shall check how far one can get with the pyplot machinery. Regarding the OO interface: Explain that to the thread opener :). Edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.