【问题标题】:Going from 2 Axes to 1?从 2 个轴变为 1 个?
【发布时间】:2021-04-22 07:15:34
【问题描述】:

如何在不出现错误的情况下进入一个图(轴)“'AxesSubplot' 对象不可下标” 我尝试更改此行 .....但我得到了错误。

任何帮助将不胜感激。

只是一些背景知识,这是一个实时 EEG(生物传感器)流。


        #############3 Create matplotlib plots

        #self.figure = Figure() ???
        self.fig, axes = plt.subplots(2, 1, figsize=(11,7)) #2 or 1?

        prop_cycle = plt.rcParams['axes.prop_cycle'] #CHANGED
        colors = prop_cycle.by_key()['color']


        # Set the Axes Instance
        # Add brainflow waveform and FFT
        self.wave_ax = axes[0] #Location 1

        # Set titles
        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() #Creates spaces between titles :)
        
        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]

    ################################################# Animation helpers

    def updateVars(self): #THIS IS WHERE YOU PICK THE FIRST CHANNEL
        ### What's the latest?? # Using 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


    ################################## 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 #'stale' and needs to be re-drawn for the output to match the internal state
        self.fig.canvas.draw() # Redraw the current figure. This is used to update a figure that has been altered, but not automatically re-drawn
        self.fig.canvas.flush_events() # Flush the GUI events for the figure.


global stream
'''
i = 0
while i < 5:
    print("starting loop")
    i += 1
    '''
try:
    stream = CytonStream()
    plt.show()
#except KeyboardInterrupt: # Dont understand incentive behind this???
   # print('\nKeyboard interrupted, ending program')
  # stream.board.release_session()
except Exception as e:
    print("other exception,", e)
    time.sleep(1)

【问题讨论】:

  • 你能把它简化为一个最小的工作示例吗?
  • 缩短了很多。如果需要更多信息,请告诉我。
  • 如果你想保持它可订阅,请尝试为 subplots 使用挤压 = False 参数。

标签: python matplotlib matplotlib-animation


【解决方案1】:

当你可以self.fig, axes = plt.subplots(2, 1, figsize=(11,7)) 时,axes 将是一个你可以索引的子图的 numpy 数组。

当您调用 self.fig, axes = plt.subplots(1, 1, figsize=(11,7)) 时,axes 将是您无法索引的单个子图。

f1, a1 = plt.subplots(1, 1, figsize=(11,7))
f2, a2 = plt.subplots(2, 1, figsize=(11,7))
print(type(a1)) # <class 'matplotlib.axes._subplots.AxesSubplot'>
print(type(a2)) # <class 'numpy.ndarray'>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多