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


def fig5_14():
    # piecewise linear interpolation
    # note that this uses the fact that MATLAB draws straight lines betwen data
    # points when plotting

    a = 0
    b = 1
    nx = 6
    print(nx)
    xd = np.linspace(a, b, nx)

    # data
    yd = np.zeros(nx)
    for iy in range(nx):
        yd[iy] = np.cos(2 * np.pi * xd[iy])

    # exact
    n = 400
    xp = np.linspace(a, b, n)
    y = np.zeros(n)
    for i in range(n):
        y[i] = np.cos(2 * np.pi * xp[i])

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(figsize=(9.0, 3.2))
    fig.subplots_adjust(left=0.09, right=0.97, bottom=0.21, top=0.97)
    co = np.array([
        [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],
    ])
    ax.set_prop_cycle(cycler(color=co))

    ax.plot(xp, y, "--r", linewidth=1.8)
    ax.plot(xd, yd, "-b", linewidth=1.6)
    ax.plot(xd, yd, "or", markersize=7, linewidth=2)
    ax.grid(True)
    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    ax.legend(
        [" Exact", " P-Linear"],
        loc="upper center",
        prop={"size": 14, "weight": "bold"},
    )
    ax.tick_params(labelsize=14)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    for spine in ax.spines.values():
        spine.set_visible(True)
    plt.show()

    # plt.savefig('/Users/mark/Desktop/pcos.eps')


if __name__ == "__main__":
    fig5_14()
