khaiwen1111

2nd file

Mar 8th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from scipy import optimize
  4.  
  5. get_ipython().run_line_magic('matplotlib', 'inline')
  6. fig,ax = plt.subplots(figsize=(16,8))
  7. f=np.poly1d([5,1])
  8. x=np.linspace(0,20,30)
  9. y=f(x)+6*np.random.normal(size=len(x))
  10. m,c=np.polyfit(x,y,1)
  11. xn=np.linspace(0,20,200)
  12. yn=np.polyval([m,c],xn)
  13. xn_1=np.linspace(0,15,200)
  14.  
  15. ##see how i manipulate the position of data displayed according to the legend ur lect provided##
  16. ##it should be followed by the variable of ur plot, then the title of ur legend##
  17. ## also note that there is no label in the ax.plot, it wont conflict but to avoid confusion#
  18. ## note that there is a comma at the variable##
  19. ##it is because without that comma,the plt returns those variable as a tuple, instead of a line object##
  20.  
  21. data,=ax.plot(x,y,"ob")
  22. polyfit,=ax.plot(xn,yn,color="black")
  23. ntg,=ax.plot(x,y)
  24. polyfit12,=ax.plot(xn_1,yn,"red")
  25. ax.legend((polyfit12,polyfit,ntg,data),(["Linear fit first 12 points","Linear fit first 20 points","polynomial fit","datapoints"]),ncol=2)
Add Comment
Please, Sign In to add comment