Posted By

shubham09 on 05/30/19


Tagged

python lines draw matplotlib


Versions (?)

Updating a plot using matplotlib


 / Published in: Python
 

This code is to enable updating of plot while using matplotlib

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import time
  4.  
  5. np.random.seed(26)
  6. params = np.random.rand(3)
  7.  
  8. F = lambda x: (params[2]/params[1]) - (params[0]/params[1]) * x
  9.  
  10. plt.ion()
  11. x = np.linspace(0, 1, 200)
  12. bx = plt.plot(x, F(x))
  13.  
  14. for i in range(100):
  15. bx.pop(0).remove()
  16. params = np.random.rand(3)
  17. bx = plt.plot(x, F(x))
  18. plt.pause(0.05)
  19.  
  20. for i in range(100):
  21. if 'ax' in globals(): ax.remove()
  22. newx = np.random.choice(x, size = 10)
  23. ax = plt.scatter(newx, F(newx))
  24. plt.pause(0.05)
  25.  
  26. x = input("DONE")
  27. plt.ioff()
  28. plt.show()

Report this snippet  

You need to login to post a comment.