import numpy as np
import matplotlib.pyplot as plt


def fig1_1():
    """Literal Python conversion of fig1_1.m."""

    ### this requires the subaxis.m and parseArgs.m files
    # Matplotlib's constrained layout replaces subaxis.m and parseArgs.m.

    plt.close('all')
    # get(gcf)
    # MATLAB Position [25 930 660 420]: preserve the 660 x 420 pixel size.
    fig, axes = plt.subplots(2, 1, figsize=(6.60, 4.20), dpi=100,
                             layout='constrained')
    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]
    ])
    plt.rcParams['axes.prop_cycle'] = plt.cycler(color=co)

    #  generate and then plot upper graph
    ax = axes[0]
    d = 0.1
    nx = 1000
    x = np.linspace(1 - d, 1 + d, nx)
    y = np.empty(nx)
    yy = np.empty(nx)
    for i in range(nx):
        y[i] = (x[i]**8 - 8*x[i]**7 + 28*x[i]**6 - 56*x[i]**5
                + 70*x[i]**4 - 56*x[i]**3 + 28*x[i]**2 - 8*x[i] + 1)
        yy[i] = (x[i] - 1)**8
    ax.plot(x, yy, '--b', linewidth=2)
    ax.plot(x, y, 'r', linewidth=1.2)
    ax.grid(True)
    ax.set_frame_on(True)
    ax.axis([1 - d, 1 + d, -1e-9, 1e-8])
    ax.set_xticks([0.9, 0.95, 1, 1.05, 1.1])
    # xlabel('x-axis')
    ax.set_ylabel('y-axis', fontsize=14, fontweight='bold')
    ax.tick_params(labelsize=14)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight('bold')
    legend = ax.legend([' Using (1.3)', ' Using (1.4)'], loc='upper center',
                       fontsize=14)
    for text in legend.get_texts():
        text.set_fontweight('bold')

    #  generate and then plot lower graph
    ax = axes[1]
    d = 0.02
    x = np.linspace(1 - d, 1 + d, nx)
    y = np.empty(nx)
    yy = np.empty(nx)
    for i in range(nx):
        y[i] = (x[i]**8 - 8*x[i]**7 + 28*x[i]**6 - 56*x[i]**5
                + 70*x[i]**4 - 56*x[i]**3 + 28*x[i]**2 - 8*x[i] + 1)
        yy[i] = (x[i] - 1)**8
    ax.plot(x, y, 'r', linewidth=1)
    ax.plot(x, yy, '--b', linewidth=2.5)
    ax.grid(True)
    ax.set_frame_on(True)
    ax.axis([1 - d, 1 + d, -2e-14, 4e-14])
    ax.set_xticks([0.98, 0.99, 1, 1.01, 1.02])
    ax.set_xlabel('x-axis', fontsize=14, fontweight='bold')
    ax.set_ylabel('y-axis', fontsize=14, fontweight='bold')
    ax.tick_params(labelsize=14)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight('bold')

    # exportgraphics(gcf,'/Users/mark/Desktop/power.eps')

    plt.show()


if __name__ == '__main__':
    fig1_1()
