title: plotting data from a list in python
body: <p>I need to plot the velocities of some objects(cars).</p>
  
  <p>Each velocity are being calculated through a routine and written in a file, roughly through this ( I have deleted some lines to simplify):</p>
  
  <pre><code>thefile_v= open('vels.txt','w') 
  
  for car in cars:
      velocities.append(new_velocity) 
  
       if len(car.velocities) &gt; 4:
            try:
                thefile_v.write("%s\n" %car.velocities) #write vels once we get 5 values
                thefile_v.close
  
            except:
                print "Unexpected error:", sys.exc_info()[0]
                raise
  </code></pre>
  
  <p>The result of this is a text file with list of velocities for each car.</p>
  
  <p>something like this:</p>
  
  <pre><code>[0.0, 3.8, 4.5, 4.3, 2.1, 2.2, 0.0]
  [0.0, 2.8, 4.0, 4.2, 2.2, 2.1, 0.0]
  [0.0, 1.8, 4.2, 4.1, 2.3, 2.2, 0.0]
  [0.0, 3.8, 4.4, 4.2, 2.4, 2.4, 0.0]
  </code></pre>
  
  <p>Then I wanted to plot each velocity </p>
  
  <pre><code>with open('vels.txt') as f:
      lst = [line.rstrip() for line in f]
  
  plt.plot(lst[1]) #lets plot the second line
  plt.show()
  </code></pre>
  
  <p>This is what I found. The values are taken as a string and put them as yLabel. </p>
  
  <p><img src="https://i.stack.imgur.com/AllOs.png" alt="enter image description here"></p>
  
  <p>I got it working through this:</p>
  
  <pre><code>from numpy import array
  
  y = np.fromstring( str(lst[1])[1:-1], dtype=np.float, sep=',' )
  plt.plot(y)
  plt.show()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/dW0yF.png" alt="enter image description here"></p>
  
  <p>What I learnt is that, the set of velocity lists I built previously were treated as lines of data. </p>
  
  <p>I had to convert them to arrays to be able to plot them. However the brackets [] were getting into the way. By converting the line of data to string and removing the brackets through this (i.e. [1:-1]).</p>
  
  <p>It is working now, but I'm sure there is a better way of doing this. </p>
  
  <p>Any comments?</p>