提高程序的平滑度[加载过多数据时出现崩溃]



基本上,我正在使用Matplotlib创建一个每秒显示实时数据的图。该程序在启动时运行得很好(相当平稳-没有发现错误(。然而,在一段时间后,程序开始滞后,最终没有响应(我的假设可能是加载了太多数据?(。我正在努力实现为我的程序处理大量数据的能力。我所做的是从图表中删除网格并隐藏X轴,还使用thread单独运行函数。在开了一段时间后,仍然会遇到崩溃,这感觉只是帮助了一点,没有多大帮助。

如果有人对我如何改进程序以使其顺利运行有任何建议,请务必提供建议。我事先对此表示高度赞赏。

from __future__ import annotations
from concurrent.futures import thread
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import mplcursors
from mpl_interactions import zoom_factory
import threading 
plt.style.use('fivethirtyeight')
x_vals = []
y_vals = []
index = count()
ax = plt.gca()
def animate(i):

data = pd.read_csv('data.csv')
x = data['x_value']
y = data['total_1']

line = plt.plot(x, y)

plt.setp(line,linewidth=0.5, color='r')
mplcursors.cursor(hover=False)
threads = []
for i in range(1):
thread = threading.Thread(target=animate, args=(i,))
thread.start()
threads.append(thread)

disconnect_zoom = zoom_factory(ax)
plt.title('Live Data')
plt.rcParams['font.size'] = 8    
ax.get_xaxis().set_visible(False)
ax.grid(False)    
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.show()

下面是data.csv(时间和值(的例子,每秒钟都会累积新的数据

x_value,total_1
02:22:30-08/16/22,-0.049
02:24:00-08/16/22,0.079
02:24:02-08/16/22,0.081
02:24:03-08/16/22,0.083
02:24:04-08/16/22,0.084
02:24:05-08/16/22,0.073
02:24:06-08/16/22,0.073
02:24:07-08/16/22,0.073
02:24:08-08/16/22,0.073
02:24:09-08/16/22,0.083

这不是一个好策略,因为您正在通过plt.plot((在绘图上积累数据。最好的方法是,在调用plt.plot((之前:

  1. 获取轴:

    ax=plt.gca((

  2. 获取轴中的线条:

    lines=ax.get_lines((

  3. 删除行:

    行[0].remove((

  4. 添加新行:

    线=plt.plot(x,y(

这将防止图形包含太多行(此外,这可能不是最佳策略,具体取决于.csv的数据量(。此外,线程并没有真正执行任何操作。

编辑

我已尝试尽可能少地修改您的代码。但我已经删除了线程包含,这感觉不是一个明智的选择。试试下面的代码,让我知道:

from __future__ import annotations
from concurrent.futures import thread
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import threading 
import mplcursors

plt.style.use('fivethirtyeight')
x_vals = []
y_vals = []
index = count()
ax = plt.gca()
def animate(i):
data = pd.read_csv('data.csv')
x = data['x_value']
y = data['total_1']
# removal of previous lines
ax = plt.gca()
lines = ax.get_lines()
if len(lines) > 0:
lines[0].remove()
line = plt.plot(x, y)

plt.setp(line,linewidth=0.5, color='r')
mplcursors.cursor(hover=False)
i = 1
animate(i)

plt.title('Live Data')
plt.rcParams['font.size'] = 8    
ax.get_xaxis().set_visible(False)
ax.grid(False)    
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.show()

编辑2

我修改了代码,以利用行原语上的set_data方法。这应该会加快速度。

from __future__ import annotations
from concurrent.futures import thread
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
#import threading 
import mplcursors

plt.style.use('fivethirtyeight')
x_vals = []
y_vals = []
index = count()
ax = plt.gca()
def animate(i):
data = pd.read_csv('data.csv')
x = data['x_value']
y = data['total_1']
# removal of previous lines
ax = plt.gca()
lines = ax.get_lines()
if len(lines) == 0:
plt.plot(x,y,c="r")
else:
lines[0].set_data(x,y)
mplcursors.cursor(hover=False)
i = 1
animate(i)

plt.title('Live Data')
plt.rcParams['font.size'] = 8    
ax.get_xaxis().set_visible(False)
ax.grid(False)    
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.show()

最新更新