Python绘图第2期
好的,这里是第二轮谢谢大家对前一个问题的帮助,但不幸的是,我回到了我开始的地方。所有这些都发生在我试图给这个图表添加一条线的时候。传入数据是来自另一个程序的列表。出于测试目的,我让其他程序吐出[100110]。我想一条线100英镑,另一条线110英镑。最终,这将是来自Arduino的实时数据。我一直在犯这个错误Python绘图第2期,python,matplotlib,graphing,Python,Matplotlib,Graphing,好的,这里是第二轮谢谢大家对前一个问题的帮助,但不幸的是,我回到了我开始的地方。所有这些都发生在我试图给这个图表添加一条线的时候。传入数据是来自另一个程序的列表。出于测试目的,我让其他程序吐出[100110]。我想一条线100英镑,另一条线110英镑。最终,这将是来自Arduino的实时数据。我一直在犯这个错误 AttributeError Traceback (most recent call last) /Users/Tyler/Desk
AttributeError Traceback (most recent call last)
/Users/Tyler/Desktop/Arduino/Graphing_22.py in on_redraw_timer(self, event)
284 #self.data.extend(self.datagen.next())
285
--> 286 self.draw_plot()
287
288 def on_exit(self, event):
/Users/Tyler/Desktop/Arduino/Graphing_22.py in draw_plot(self)
240 visible=self.cb_xlab.IsChecked())
241
--> 242 self.plot_data.set_xdata(np.arange(len(self.data[0])))
243 #self.plot_data.set_xdata(np.arange([1,1000])
244 self.plot_data.set_ydata(np.array(self.data[1]))
AttributeError: 'list' object has no attribute 'set_xdata'
以下是传入数据的代码以及发生错误的位置
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
if self.cb_grid.IsChecked():
self.axes.grid(True, color='gray')
else:
self.axes.grid(False)
# Using setp here is convenient, because get_xticklabels
# returns a list over which one needs to explicitly
# iterate, and setp already handles this.
#
pylab.setp(self.axes.get_xticklabels(),
visible=self.cb_xlab.IsChecked())
self.plot_data.set_xdata(np.arange(len(self.data[0])))
#self.plot_data.set_xdata(np.arange([1,1000])
self.plot_data.set_ydata(np.array(self.data[1]))
self.canvas.draw()
谢谢你们的帮助 给出您评论中的代码:
self.plot_data = self.axes.plot( self.data[0], linewidth=1, color=(1, 1, 0), )
self.axes.plot( self.data[1], linewidth=1, color=(1, 2, 0), )
问题在于plot
返回它生成的线对象列表。由于您似乎在绘制一条直线,因此只需确保您正在查看列表的第一个(也是唯一一个)元素
或者
self.plot_data = self.axes.plot( self.data[0], linewidth=1, color=(1, 1, 0), )[0]
或
它以定义
plot\u data
返回列表的方式出现。另外,我不确定轴。当与一个参数一起使用时,plot(*args,**kwargs)
是否用于任一轴上的数据。我查看了文档,发现:
plot(x, y) # plot x and y using default line style and color
plot(x, y, 'bo') # plot x and y using blue circle markers
plot(y) # plot y using x as index array 0..N-1
plot(y, 'r+') # ditto, but with red plusses
返回值是已添加行的列表。这里有一个类型错误。以下是set\u xdata(x)
的文档:
它来自于课堂:
matplotlib.lines.Line2D(xdata, ydata, linewidth=None, linestyle=None, color=None,
marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None,
markerfacecolor=None, markerfacecoloralt='none', fillstyle='full',
antialiased=None, dash_capstyle=None, solid_capstyle=None, dash_joinstyle=None,
solid_joinstyle=None, pickradius=5, drawstyle=None, markevery=None, **kwargs)
<>你可能会考虑声明一些代码,比如“代码> Self.Loe= PLT.Loo.Lim2D(Self.xyDATA,Self.YyDATA,线宽=1,颜色=(1,1,0)),在这里你必须使用一些类似的词:
self.x_data = self.axes.plot( self.data[0] )
self.y_data = self.axes.plot( self.data[1] )
希望有帮助!我提到:
如何定义
self.plot\u data
?self.plot\u data=self.axes.plot(self.data[0],线宽=1,颜色=(1,1,0),)#在plot self.plot_data=self.axes.plot(self.data[1],linewidth=1,color=(1,2,0))中添加一条线,可以使问题标题更具描述性吗?2所有标记的matplotlib
都可能重复,这是一个绘图问题。请更改标题以描述您的实际问题。好的,我明白您的意思,但我想要两行,例如我的列表是[100110],我想要一行写100,另一行写110Ok,这确实很有帮助。现在让我问一下,我想做的是,让x轴和我现在的时间一样,让y轴作为我的输入数据。所以会有两行,一行从我列表的0元素读取数据,另一行从我列表的1元素读取数据。这几乎奏效了。我没有收到任何错误消息,但它不会绘制任何线条。您是否在结尾处编写plt.plot()?老实说,我甚至不知道在程序的末尾(底部)将其放在哪里,朋友:)。
matplotlib.lines.Line2D(xdata, ydata, linewidth=None, linestyle=None, color=None,
marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None,
markerfacecolor=None, markerfacecoloralt='none', fillstyle='full',
antialiased=None, dash_capstyle=None, solid_capstyle=None, dash_joinstyle=None,
solid_joinstyle=None, pickradius=5, drawstyle=None, markevery=None, **kwargs)
self.x_data = self.axes.plot( self.data[0] )
self.y_data = self.axes.plot( self.data[1] )