var('x')
x1 = 1
x2 = 1.2
def f(x):
return x^2
y1 = f(x1)
y2 = f(x2)
slope = (y2-y1)/(x2-x1)
p = plot(f(x), x, 0, 4)
pt1 = point((x1, y1), rgbcolor=(1,0,0), pointsize=30)
pt2 = point((x2, y2), rgbcolor=(1,0,0), pointsize=30)
d = 3
l = line([(x1-d, y1-d*slope), (x2+d, y2+d*slope)], rgbcolor=(1,0,0))
t1 = text("P(%s, %s)" % (float(x1), float(y1)), (x1+0.5, y1-0.8), rgbcolor=(1,0,0))
t2 = text("Q(%s, %s)" % (float(x2), float(y2)), (x2+0.5, y2-0.8), rgbcolor=(1,0,0))
t3 = text("slope: %s" % float(slope), ((x1+x2)/2-0.5, (y1+y2)/2), rgbcolor=(1,0,0))
g = p+pt1+pt2+l+t1+t2+t3
g.show(xmin=0, xmax=4, ymin=-1, ymax=20)