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?!

Community
  • 1
  • 1
Robert
  • 652
  • 2
  • 11
  • 23
  • 1
    You never get out of `while True` so your code never reaches `show` part. – Lafexlos Nov 30 '16 at 13:09
  • Have you checked this one http://stackoverflow.com/questions/4098131/how-to-update-a-plot-in-matplotlib or https://www.lebsanft.org/?p=48 ? – Lafexlos Nov 30 '16 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. – Robert Nov 30 '16 at 14:49

1 Answers1

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

Aakash Makwana
  • 734
  • 5
  • 9