python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt.gca().set_prop_cycle( ’ color ’ , [ ’ red ’ , ’ green ’ , ’ blue ’ , ’ black ’ ]) 6 7 class MyLine: 8 9 def __init__(self, * args, ** options): 10 #TO DO: IMPLEMENT FUNCTION 11 pass 12 13 def draw(self): 14 plt.plot(abscissa,self.line(abscissa)) 15 16 def get_line(self): 17 return "y = x + ".format(self.slope, self.intercept) 18 19 def __str__(self): 20 return self.get_line() 21 22 def __mul__(self,other): 23 #TO DO:IMPLEMENT FUNCTION 24 pass 25 26 x1 = MyLine((0,0), (5,5),options = "2pts") 27 x1.draw() 28 x2 = MyLine((5,0),-1/4, options = "point-slope") 29 x2.draw() 30 x3 = MyLine("(-4/5) * x + 5", options = "lambda") 31 x3.draw() 32 x4 = MyLine("x + 2", options = "lambda") 33 x4.draw() 34 35 print("The intersection of and is ".format(x1,x2,x1 * x2)) 36 print("The intersection of and is ".format(x1,x3,x1 * x3)) 37 print("The intersection of and is ".format(x1,x4,x1 * x4)) 38 39 40 plt.legend([x1.get_line(), x2.get_line(), x3.get_line(),x4.get_line()], ← ↩ loc= ’ upper left ’ ) 41 plt.show() Problem 2: Lines In class we reviewed the equation of lines: (10) Ay We also discussed "variable and"variable, the former binding to a tuple of actual parameters and the latter allowing you to set switches. In this problem, you'll complete a Line class that not only draws the lines, but allows someone to describe the line by using two points, a point and slope, or simply the formula. For the formula, we assume the variable is always in r You'll overload the operator to find the intersection of two lines and display (x.y) when they intersection and 0 when they do not. You should assume, at a minimum the class has three instance variables (but with some choices, it might have more) 1. slope 2. intercept 3. line-A x: f(x) For example, the line y 1.3z-8 would have slope of 1.3, intercept of 8 and lineAx 1.3*x-8 (12) For this problem, you only need to implement the_init and overloaded *function. lineclass 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa np.arange(20) 5 plt.gca().set_prop_cycle( color, ['red, 'green 'blue' 'black']) 7 class MyLine def-init-(self, *args , **options): 10 #TODO: IMPLEMENT FUNCTION pass 12 13 14 15 16 def draw(self): plt.plot(abscissa,self.line(abscissa)) def get_line(self): return "y 0:.2fłx 1:.2f".format(self.slope, self.intercept) 18 19 20 21 def _str__(self): return self.get line() def _mul__(self,other): 23 #TODO: IMPLEMENT FUNCTION 24 25 26 x1MyLine( (0,0), (5,5),options"2pts" 27 x1.draw() 28 x2MyLine( (5,0),-1/4, options"point-slope") 29 x2.draw) 30 x3 MyLine("(-4/5)*X+5", options"lambda" 31 x3.draw) 32 x4 - MyLine("x + 2", optionslambda") 33 x4.draw( 34 35 print("The intersection of (o and 11 is (2)".format (x1,x2,x1*x2)) 36 print("The intersection of tO and (1 is (2)". format(x1,x3,x1*x3)) 37 print("The intersection of tO and 1 is (2)". format(x1,x4,x1*x4)) 38 39 40 plt.legend([x1.get line(), x2.get line), x3.getline),x4.get_line()], - pass loc- 'upper left') 41 plt.show) 一y·1.00x +0.00 _ y#-0.25x + 1.25 y--0.80x +5.00 20 15- y-1.00x+2.00 10 -10 0.0 2.5 5.0 7.5 10.012.5 15.0 17.5 Figure 1: Plot of 4 lines. Session Output The intersection of y 1.00x0.00 and y --0.25x 1.25 is: The intersection of y = 1.00x + 0.00 and y = -0.80x + 5.00 is: (2.778, 2.778) The intersection of y -1.00x+0.00 and y1.00x+2.00 is: C) Deliverables Programming Problem 2 Complete the two functions Put your code for this problem in a new module named lineclass.py Problem 2: Lines In class we reviewed the equation of lines: (10) Ay We also discussed "variable and"variable, the former binding to a tuple of actual parameters and the latter allowing you to set switches. In this problem, you'll complete a Line class that not only draws the lines, but allows someone to describe the line by using two points, a point and slope, or simply the formula. For the formula, we assume the variable is always in r You'll overload the operator to find the intersection of two lines and display (x.y) when they intersection and 0 when they do not. You should assume, at a minimum the class has three instance variables (but with some choices, it might have more) 1. slope 2. intercept 3. line-A x: f(x) For example, the line y 1.3z-8 would have slope of 1.3, intercept of 8 and lineAx 1.3*x-8 (12) For this problem, you only need to implement the_init and overloaded *function. lineclass 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa np.arange(20) 5 plt.gca().set_prop_cycle( color, ['red, 'green 'blue' 'black']) 7 class MyLine def-init-(self, *args , **options): 10 #TODO: IMPLEMENT FUNCTION pass 12 13 14 15 16 def draw(self): plt.plot(abscissa,self.line(abscissa)) def get_line(self): return "y 0:.2fłx 1:.2f".format(self.slope, self.intercept) 18 19 20 21 def _str__(self): return self.get line() def _mul__(self,other): 23 #TODO: IMPLEMENT FUNCTION 24 25 26 x1MyLine( (0,0), (5,5),options"2pts" 27 x1.draw() 28 x2MyLine( (5,0),-1/4, options"point-slope") 29 x2.draw) 30 x3 MyLine("(-4/5)*X+5", options"lambda" 31 x3.draw) 32 x4 - MyLine("x + 2", optionslambda") 33 x4.draw( 34 35 print("The intersection of (o and 11 is (2)".format (x1,x2,x1*x2)) 36 print("The intersection of tO and (1 is (2)". format(x1,x3,x1*x3)) 37 print("The intersection of tO and 1 is (2)". format(x1,x4,x1*x4)) 38 39 40 plt.legend([x1.get line(), x2.get line), x3.getline),x4.get_line()], - pass loc- 'upper left') 41 plt.show) 一y·1.00x +0.00 _ y#-0.25x + 1.25 y--0.80x +5.00 20 15- y-1.00x+2.00 10 -10 0.0 2.5 5.0 7.5 10.012.5 15.0 17.5 Figure 1: Plot of 4 lines. Session Output The intersection of y 1.00x0.00 and y --0.25x 1.25 is: The intersection of y = 1.00x + 0.00 and y = -0.80x + 5.00 is: (2.778, 2.778) The intersection of y -1.00x+0.00 and y1.00x+2.00 is: C) Deliverables Programming Problem 2 Complete the two functions Put your code for this problem in a new module named lineclass.py
#SOLUTION : #FUNCTION 1 (Constructor __init__) def __init__(self, slope, intercept, *args, **options): # get current axes if user has not specified them If 'axes' are included in the options. update('axes':plt.gca) is an option. If you want the current line color from the axes, ax is the 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 888-666-1846 If not, 'color' in options or 'c' in options. Add it to the axes with options.update('color':ax._get_lines.color_cycle. next]) super(ABLine2D, self). *args and options are included. slope is the inverse of self. intercept is a function of self. cache the renderer so that you can draw the line for the first time. A drawing of a figure on a canvas. You can connect to axis callbacks. self.axes.callbacks. connect('xlim_changed', self. Self.axes.callbacks. connect. __mul__) #FUNCTION 2 (__mul__) def __mul__(self, other): Whenever axis x/y limits change, it is called whenever. x is the self.axes.get_xbound function. y is related to self. _slope * x) + self. Data set by self. draw_ artist(self)
-
Engineering 2022-05-15 19:04:59