如何只显示更新/实时图的最后20个结果

gkl3eglg  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(397)

每次向我的csv文件中添加新值时,我都会运行这个脚本进行更新(这是一个从机器中获取值的手动日志):

from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []

counter=0
index = count()

def animate(i):
    data = pd.read_csv('C:/Users/Owner/Downloads/Manual-Log-27Nov2020-150846.csv')
    data=data.dropna()
    data['Date And Time']=data['Date']+' '+data['Time']
    Date = data['Date And Time']
    Temp = data['Temperature']
    Pressure = data['Pressure']
    pH = data['pH']

    plt.cla()

    plt.plot(Date, Temp, label='Temperature')
    plt.plot(Date, Pressure, label='Pressure')
    plt.plot(Date, pH, label='pH')

    plt.legend(loc='upper left')
    plt.xlabel('Date and Time')
    plt.ylabel('Y Values')
    plt.xticks(rotation=90)

    plt.tight_layout()

ani = FuncAnimation(plt.gcf(), animate, interval=30000)

plt.tight_layout()
plt.show()

由于它是一个如此大的文件(20k行左右),因此生成的图形非常庞大,您无法真正正确地读取数据。有没有办法我只能让它显示最近的20个读数?

ukdjmx9f

ukdjmx9f1#

您可以将其切片或获取前n条记录。可以在代码行之后应用以下内容:data=data.dropna()

df=pd.DataFrame({'Count':[2,33,4,6,8,9],'apha':['A','B','C','D','E','F']})
df_sorted=sorted_df = df.sort_values(by=['Count'], ascending=True) # in case needed
df_limited=df_sorted.head(3) # this is one you are looking for

相关问题