简体   繁体   English

Matplotlib 实时图出现 0 轴?

[英]Matplotlib live graph appearing with 0 axes?

Below is the code I have to create a live updating graph that reads values from a csv file.下面是我必须创建一个从csv文件中读取值的实时更新图的代码。 Evertime I run it it says the figure is created with 0 axes.每次我运行它时,它都会说这个图形是用 0 个轴创建的。 what am I missing?我错过了什么?

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()

Try to change your code this way:尝试以这种方式更改您的代码:

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()

In this way you instantiate the ax (where you plot your data inside the animate function) and the fig (that you pass as a parameter to the FuncAnimation ) before calling the FuncAnimation .通过这种方式,您可以在调用FuncAnimation之前实例化ax (您在其中 plot 您的数据在animate函数中)和fig (作为参数传递给FuncAnimation )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM