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


def fig2_20():
    a = -2
    b = 2
    nx = 100
    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, linewidth=1.8)
    ax.plot([a, b], [0, 0], "k", linewidth=1)
    ax.axis([a, b, -1.1, 1.1])

    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    ax.grid(True)
    for spine in ax.spines.values():
        spine.set_visible(True)
    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/tanh.eps')
    plt.show()


def f(x):
    # g=x^3+2*x+2;
    g = (1 - math.exp(-10 * x)) / (1 + math.exp(-10 * x))
    # g=1-4*x*exp(-2*x*x);
    return g


if __name__ == "__main__":
    fig2_20()
