有一个传感器给我一个8x8网格的温度,我将在实时热图上显示。我已经制作了一个8x8 rand来模拟这些数据。这个热图应该可以运行,直到我告诉它不再运行。我正在使用python3和matplotlib来尝试这种可视化。
我已经尝试过如何使这项工作,包括清理屏幕,将plt变成一个数字,告诉show()不要阻止等等。你可以在评论中看到我的许多尝试。它只显示一次,或者根本不显示(例如ion()和plt.show(block = False)从不显示任何数据)。我已经把头撞到墙上整整两个工作日,我无法弄清楚为什么它不能正常显示。
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)
我希望代码每5秒生成一组新的数字,然后用这些新数字的颜色刷新屏幕。如果我不中断,这应该可以运行几个小时,但屏幕永远不会刷新。
更多:我已尝试过帖子“如何更新matplotlib中的绘图?”中列出的所有内容。他们所做的一切都是为了让所有人都不会填充图表。启动器的行为就像是通过在任务栏中显示来做某事,但后来什么也没做。我在Mac和Pi上尝试过,两者都有同样的问题。也许这是因为那个帖子是8岁,这是python 3而不是python 2?也许是因为我使用imshow()而不是plot()?我还没弄明白如何让他们的代码在我的机器上运行。
编辑:由于第一个评论者的建议,我已经得到它在树莓派工作。但现在我不知道......我的Mac出了什么问题?
这是与this one类似的问题。您可以尝试将代码修改为以下内容:
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)
祝你有愉快的一天,休息一下:)
试试这个:
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()
神奇的是plt.pause(5)
线,它显示图像五秒钟。如果你想关闭它(plt.close()
)或清除它(plt.clf()
),这取决于你。如果你想不断更新你的情节,你不使用plt.show()
或plt.draw()
,你使用plt.pause()
。
取消注释某些行以尝试一些变化......当然,其中一些不起作用。