Python实时绘图

2024-05-14 00:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我用两个数组获取一些数据:一个是时间数组,一个是值数组。当我到达1000点时,我触发一个信号并绘制这些点(x=时间,y=值)。

我需要保持之前的图的相同数字,但只有一个合理的数字,以避免放缓进程。例如,我想在我的图表上保留10000个点。 matplotlib交互绘图工作良好,但我不知道如何删除第一个点,它使我的计算机速度非常快。 我研究了matplotlib.animation,但它似乎只是重复相同的情节,并没有真正实现它。

我真的在寻找一个简单的解决方案,以避免任何放缓。

当我获取大量时间时,我会删除每个循环上的输入数据(1001点存储在第一行中,以此类推)。

这是我现在所拥有的,但它保留了图表上的所有点:

import matplotlib.pyplot as plt

def init_plot():
  plt.ion()
  plt.figure()
  plt.title("Test d\'acqusition", fontsize=20)
  plt.xlabel("Temps(s)", fontsize=20)
  plt.ylabel("Tension (V)", fontsize=20)
  plt.grid(True)

def continuous_plot(x, fx, x2, fx2):
  plt.plot(x, fx, 'bo', markersize=1)
  plt.plot(x2, fx2, 'ro', markersize=1)
  plt.draw()

我调用init函数一次,而continuous_plot处于一个进程中,每次数组中有1000个点时都会调用它。


Tags: 数据plot进程matplotlibinitdef图表时间
3条回答

我知道我来不及回答这个问题了,英国电信对于你的问题你可以查一下“操纵杆”包。它基于line.set_data()和canvas.draw()方法,具有可选的轴重新缩放。它还允许交互式文本记录或图像打印(除了图形打印)。 不需要在单独的线程中执行自己的循环,包会处理它,只需给出您希望的更新频率。此外,控制台仍可用于其他监视命令。 请参阅http://www.github.com/ceyzeriat/joystick/https://pypi.python.org/pypi/joystick(使用pip安装操纵杆进行安装)

尝试:

import joystick as jk
import numpy as np
import time

class test(jk.Joystick):
    # initialize the infinite loop decorator
    _infinite_loop = jk.deco_infinite_loop()

    def _init(self, *args, **kwargs):
        """
        Function called at initialization, see the doc
        """
        self._t0 = time.time()  # initialize time
        self.xdata = np.array([self._t0])  # time x-axis
        self.ydata = np.array([0.0])  # fake data y-axis
        # create a graph frame
        self.mygraph = self.add_frame(jk.Graph(name="test", size=(500, 500), pos=(50, 50), fmt="go-", xnpts=10000, xnptsmax=10000, xylim=(None, None, 0, 1)))

    @_infinite_loop(wait_time=0.2)
    def _generate_data(self):  # function looped every 0.2 second to read or produce data
        """
        Loop starting with the simulation start, getting data and
    pushing it to the graph every 0.2 seconds
        """
        # concatenate data on the time x-axis
        self.xdata = jk.core.add_datapoint(self.xdata, time.time(), xnptsmax=self.mygraph.xnptsmax)
        # concatenate data on the fake data y-axis
        self.ydata = jk.core.add_datapoint(self.ydata, np.random.random(), xnptsmax=self.mygraph.xnptsmax)
        self.mygraph.set_xydata(t, self.ydata)

t = test()
t.start()
t.stop()

使用固定大小的数组并使用matplot绘制。

 import collections
 array = collections.deque([None] * 1000, maxlen=1000)

当您附加到数组时,它将删除第一个元素。

最轻的解决方案是替换现有绘图的X和Y值。(或仅在X数据不变的情况下使用Y值。一个简单的例子:

import matplotlib.pyplot as plt
import numpy as np
import time

fig = plt.figure()
ax = fig.add_subplot(111)

# some X and Y data
x = np.arange(10000)
y = np.random.randn(10000)

li, = ax.plot(x, y)

# draw and show it
ax.relim() 
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.show(block=False)

# loop to update the data
while True:
    try:
        y[:-10] = y[10:]
        y[-10:] = np.random.randn(10)

        # set the new data
        li.set_ydata(y)

        fig.canvas.draw()

        time.sleep(0.01)
    except KeyboardInterrupt:
        break

这个解决方案也很快。上面代码的最大速度是每秒100次重绘(受time.sleep的限制),我得到大约70-80次重绘,这意味着一次重绘大约需要4毫秒,但YMMV取决于后端等等

相关问题 更多 >