【问题标题】:Line on animated matplotlib graph not showing up动画 matplotlib 图表上的线条未显示
【发布时间】:2018-08-10 09:18:13
【问题描述】:

我不确定为什么我的小部件上没有显示图表线。

x 轴应随时间移动,当我打印应绘制的 y 值 (Temp) 时,新值会按预期出现。但是,绘图上没有显示线/点。

我在过去的绘图中遇到过类似的问题,通过更改绘制点的样式(即使用 'r*' 使点可见红星)解决了这个问题。我不确定如何在这段代码中实现同样的事情。

import matplotlib.pyplot as plt
import numpy
import datetime
import matplotlib.animation as animation

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    Time = []
    Temp = []
    x = datetime.datetime.now()
    y = numpy.random.randint(48,52)
    Time.append(x)
    Temp.append(int(y))    

    ax1.plot(Time,Temp)
    print(Temp)

ani = animation.FuncAnimation(fig,animate, interval=1000)
plt.show()

【问题讨论】:

    标签: python-3.x animation matplotlib plot widget


    【解决方案1】:

    只需将 Time 和 Temp 放在函数 animate 之外

    Time = []
    Temp = []
    def animate(i):
        x = datetime.datetime.now()
        y = numpy.random.randint(48,52)
        Time.append(x)
        Temp.append(int(y))    
        ax1.plot(Time,Temp)
        print(Temp)
    

    【讨论】:

      【解决方案2】:

      您可以添加marker,它会显示积分:

      ax1.plot(Time,Temp, marker="s")
      

      【讨论】:

      • 谢谢你,超级有帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多