import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler


def fig2_16():
    xa = 1.5
    tol = 10 ** (-18)

    e0 = 1e-1
    e1 = np.zeros(20)
    e2 = np.zeros(20)
    e3 = np.zeros(20)
    e4 = np.zeros(20)
    e5 = np.zeros(20)
    iter = np.zeros(20, dtype=int)

    e1[0] = e0
    e2[0] = e0
    e3[0] = e0
    e4[0] = e0
    e5[0] = e0
    iter[0] = 1
    for i in range(1, 20):
        iter[i] = i + 1
        e1[i] = e1[i - 1] / 2
        e2[i] = e2[i - 1] / 5
        e3[i] = e3[i - 1] ** 1.5
        e4[i] = e4[i - 1] ** 2
        e5[i] = e5[i - 1] ** 2.5

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(
        figsize=(658 / 100, 230 / 100), dpi=100, constrained_layout=True
    )
    co = [
        (0, 0, 1),
        (0, 0.5, 0),
        (1, 0, 0),
        (0, 0.75, 0.75),
        (0.75, 0, 0.75),
        (0.75, 0.75, 0),
        (0.25, 0.25, 0.25),
    ]
    plt.rcParams["axes.prop_cycle"] = cycler(color=co)

    # plot error
    ax.semilogy(iter, e1, "om", markersize=8, linewidth=2)
    ax.semilogy(iter, e1, "--m", markersize=8, linewidth=1.2)
    ax.semilogy(iter, e2, "+", color=(0, 0.5, 0), markersize=8, linewidth=2)
    ax.semilogy(iter, e2, "--", color=(0, 0.5, 0), markersize=8, linewidth=1.2)
    ax.semilogy(iter, e3, "--r", markersize=8, linewidth=1.2)
    ax.semilogy(iter, e3, "*r", markersize=8, linewidth=2)
    ax.semilogy(iter, e4, "--b", markersize=8, linewidth=1.2)
    ax.semilogy(iter, e4, ">b", markersize=8, linewidth=2)
    ax.semilogy(iter, e5, "--k", markersize=8, linewidth=1.2)
    ax.semilogy(iter, e5, "sk", markersize=8, linewidth=2)

    # ax.yaxis.grid(True, which="minor")
    ax.axis([1, 10, 1e-16, 1e-1])
    ax.set_yticks([1e-16, 1e-13, 1e-10, 1e-7, 1e-4, 1e-1])

    # add in axes
    ax.set_xlabel("Iteration Step")
    ax.set_ylabel("Error")
    ax.grid(True)
    ax.set_axisbelow(True)
    for spine in ax.spines.values():
        spine.set_visible(True)
    ax.tick_params(labelsize=12)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    ax.xaxis.label.set_fontsize(12)
    ax.yaxis.label.set_fontsize(12)
    ax.xaxis.label.set_fontweight("bold")
    ax.yaxis.label.set_fontweight("bold")

    # fig.savefig('/Users/mark/Desktop/exer4.eps')
    plt.show()


if __name__ == "__main__":
    fig2_16()
