【问题标题】:Matplotlib live graph appearing with 0 axes?Matplotlib 实时图显示为 0 轴?
【发布时间】:2020-10-15 05:07:21
【问题描述】:

下面是我必须创建一个从csv 文件读取值的实时更新图的代码。每次我运行它时,它都会说这个图形是用 0 个轴创建的。我错过了什么?

def animate(i):
    data = pd.read_csv(tool1path)
    count = range(20)
   
    
    
    plt.cla()
    plt.plot(data,count, label='Jet Height')
 
    plt.legend(loc='upper left')
    plt.tight_layout()
    
ani = FuncAnimation(plt.gcf(), animate, interval=1000)

plt.tight_layout()
plt.show()

【问题讨论】:

    标签: python csv matplotlib animation graph


    【解决方案1】:

    尝试以这种方式更改您的代码:

    fig, ax = plt.subplots()
    
    def animate(i):
        data = pd.read_csv(tool1path)
        count = range(20)
       
        
        
        plt.cla()
        ax.plot(data,count, label='Jet Height')
     
        ax.legend(loc='upper left')
        plt.tight_layout()
        
    ani = FuncAnimation(fig, animate, interval=1000)
    
    plt.tight_layout()
    plt.show()
    

    通过这种方式,您可以在调用FuncAnimation 之前实例化ax(在animate 函数中绘制数据)和fig(作为参数传递给FuncAnimation)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多