import numpy as np
import matplotlib.pyplot as plt


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

    # compute the values of the ratio
    n = 10000
    exs = np.linspace(-18, -2, n)
    k = np.empty(n)
    y = np.empty(n)
    j = 0
    for i in range(n):
        k[i] = 10**exs[i]
        y[i] = (np.sqrt(16 + k[i]) - 4) / k[i]
        # determine when y=0
        if y[i] == 0:
            j = i

    # k value where y=0 starts
    print(k[j])

    plt.close('all')
    # get(gcf)
    # MATLAB Position [25 1115 658 230]: preserve the 658 x 230 pixel size.
    fig, ax = plt.subplots(figsize=(6.58, 2.30), 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)

    # plot the results
    ax.semilogx(k, y, 'b-', linewidth=1.2)
    ax.axis([1e-18, 1e-2, -0.02, 0.1875])
    ax.set_yticks([0, 0.0625, 0.1250, 0.1875])
    ax.set_yticklabels(['0 ', '1/16 ', '1/8 ', '3/16 '])
    ax.set_xticks([1e-18, 1e-14, 1e-10, 1e-6, 1e-2])
    ax.grid(True)
    ax.set_xlabel('k-axis', fontsize=12, fontweight='bold')
    ax.set_ylabel('y-axis', fontsize=12, fontweight='bold')
    ax.tick_params(labelsize=12)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight('bold')

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

    plt.show()


if __name__ == '__main__':
    fig1_2()
