首页 > 解决方案 > 关于 Matplotlib 的基本问题 - 忽略第一个值和 - 让你的 x 值滚动而不是缩小

问题描述

我有几个问题。下面是来自生物反馈工具(脑机接口)的实时图的代码。

问题1:如果你看Matplotlib 的图片,有一条线可以到达第一个值。我希望它消失,因为它会使数据缩小。有没有办法做到这一点?

问题2:记录数据时,图形保持为0,不断缩小。相反,我希望图表以相等的间隔滚动,不断记录新值。

问题 3:我怎样才能摆脱第一张图下方的两个 grahs

########### 1. Dependencies
import time
import math
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.animation
import matplotlib.pyplot as plt
import scipy
from matplotlib.figure import Figure
from scipy.signal import filtfilt

# matplotlib.use ('TkAgg') # 3 FPS
import brainflow
from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds
from brainflow.data_filter import DetrendOperations, DataFilter, FilterTypes, AggOperations, WindowFunctions
import scipy.signal as sig


class CytonStream(object):
    def __init__(self):
        ########### 2. Create Variables for Holding the Stream
        self.display_window = DataFilter.get_nearest_power_of_two(500) # Attribute, DataFilter class contains methods for signal processing
        self.data = [0]*self.display_window 

        ########### 3. Connecting to Cyton Board
        BoardShim.enable_dev_board_logger () 
        params = BrainFlowInputParams()
        board_id = BoardIds.CYTON_BOARD
        self.sample_rate = BoardShim.get_sampling_rate(board_id)
        self.board_channel = BoardShim.get_eeg_channels(board_id)[0]
        params.serial_port = '/dev/cu.usbserial-DM03H7UU' # Port
        board_id = 0 #BoardIds.CYTON_BOARD(0)
        board = BoardShim(board_id, params) # Board Stream Object
        board.prepare_session() # Prepare the session
        # board.start_stream () # use this for default options
        board.start_stream() #Create streaming thread 
        self.board = board

        ########### 4. Creating Matplotlib Parmeters/ Plots
        self.fig, axes = plt.subplots(3, 1, figsize=(11,7))
        colors = matplotlib.pyplot.rcParams['axes.prop_cycle'].by_key()['color']

        ##### Set the Axes
        self.wave_ax = axes[0] #Location 1

        ##### Set Title
        self.wave_ax.set_title("Cyton Waveform") #Title 
        
        ##### Create line objects whose data we update in the animation
        self.lines = [x[0] for x in [ax.plot(self.data, self.data, color=colors[i]) for i,ax in enumerate(axes)]]

        ##### Start animation
        self.fig.tight_layout()

        self.ani = matplotlib.animation.FuncAnimation(self.fig, self.updateFig,
                                               interval=5, blit=True) #Animation

        ##### Create list of objects that get animated
        self.ani_objects = [self.wave_ax]

        ########### 5. Animation Helpers
    def updateVars(self): #THIS IS WHERE YOU PICK THE FIRST CHANNEL
        all_data = self.board.get_current_board_data(self.display_window)
        self.curData = all_data[self.board_channel, :]

        if (len(self.curData) < self.display_window):
            self.data = self.curData
            return

    #def filtering(self, data):


    ########### 6. Animation Function
    def updateFig(self, *args):
        now = time.time()
            
        ##### Update data from the brainflow
        self.updateVars()

        if (len(self.data) == self.display_window):
            ##### Update plots with new data
            self.lines[0].set_data(self.waveX, self.data)

        else: # Set filler waveform data if the buffer hasn't filled yet
            self.lines[0].set_data(list(range(len(self.data))), self.data)

        #### Reset limits of the waveform plot so it looks nice
        self.wave_ax.relim()
        self.wave_ax.autoscale_view(tight=True)

        self.propagateChanges()
        return self.ani_objects

    def propagateChanges(self):
        self.fig.stale = True
        self.fig.canvas.draw()
        self.fig.canvas.flush_events()

    global stream

try:
    stream = CytonStream()
    plt.show()

except Exception as e:
    print("other exception,", e)
    time.sleep(1)

提前感谢所有帮助!

标签: pythonmatplotlibmatplotlib-animation

解决方案


推荐阅读