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


def fig2_3():
    a = -1.5
    b = 2.5
    nx = 100
    x = np.linspace(a, b, nx)
    y = np.zeros(nx)
    ik = 0
    for ix in range(nx):
        y[ix] = f(x[ix])
        if y[ix] < 0:
            ik = 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=1.4)
    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, -2.5, 4])
    ax.set_yticks([-2, 0, 2, 4])
    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/cubic.eps')
    plt.show()


def f(x):
    g = (x - 0.1) * (x - 2.1) * (x + 1.1)
    return g


if __name__ == "__main__":
    fig2_3()
