# P-1 MCS 572 due Fri 27 Sep 2024 : matrixplot.py
"""
Plots the basins of attraction of the method of Muller,
given the matrix of integers on file separated by commas.
"""
import numpy as np

def matrixplot(A):
    """
    Uses matshow to make a plot of the matrix A.
    """
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import matshow
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.matshow(A, cmap='Paired')
    plt.show()

def main():
    """
    Reads the matrix from file and makes the plot.
    """
    name = input('Give the name of the input file : ')
    file = open(name, 'r')
    line = file.readline()
    dim = int(line)
    print('the dimension :', dim)
    mat = np.zeros((dim, dim), dtype=int)
    for row in range(dim):
        line = file.readline()
        nonewline = line[:-1] 
        nbrs = nonewline.split(',')
        for col in range(dim):
            mat[row,col] = int(nbrs[col])
    matrixplot(mat)

if __name__ == "__main__":
    main()
