title: Why is this Python script with Matplotlib so slow?
body: <p>I'm trying so simulate coin tosses and profits and plot the graph in matplotlib:</p>
  <pre><code>from random import choice
  import matplotlib.pyplot as plt
  import time
  
  start_time = time.time()
  num_of_graphs = 2000
  tries = 2000
  coins = [150, -100]
  last_loss = 0
  
  
  for a in range(num_of_graphs):
      profit = 0
      line = []
      for i in range(tries):
          profit = profit + choice(coins)
          if (profit &lt; 0 and last_loss &lt; i):
              last_loss = i
          line.append(profit)
      plt.plot(line)
  plt.show()
  
  print(&quot;--- %s seconds ---&quot; % (time.time() - start_time))
  print(&quot;No losses after &quot; + str(last_loss) + &quot; iterations&quot;)
  </code></pre>
  <p>The end result is</p>
  <pre><code>--- 9.30498194695 seconds ---
  No losses after 310 iterations
  </code></pre>
  <p>Why is it taking so long to run this script? If I change <code>num_of_graphs</code> to 10000, the scripts never finishes.</p>
  <p>How would you optimize this?</p>
  <p><img src="https://i.stack.imgur.com/YEbCO.png" alt="Screenshot of the script running in Jypiter" /></p>