【问题标题】:There is no error message... but the plot is not being plotted没有错误消息...但情节没有被绘制
【发布时间】:2020-08-26 05:34:54
【问题描述】:

所以我正在关注绘制定期变化的值(实时数据)的教程...我正在使用 matplotlib 并使用 FuncAnimation...我已经导入了所有必要的模块但没有答案...这里是我做了什么和输出

x = []
y = []
c = count()
def anim(i):
  x.append(next(c))
  y.append(random.randint(0,10))
  plt.cla()
  plt.plot(x,y,'or',markersize=10)
  

ani = FuncAnimation(plt.gcf(),anim,interval=5000)

输出:

<Figure size 432x288 with 0 Axes>

【问题讨论】:

  • 尝试在代码末尾添加plt.show() 可能吗?
  • 试过...同样的结果

标签: python matplotlib


【解决方案1】:

这部分解释了动画的基本结构。

  1. 配置将绘制图形的对象。 (第 5 行)
  2. 使用动画函数 (def anim()) 设置要设置动画的 Y 值。
  3. 然后更新#1中设置的graph对象的值
  4. Funcanimation()设置抽奖次数、抽奖间隔、不能重复等。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

fig, ax = plt.subplots()

im, = ax.plot([], [], 'or', markersize=10)
x = np.arange(10)

ax.set_xlim(0, 9)
ax.set_ylim(0, 1)

def anim(i):
    y = np.random.rand(10)
    im.set_data(x, y)

anim = FuncAnimation(fig, anim, frames=50, interval=200, repeat=False, blit=False)

fig.show()

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2020-09-25
    相关资源
    最近更新 更多