"""Literal Python conversion of fig8_19.m."""

import matplotlib.pyplot as plt
import numpy as np


def fig8_19():
    #  generate data for regression
    nd = 50
    a = 4; b = 2
    rdd = 0.2 * (0.5 - np.random.rand(nd))
    thetad = 2 * np.pi * np.random.rand(nd)
    xd = np.zeros(nd)
    yd = np.zeros(nd)
    for i in range(nd):
        xd[i] = (1 + rdd[i]) * a * np.cos(thetad[i])
        yd[i] = (1 + rdd[i]) * b * np.sin(thetad[i])

    r = np.linalg.norm(3 + rdd, 1) / nd
    #  evaluate interpolation fn's for use in plotting
    #  N = number of points plotted
    n = 300
    theta = np.linspace(0, 2 * np.pi, n)
    x = np.zeros(n)
    y = np.zeros(n)
    for ix in range(n):
        x[ix] = 4 * np.cos(theta[ix])
        y[ix] = 2 * np.sin(theta[ix])

    plt.close("all")
    # get(gcf)
    # MATLAB Position [23 974 430 371], scaled to fit the plot window.
    fig, ax = plt.subplots(figsize=(4.3, 4.0), 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],
    ])
    ax.set_prop_cycle(color=co)

    ax.plot(x, y, "r", linewidth=1.6)
    ax.plot(xd, yd, ".b", markersize=22)
    ax.axis([-5, 5, -3, 3])
    ax.set_xticks([-5, 0, 5])
    ax.set_yticks([-3, 0, 3])
    ax.grid(True)
    ax.set_frame_on(True)
    ax.set_xlabel("x-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")

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


if __name__ == "__main__":
    fig8_19()
