0

I have been trying to get an updated plot from a pandas dataframe without success. My problem: the plot window does not appear (it is not hidden - I am sure about that).

I already tried to rebuild and change different solutions from stackoverflow. My most recent try is based on this post. Pure copy,paste does work, so the problem needs to be in my modifications.

I changed it to this as I want to update it automatically every second.

import serial as s
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from time import sleep

data = pd.DataFrame(np.random.random((10,10))) # I want to use pandas because 
# of easier timestamp handling   

fig, ax = plt.subplots()
ax.set(title='title')
im = ax.imshow(data)

while True:
    im.set_data(np.random.random((10,10)))
    print "hello" #just to see if sth happens
    fig.canvas.draw()
    sleep(1)

plt.show()

Just to explain: Later I want to read data from serial ports and feed them to the plot to get my data visualized.

Well, what you expect: the provided code does print hello each second but does not show me any plot. Any ideas? I am out of them.

By the way: I am surprised that there is no "easy, straight forward" solution for this kind of problem to be found. I can imagine, there is some people who are trying to do updated plots?!

3
  • 1
    You never get out of while True so your code never reaches show part. Commented Nov 30, 2016 at 13:09
  • Have you checked this one stackoverflow.com/questions/4098131/… or lebsanft.org/?p=48 ? Commented Nov 30, 2016 at 13:16
  • @Lafexlos:True, the plt.show() is somewhat redundant, but fig.canvas.draw() is supposed to show the plot. And I checked both of your proposed links but did not get the result I was aiming for. Commented Nov 30, 2016 at 14:49

1 Answer 1

1

you can use the package drawnow

from pylab import * # import matplotlib before drawnow
from drawnow import drawnow, figure
from time import sleep
import numpy as np

def draw_fig_real():
    imshow(data, interpolation='nearest')

data = np.random.random((10,10))

figure()
for i in range(10):
    data = np.random.random((10,10))
    sleep(0.5)
    drawnow(draw_fig_real)

Hope this helps

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

1 Comment

haven't given it much more than a quick try but it looks promising!

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.