【发布时间】:2021-09-22 06:49:49
【问题描述】:
我正在构建一个机器人,它每 30 秒从互联网上获取数据。
我想使用 matplotlib 绘制这些数据,并在我获取一些新数据时能够更新图表。
在脚本的开头,我初始化了我的情节。 然后我运行一个函数每 30 秒更新一次这些绘图,但此时我的绘图窗口冻结了。
我做了一些研究,但似乎找不到任何可行的解决方案:
plt.show(block=False)
plt.pause(0.001)
我做错了什么?
代码的一般结构:
import matplotlib.pyplot as plt
import time
def init_plots():
global fig
global close_line
plt.ion()
fig = plt.figure()
main_fig = fig.add_subplot(111)
x = [datetime.fromtimestamp(x) for x in time_series]
close_line, = main_fig.plot(x, close_history, 'k-')
plt.draw()
def update_all_plots():
global close_line
x = [datetime.fromtimestamp(x) for x in time_series]
close_line.set_xdata(time_series)
close_line.set_ydata(close_history)
plt.draw()
# SCRIPT :
init_plots()
while(True):
# Fetch new data...
# Update time series...
# Update close_history...
update_plots()
time.sleep(30)
【问题讨论】:
标签: python matplotlib