【问题标题】:Python graphing issue 2Python 绘图问题 2
【发布时间】:2013-09-05 21:40:09
【问题描述】:

好的,这是第 2 轮,感谢大家对上一个问题的帮助,但不幸的是,我又回到了开始的地方。当我尝试在该图中添加一条线时,这一切都发生了。传入的数据是来自另一个程序的列表。出于测试目的,我让另一个程序吐出 [100,110]。我想要一条线 100 条,另一条线 110 条。最终,这将是来自 Arduino 的传入数据,这些数据将是实时数据。我不断收到此错误。

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.plot_data = self.axes.plot( self.data[0], linewidth=1, color=(1, 1, 0), ) #在图中添加一条线 self.plot_data = self.axes.plot(self.data[1], linewidth=1, color=(1, 2, 0), )
  • 你能让你的问题标题更具描述性吗? 2
  • everything 标记为matplotlib 是一个图形问题。请更改您的标题以描述您的实际问题。

标签: python matplotlib graphing


【解决方案1】:

鉴于您评论中的代码:

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]

self.plot_data[0].set_xdata(np.arange(len(self.data[0])))

【讨论】:

  • 好的,我明白你的意思,但我想要两行,所以我的列表例如是 [100,110] 我想要一行 100,另一行 110
【解决方案2】:

您定义plot_data 的方式似乎返回了一个列表。另外,我不确定axes.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_xdata(x) 的文档:

set_xdata(x)
    Set the data np.array for x
    ACCEPTS: 1D array

它来自班级:

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.line = plt.lines.Line2D(self.x_data, self.y_data, linewidth=1, color=(1,1,0) ) 之类的内容,您必须使用以下内容:

self.x_data = self.axes.plot( self.data[0] )
self.y_data = self.axes.plot( self.data[1] )

希望对您有所帮助!我参考了:

update matplotlib plot

http://matplotlib.org/api/axes_api.html http://matplotlib.org/api/artist_api.html#matplotlib.lines.Line2D.set_xdata

【讨论】:

  • 好吧,这确实有很大帮助。现在让我问这个,我想要做的是让 x 轴和我现在一样的时间和 y 轴作为我的传入数据。所以会有两行,一条从列表的 0 元素读取数据,另一行从列表的 1 元素读取数据。
  • 这也几乎奏效了。我没有收到任何错误消息,但它没有绘制任何线条
  • 最后有没有写 plt.plot() ?
  • 老实说,我什至不知道把它放在程序的哪个位置
  • 最后(底部),朋友:)。
猜你喜欢
  • 2018-12-28
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 2017-04-06
  • 2016-08-15
相关资源
最近更新 更多