In: Computer Science
1) Using matplotlib, graph the line of slope -2 going through the point (1,1).
2) Instead of graphing the previous line, plot 5 evenly-spaced points on that line in the interval [0,1] (including 0 and 1).
ANS 1:
CODE:
#ANS :- 1
# the below two lines are required packeges to plotting lines and points on that line.
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1,5) #linspace() is one of the important method of numpy and used to divide points as per given Number/data.
y = -2*x + 3 #Here slope m=-2 and find the intercept value c = 3 by putting the value (1, 1) since line passing through this point(1, 1)
plt.plot(x,y, '-r',label="line y=-2*x + c") #this is used to plot line in matplotlib packages.
plt.title("line plot with slope = -2 passing through (1,1)") #Used to set title of the graph in matplotlib
plt.show() #Used to show() the ON CONSOLE plotting.
ANS 2:
CODE:
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1,5)
y = -2*x + 3
plt.plot(x,y, '-r+')
plt.title("five evenly spaced point on the line")
plt.show()