-1

Ok heres round 2 thank guys for help with the previous problem, but I am back to where I started unfortunately. All this happened when I tried to add a line to this graph. The incoming data is a list coming from another program. For testing purposes I am having the other program spit out [100,110]. I want 100 for one line and 110 for another. Eventually this will be incoming data from an Arduino that will be live data. I keep getting this error.

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'

Here is the code for the incoming data and where the error is occurring.

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()

Thanks for the help guys!

  • How do you define `self.plot_data`? – Sajjan Singh Sep 05 '13 at 21:42
  • self.plot_data = self.axes.plot( self.data[0], linewidth=1, color=(1, 1, 0), ) #adding a line to the plot self.plot_data = self.axes.plot( self.data[1], linewidth=1, color=(1, 2, 0), ) – Tyler Goerlitz Sep 05 '13 at 21:49
  • 1
    Can you make your question title more descriptive? 2 – tacaswell Sep 05 '13 at 22:40
  • possible duplicate of [line, = plot(x,sin(x)) what does comma stand for?](http://stackoverflow.com/questions/10422504/line-plotx-sinx-what-does-comma-stand-for) – tacaswell Sep 05 '13 at 22:41
  • You want me to change my title to match what you just wrote? Sorry I am not quite understanding. – Tyler Goerlitz Sep 05 '13 at 22:48
  • 1
    _everything_ tagged `matplotlib` is a graphing issue. Please change your title to describe your _actual_ issue. – tacaswell Sep 06 '13 at 02:52
  • The 2 was a joke because you posted two questions in a row with identical titles, except for the 2, so I left you two comments which were identical except for a 2 ;) – tacaswell Sep 06 '13 at 03:04

2 Answers2

0

Given the code from your comment:

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), )

Your issue lies in the fact that plot returns a list of the line objects it produces. Since you appear to be drawing a single line, just make sure you're looking at the first (and only) element of the list.

Either

self.plot_data = self.axes.plot( self.data[0], linewidth=1, color=(1, 1, 0), )[0]

or

self.plot_data[0].set_xdata(np.arange(len(self.data[0])))
Sajjan Singh
  • 2,523
  • 2
  • 27
  • 34
  • Ok I get what you are saying but I want two lines so my list for example is [100,110] I want one line for 100 and another for 110 – Tyler Goerlitz Sep 05 '13 at 22:29
-1

It appears the way you define plot_data returns a list. Also, I'm not sure axes.plot(*args, **kwargs), when used with one argument, is used for data on either axes. I checked in the documentation and found this:

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

Return value is a list of lines that were added. There's the type error right there. Here's the documentation for set_xdata(x):

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

And it's from the class:

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)

So you might consider declaring something like self.line = plt.lines.Line2D(self.x_data, self.y_data, linewidth=1, color=(1,1,0) ) where you'll have to use something like:

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

Hope it helps! I referenced:

update matplotlib plot

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

Community
  • 1
  • 1
Cawb07
  • 2,037
  • 3
  • 17
  • 22