我有一个Pandas DataFrame,它在while循环中更新,我想实时绘制它,但是不幸的是我不知道如何做到这一点。 一个示例代码可能是:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import time as tm
from datetime import datetime, date, time
import pandas as pd
columns = ["A1", "A2", "A3", "A4","A5", "B1", "B2", "B3", "B4", "B5", "prex"]
df = pd.DataFrame()
"""plt.ion()"""
plt.figure()
while not True:
now = datetime.now()
adata = 5 * np.random.randn(1,10) + 25.
prex = 1e-10* np.random.randn(1,1) + 1e-10
outcomes = np.append(adata, prex)
ind = [now]
idf = pd.DataFrame(np.array([outcomes]), index = ind, columns = columns)
df = df.append(idf)
ax = df.plot(secondary_y=['prex'])
plt.show()
time.sleep(0.5)
但是如果我取消注释“plt.ion()”,将会打开许多不同的窗口。否则,我必须关闭窗口才能获得更新的绘图。
有什么建议吗?