רובוטרוניקס : קורס פייתון – 510 / שיעור 4 – גרף של פונקציה ושימוש ב NUMPY
קוד לשיעור נימצא בהמשך הדף
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# learn python lesson 4 , www.robotronix.co.il import matplotlib.pyplot as plt import numpy as np x = np.linspace(-2,2,100) # the function y = x**2-2 # setting the axes at the centre fig = plt.figure() # Add an Axes to the figure as part of a subplot arrangement. // default: (1, 1, 1) ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['left'].set_color('blue') ax.spines['bottom'].set_position('center') ax.spines['bottom'].set_color('green') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # plot the function r='red' plt.plot(x,y, 'red') # show the plot plt.show() import matplotlib.pyplot as plt import numpy as np # 20 linearly spaced numbers x = np.linspace( -np.pi , np.pi ,20) print(x) # function y = np.sin(x) print("print y array \r\n",y) # setting the axes at the centre fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # plot the function plt.plot(x,y, 'b') # b = blue # show the plot plt.show() import matplotlib.pyplot as plt import numpy as np # 20 linearly spaced numbers x = np.linspace(-np.pi ,np.pi ,60) # function y1 = 1*np.sin(x) y2 = 2*np.sin(2*x) y3 = 3*np.sin(4*x) # axes fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # plot the functions #x = np.linspace(-np.pi ,np.pi ,20) plt.plot(x ,y1, 'b', label='y1=sin(x)') plt.plot(x ,y2, 'r', label='y2=2sin(2x)') plt.plot(x ,y3, 'y', label='y3=3sin(4x)') plt.legend(loc='upper left') # show the plot plt.show() import matplotlib.pyplot as plt import numpy as np # 20 linearly spaced numbers x = np.linspace(-np.pi,np.pi,20) # function y1 = 1*np.sin(x) y2 = 1*np.cos(x) # axes fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # plot the functions plt.plot(x,y1, 'b', label='y1=sin(x)') plt.plot(x,y2, 'r', label='y2=cos(x)') plt.legend(loc='upper right') # show the plot plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import matplotlib.pyplot as plt import numpy as np # 20 linearly spaced numbers x = np.linspace(-10,10,200) # function y1 = 1*np.exp(x) y2 = -1*np.exp(x) # axes fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # plot the functions plt.plot(x,y1, 'b', label='y1=exp(x)') plt.plot(x,y2, 'r', label='y2=-exp(x)') plt.legend(loc='upper left') # show the plot plt.show() print("x",type(x)) print("y1",type(y1)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# learn python lesson 4 , www.robotronix.co.il import matplotlib.pyplot as plt import numpy as np #Credit #https://www.math.ubc.ca/~pwalls/math-python/integration/riemann-sums/ def derivative(f,a,method='central',h=0.01): '''Compute the difference formula for f'(a) with step size h. Parameters ---------- f : function Vectorized function of one variable a : number Compute derivative at x = a method : string Difference formula: 'forward', 'backward' or 'central' h : number Step size in difference formula Returns ------- float Difference formula: central: f(a+h) - f(a-h))/2h forward: f(a+h) - f(a))/h backward: f(a) - f(a-h))/h ''' if method == 'central': return (f(a + h) - f(a - h))/(2*h) elif method == 'forward': return (f(a + h) - f(a))/h elif method == 'backward': return (f(a) - f(a - h))/h else: raise ValueError("Method must be 'central', 'forward' or 'backward'.") x = np.linspace(-2,2,100) f = lambda x: (2*x**2 + 2*x + 1) y = f(x) dydx = derivative(f,x) print("dydx",type(dydx) , "\r\n",dydx) # axes fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # plot the functions plt.plot(x,y, 'b', label='y') plt.plot(x,dydx, 'r', label='dydx') plt.legend(loc='upper left') # show the plot plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# learn python lesson 4 , www.robotronix.co.il import matplotlib.pyplot as plt import numpy as np #Credit #https://www.math.ubc.ca/~pwalls/math-python/integration/riemann-sums/ f = lambda x : 2*x**2+5 a = 0; b = 5; N = 50 n = 10 # Use n*N+1 points to plot the function smoothly x = np.linspace(a,b,N+1) y = f(x) X = np.linspace(a,b,n*N+1) Y = f(X) plt.figure(figsize=(15,5)) plt.subplot(1,3,2) plt.plot(X,Y,'b') x_mid = (x[:-1] + x[1:])/2 # Midpoints y_mid = f(x_mid) plt.plot(x_mid,y_mid,'b.',markersize=10) plt.bar(x_mid,y_mid,width=(b-a)/N,alpha=0.2,edgecolor='b') plt.title('Midpoint Riemann Sum, N = {}'.format(N)) plt.show() |
matplotlib.org קישור לאתר של ה
https://matplotlib.org/stable/gallery/mplot3d/mixed_subplots.html?highlight=plt%20figure