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

from splineA import splineA


def fig2_14():
    a = -3
    b = 3
    nx = 200
    x = np.linspace(a, b, nx)
    y = np.zeros(nx)
    for ix in range(nx):
        y[ix] = f(x[ix])

    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)

    ax.plot(x, y, "b", linewidth=2)
    ax.plot([a, b], [0, 0], "-k", linewidth=0.5)
    for spine in ax.spines.values():
        spine.set_visible(True)
    ax.grid(True)
    ax.axis([a, b, -1.8, 3.5])
    ax.set_yticks([-10, 0, 10])
    ax.set_xlabel("x-axis")
    ax.set_ylabel("f(x)")
    ax.tick_params(labelsize=14)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    ax.xaxis.label.set_fontsize(14)
    ax.yaxis.label.set_fontsize(14)
    ax.xaxis.label.set_fontweight("bold")
    ax.yaxis.label.set_fontweight("bold")

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


def f(x):
    # g=x^3-x^2+3;
    # g=cos(pi*x+0.1*exp(-x/3)-0.2*pi)-x/4;
    xd = [-3, -2.1, -0.8, 0.2, 1.3, 1.5, 2.2, 3]
    yd = [-1, 3, -1, 1.5, -0.95, 0, 2, -1.5]
    # xd=[-3 -2.1 0.8  2.2   3];
    # yd=[-1  3    -1   1.8  -1.5];
    g = splineA(xd, yd, x)
    return g


if __name__ == "__main__":
    fig2_14()
