gpt4 book ai didi

python - 实时数据帧图

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:47 24 4
gpt4 key购买 nike

我有一个在 while 循环中更新的 pandas 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()"""我会打开许多​​不同的窗口。否则我必须关闭窗 Eloquent 能获得更新的情节。有什么建议吗?

最佳答案

您可以为 plot 指定轴每次调用它时使用而不是创建不同的轴。要以交互模式重绘绘图,您可以使用 draw而不是展示。

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)

关于python - 实时数据帧图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30535280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com