1

Have a sensor that gives me an 8x8 grid of temps that I am to display on a live heatmap. I have made an 8x8 rand to simulate that data. This heatmap should be able to run until I tell it not to run anymore. I'm using python3 and matplotlib to attempt this visualization.

I've tried may ways to make this work, including clearing the screen, turning the plt into a figure, telling show() to not block, etc. You can see many of my attempts in the comments. It either displays once only, or never displays at all (e.g. ion() and plt.show(block=False) never display any data). I've hit my head against a wall for 2 whole work days and I can't figure out why it won't display properly.

import time
import socket
import matplotlib.pyplot as plt
import numpy as np 
import random

first = True
randArray = []

#plt.ion()
plt.show(block=False)
#fig = plt.figure()
#str1 = ''.join(str(e) for e in amg.pixels)
#print (type(str1))
while True:
    #create a bunch of random numbers
    randArray = np.random.randint(0,50, size=(8,8))
    #print the array, just so I know you're not broken
    for x in randArray:
        print(x)
    #This is my attempt to clear.   
    if (first == False):
        plt.clf()
    first = False
    #basical visualization
    plt.imshow(randArray, cmap='hot', interpolation='nearest')
    plt.draw()
    plt.show()
    #fig.canvas.draw()
    #plt.canvas.draw()
    #plt.display.update
    print("Pausing...")
    time.sleep(5)

I expect the code to generate a new set of numbers every 5 seconds, and then refresh the screen with the colors of those new numbers. This should be able to run for hours if I don't interrupt, but the screen never refreshes.

More: I have tried everything listed in the post "How to update a plot in matplotlib?" and everything they do just makes it so that no graph ever populates. The launcher acts like it's going to do something by showing up in the task bar, but then does nothing. I've tried it on a Mac and a Pi, both have the same issue. Maybe it's because that post is 8 years old, and this is python 3 not python 2? Maybe it's because I use imshow() instead of plot()? I haven't figured out how to make their code work on my machine either.

Edit: I've gotten it to work on the raspberry pi thanks to the first commenters recommendations. But now I'm left wondering.... what's wrong with my Mac??

adazebra
  • 68
  • 2
  • 9
  • Possible duplicate of [How to update a plot in matplotlib?](https://stackoverflow.com/questions/4098131/how-to-update-a-plot-in-matplotlib) – Georgy Apr 11 '19 at 08:15
  • Nope. I've tried everything in that post, and still can't get anything to work. I've even tried the code that was posted that people said worked, and I can't get it to work. That recommendation is 8 years old, so I'm guessing something in Python 3 changes how this works. – adazebra Apr 12 '19 at 00:09

2 Answers2

1

This is a similar question to this one. You could try to modify your code to something like this:

import time
import socket
import matplotlib.pyplot as plt
import numpy as np 
import random

first = True
randArray = []

#plt.ion()
plt.show(block=False)
#fig = plt.figure()
#str1 = ''.join(str(e) for e in amg.pixels)
#print (type(str1))
fig = plt.figure()
for i in range(0,5):
    #create a bunch of random numbers
    randArray = np.random.randint(0,50, size=(8,8))
    #print the array, just so I know you're not broken
    for x in randArray:
        print(x)
    #This is my attempt to clear.   
    if (first == False):
        plt.clf()
    first = False
    #basical visualization
    ax = fig.add_subplot(111)
    ax.imshow(randArray, cmap='hot', interpolation='nearest')
    fig.canvas.draw()
    fig.canvas.flush_events()
    #fig.canvas.draw()
    #plt.canvas.draw()
    #plt.display.update
    print("Pausing...")
    time.sleep(2)

Have a nice day and get some rest :).

VtotheT
  • 61
  • 4
  • VtotheT, This has the same issue as my other attempts at fig.canvas. Python pops up in my menu bar (mac) and bounces, like it's going to do something, but never displays anything. It just keeps making new data, with no visualization. I would think that this is a problem with my computer, except that I tried it on a pi as well. – adazebra Apr 11 '19 at 19:39
1

Try this one:

import matplotlib.pyplot as plt
import numpy as np 

while True:
    #create a bunch of random numbers
    random_array = np.random.randint(0,50, size=(8,8))
    #print the array, just so I know you're not broken
    print(random_array)

    #clear the image because we didn't close it
    plt.clf()

    #show the image
#    plt.figure(figsize=(5, 5))
    plt.imshow(random_array, cmap='hot', interpolation='nearest')
    plt.colorbar()
    print("Pausing...")
    plt.pause(5)

   #uncomment this line and comment the line with plt.clf()
#    plt.close()

The magic is with the line plt.pause(5), it shows the image for five seconds. It's up to you if you want to close it (plt.close()) or clear it (plt.clf()). When you want to update constantly your plot, you don't use plt.show() or plt.draw(), you use plt.pause().

Uncomment some lines to try some variations... of course, some of them won't work.

David
  • 435
  • 3
  • 12