title: Progressively append and plot new data to a matplotlib line
body: <h2>Question</h2>
  
  <p>What is the way to append data to an existing matplotlib line and <strong>plot only the added portion of the line</strong> without redrawing the whole line?</p>
  
  <h2>Comments</h2>
  
  <p>Following is a simple code that plots the redraw time vs. the number of times we append a portion of data to the line.</p>
  
  <p>You see the redraw time increases nearly linearly with the <em>total</em> size of data in the line. This points to the fact that the whole line is redrawn. I'm looking for a way to plot only a new portion of the line. In this case, the redraw time is expected to be nearly constant for the code below.</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  import time
  
  # User input
  N_chunk = 10000
  N_iter = 100
  
  # Prepare data
  xx = list(range(N_chunk))
  yy = np.random.rand(N_chunk).tolist()
  
  # Prepare plot
  fig, ax = plt.subplots()
  ax.set_xlim([0,N_chunk])  # observe only the first chunk
  line, = ax.plot(xx,yy,'-o')
  fig.show()
  
  # Appending data and redraw
  dts = []
  for i in range(N_iter):
      t0 = time.time()
      xs = xx[-1]+1
      xx.extend(list(range(xs,xs+N_chunk)))
      yy.extend(np.random.rand(N_chunk).tolist())
      line.set_data(xx,yy)
      fig.canvas.draw()
      dt = time.time() - t0
      dts.append(dt)
      plt.pause(1e-10)
  plt.close()
  
  # Plot the time spent for every redraw
  plt.plot(list(range(N_iter)), dts, '-o')
  plt.xlabel('Number of times a portion is added')
  plt.ylabel('Redraw time [sec]')
  plt.grid()
  plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/bnEZB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bnEZB.png" alt="Redraw time depending on the data size"></a></p>