#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Programme de test pour matplotlib. Si tout se passe bien, le programme doit
montrer un graphique avec plusieurs courbes et sauver le résultat dans une
image appelée test.png dans le répertoire courant."""
import math
import matplotlib.pyplot as plt


def main():
    """La partie principale du programme."""
    xmax = 100
    xrange = range(0, xmax)

    print("Courbes de fonctions usuelles")
    if type(xrange) == list:
        raise Exception('Vous devez tester ce programme en python 3!')

    functions = {
        "n":         (lambda x: x),
        "n*n":       (lambda x: x*x),
        "racine(n)": (lambda x: math.sqrt(x)),
        "ln(n)":     (lambda x: math.log(x+.0001)),
        "n*ln(n)":   (lambda x: x*math.log(x+.0001))
    }

    legend = []
    for lgd, fonction in functions.items():
        legend.append(
            plt.plot(xrange, [fonction(x) for x in xrange], label=lgd)[0]
        )

    plt.axis([0, xmax, 0, 2*xmax])
    plt.title("test")
    plt.legend(handles=legend)
    plt.xlabel("n")
    plt.ylabel("f(n)")
    plt.savefig("test.png")
    plt.show()


if __name__ == "__main__":
    main()
