import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler
from scipy.interpolate import CubicSpline

from splineA import splineA


def fig5_22():
    # fit cubic splines to the four data sets

    # ysM = MATLAB spline function
    # ys = natural spline (requires splineA.m file)

    ##### generate the data sets
    a = -1
    b = 1
    nx = 15
    xd = np.linspace(a, b, nx)

    yd = np.zeros(nx)
    yd[6] = 1

    #### evaluate and then plot spline function
    # ysM = MATLAB spline function
    # ys = natural spline (requires splineA.m file)

    n = 800
    xp = np.linspace(a, b, n)

    ys = splineA(xd, yd, xp)
    ysM = CubicSpline(xd, yd)(xp)
    # ysf = splineA(xd,yd,xp, [1 1],[0 0]);

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(figsize=(9.0, 3.2))
    fig.subplots_adjust(left=0.08, right=0.98, bottom=0.22, top=0.98)
    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(xd, yd, "or", markersize=8, linewidth=2)
    ax.plot(xp, ys, "b", linewidth=1.6)
    ax.set_xlim(-1, 1)
    ax.set_ylim(-0.2, 1.05)
    ax.set_yticks([0, 1])
    ax.set_xticks([-1, -0.5, 0, 0.5, 1])
    ax.grid(True)
    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    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/fund.eps')


if __name__ == "__main__":
    fig5_22()
