title: Plot a rolling window with Matplotlib
body: <p>I want to plot a time series in a <code>while</code> loop as a rolling window: The graph should always show the 10 most recent observations.</p>
  <p>My idea was to use a <a href="https://docs.python.org/2/library/collections.html#collections.deque" rel="nofollow noreferrer">deque object</a> with <code>maxlen=10</code> and plot it in every step.
  To my great surprise the plot <em>appends</em> new values to the old plot; apparently it remembers values that are no longer inside the deque! Why is that and how can I switch it off?</p>
  <p><a href="https://i.stack.imgur.com/h5XJe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h5XJe.jpg" alt="enter image description here" /></a></p>
  <p>This is a minimal example of what I am trying to do. The plotting part is based on <a href="https://stackoverflow.com/a/15720891/4743630">this post</a> (although <code>plt.ion()</code> did not change anything for me, so I left it out):</p>
  <pre><code>from collections import deque
  import matplotlib.pyplot as plt
  import numpy as np
  
  x = 0
  data = deque(maxlen=10)
  
  while True:
      x += np.abs(np.random.randn())
      y = np.random.randn()
      data.append((x, y))
  
      plt.plot(*zip(*data), c='black')
      plt.pause(0.1)
  </code></pre>
  <p>I also tried to use Matplotlib's animation functions instead, but could not figure out how to do that in an infinite <code>while</code> loop...</p>