import matplotlib.pyplot as plt
import numpy as np

def affiche(f, a, b,x0=None,x1=None,fx0=None, fx1=None):
    if b>a: a,b = b,a
    d = b-a
    marge = d*0.05
    xs = np.linspace(a-marge, b+marge, 100)
    ys = [f(x) for x in xs]
    plt.plot(xs, ys)
    plt.plot([a,a],[0,f(a)],color='black')
    plt.plot([b,b],[0,f(b)],color='black')
    c = 0.5 * (a+b)
    plt.plot([c,c],[0,f(c)])
    plt.plot([a,b],[0,0], color='black')
    if x0 is not None and x1 is not None and x0 != x1:
        if fx0 is None: fx0 = f(x0)
        if fx1 is None: fx1 = f(x1)
        if x0>x1: x0,x1,fx0,fx1 = x1,x0,fx1,fx0
        d = fx0-fx1
        p = d / (x0-x1)
        plt.plot([x0,x0],[0,fx0],color='red')
        plt.plot([x1,x1],[0,fx1],color='red')
        plt.plot([a,b],[fx0+(a-x0)*p,fx0+(b-x0)*p])
    plt.show()
