【发布时间】:2020-02-02 19:59:06
【问题描述】:
我正在处理一个 ML 项目,我想(相对)实时显示一个带有适应度函数的图表。
我正在使用来自 this SO answer 的代码,只要图表显示在 matplotlib 窗口中,它就可以正常工作。一旦我将它添加到页面中,它就会变成静态图像。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
max_x = 5
max_rand = 10
x = np.arange(0, max_x)
ax.set_ylim(0, max_rand)
line, = ax.plot(x, np.random.randint(0, max_rand, max_x))
def init(): # give a clean slate to start
line.set_ydata([np.nan] * len(x))
return line,
def animate(i): # update the y values (every 1000ms)
line.set_ydata(np.random.randint(0, max_rand, max_x))
return line,
ani = animation.FuncAnimation(
fig, animate, init_func=init, interval=1000, blit=True, save_count=10)
st.pyplot(plt)
知道如何为图表制作动画吗?它不一定是 matplotlib。
【问题讨论】:
-
更改函数
animate(i)以更新您的新图表数据。 -
是的,这是 stackoverflow 上的第一个精简问题 :)
标签: python matplotlib streamlit