Python图形问题

时间:2013-09-05 20:53:32

标签: python matplotlib graphing

嘿伙计们我正在阅读Arduino处理它的数据并使用这个代码我发现尝试实时绘制数据。我编辑了这段代码,使其能够处理两组数据,我希望每组数据都有一行。我已经尝试了很多东西来使这段代码工作,但现在这就是我被困住的地方。我正在使用[100,110]测试数据但是当我运行代码时我得到了

IndexError                                Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/IPython/utils/py3compat.py in execfile(fname, *where)
173             else:
174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/Users/Tyler/Desktop/Arduino/Graphing_22.py in <module>()
304 if __name__ == '__main__':
305     app = wx.PySimpleApp()
--> 306     app.frame = GraphFrame()
307     app.frame.Show()
308     app.MainLoop()

 /Users/Tyler/Desktop/Arduino/Graphing_22.py in __init__(self)
 87         self.create_menu()
 88         self.create_status_bar() 
 ---> 89         self.create_main_panel()
 90 
 91         self.redraw_timer = wx.Timer(self)

/Users/Tyler/Desktop/Arduino/Graphing_22.py in create_main_panel(self)
109         self.panel = wx.Panel(self)
110 
--> 111         self.init_plot()
112         self.canvas = FigCanvas(self.panel, -1, self.fig)
113 

/Users/Tyler/Desktop/Arduino/Graphing_22.py in init_plot(self)
180         #adding a line to the plot

181         self.plot_data = self.axes.plot(
--> 182                                        self.data[1],
183                                        linewidth=1,
184                                        color=(1, 2, 0),

IndexError: list index out of range

代码真的很长,所以我会发布我认为相关的内容。如果还有其他需要,请告诉我。谢谢你的帮助。

def __init__(self):
    wx.Frame.__init__(self, None, -1, self.title)

    self.datagen = DataGen()
    self.data = [self.datagen.next()]
    #splitting data at '
    #self.data = [self.datagen.next().split(",")
    self.paused = False


    self.create_menu()
    self.create_status_bar()
    self.create_main_panel()

    self.redraw_timer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)        
    self.redraw_timer.Start(REFRESH_INTERVAL_MS)

def init_plot(self):
    self.dpi = 100
    self.fig = Figure((3.0, 3.0), dpi=self.dpi)

    self.axes = self.fig.add_subplot(111)
    self.axes.set_axis_bgcolor('black')
    self.axes.set_title('Arduino Serial Data', size=12)

    pylab.setp(self.axes.get_xticklabels(), fontsize=8)
    pylab.setp(self.axes.get_yticklabels(), fontsize=8)

    # plot the data as a line series, and save the reference 
    # to the plotted line series
    #
    self.plot_data = self.axes.plot(
                                    self.data[0], 
                                    linewidth=1,
                                    color=(1, 1, 0),
                                    )[0]

    #adding a line to the plot
    self.plot_data = self.axes.plot(
                                   self.data[1],
                                   linewidth=1,
                                   color=(1, 2, 0),
                                    )[1]

1 个答案:

答案 0 :(得分:0)

错误消息IndexError: list index out of range表示行182 data[1]超出范围 - 这意味着没有data[1]元素(数据为len 1或len 0) 。您需要在代码中找到构建data列表的行,以了解为何会出现这种情况。