title: Python read multiple .txt and plot in same figure
body: <p>I have two .txt files of this form with thousands of rows</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>episode:2840  score:6.0  memory length:700345  epsilon:0.5576491831247304   steps:359    recent reward:5.95
  episode:2841  score:4.0  memory length:700629  epsilon:0.5574965123606621   steps:284    recent reward:5.96
  episode:2842  score:5.0  memory length:700911  epsilon:0.5573449596383373   steps:282    recent reward:5.93
  Max lifes :5
  episode:2800  score:7.0  memory length:687331  epsilon:0.5646918805492361   steps:268    recent reward:6.04
  episode:2801  score:5.0  memory length:687678  epsilon:0.5645029051613243   steps:347    recent reward:6.03
  episode:2802  score:6.0  memory length:688027  epsilon:0.564312906709082   steps:349    recent reward:6.03</code></pre>
  </div>
  </div>
  </p>
  
  <p>I want to plot how the <code>recent_reward</code> column, changes over the <code>episodes</code>. After I remove the <code>Max lifes :5</code> and some rows where the <code>episode</code> repeats, I get as a result two different plots, one for every file. However, the goal is to plot them together in one figure, to show the difference of the two <code>rewards</code> of the files over <code>episodes</code>. Could somebody help please?</p>
  
  <p>This is my code :</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import matplotlib.pyplot as plt
  import glob
  
  
  # Read any .txt output file
  path = '/home/plots/*.txt' 
  files = glob.glob(path)
  
  
  for name in files:
      # Read lines from file
      with open(name) as f:
          lines = f.readlines()
  
      # Extract episode/reward data points
      episodes = []
      rewards = []
      for line in lines:
          columns = line.split()
          if not columns[0].startswith("episode"):
              # Not a line with episode/reward data
              continue
          try:
              episode = int(columns[0].split(":")[1])
              reward = float(columns[7].split(":")[1])
          except:
              # Not a number
              continue
          if episode in episodes:
              # We don't want repeated episodes
              continue
          episodes.append(episode)
          rewards.append(reward)
  
  
      print(episodes,rewards)
      
      #plot both in the same figure
      #plt.plot(episodes, rewards, episodes, rewards)
      #plt.show()
      
      # Plot data every 1000 steps
      N = 1000
      for i in range(int(len(l_episodes)/N)+1):
          plt.plot(l_episodes[i*N:i*N+N], l_rewards[i*N:i*N+N])
          plt.xlabel('episodes', fontsize=12)
          plt.ylabel('rewards', fontsize=12)
          plt.savefig("plot{}.png".format(i))
          plt.close()</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Output figure</strong></p>
  
  <p><a href="https://i.stack.imgur.com/prCmT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/prCmT.png" alt="enter image description here"></a></p>