我有一个熊猫DataFrame,它是在一个时间循环更新,我想要绘制这个实时,但不幸的是,我没有得到如何做到这一点。样本代码可能是:
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()”“我会打开许多不同的窗口”。否则,我必须关闭窗口,以获得更新的情节。有什么建议吗?
发布于 2015-05-29 20:31:02
您可以为绘图指定要使用的轴,而不是每次调用它时创建不同的轴。若要在交互模式下重新绘制绘图,可以使用画而不是显示。
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()
fig = plt.figure()
ax = fig.add_subplot(111) # Create an axes.
while 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)
df.plot(secondary_y=['prex'], ax = ax) # Pass the axes to plot.
plt.draw() # Draw instead of show to update the plot in ion mode.
tm.sleep(0.5)https://stackoverflow.com/questions/30535280
复制相似问题