from math import pi
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


def xc(a, om1, phi1, t):
    return a*np.cos(om1*t + phi1)

def yc(b, om2, phi2, t):
    return b*np.cos(om2*t + phi2)

a = np.array([1., 1., 1., 1., 1.])
b = np.array([1., 1., 1., 1., 1.])
om1 = np.array([1., 1., 1., 1., 2.])
om2 = np.array([1., 2., 3., 4., 3.])
phi1 = np.array([0., 0., 0., 0., 0.])
phi2 = np.array([0., np.pi/4, np.pi/2, 3*np.pi/4, np.pi])
xlabs = ['0', r'$\pi/4$', r'$\pi/2$', r'$3\pi/4$', r'$\pi$']
ylabs = ['1', '1/2', '1/3', '1/4', '2/3']
T = 360
n_steps = 360
t = np.linspace(0. ,T , n_steps)/T*2*pi
d = 1.2*max(max(a),max(b))


nx = 5
ny = 5

fig = plt.figure(constrained_layout=False, figsize=(6.,6.))
gs = fig.add_gridspec(nx, ny, hspace=0, wspace=0)
#(ax1, ax2, ax3, ax4, ax5), (ax6, ax7, ax8, ax9, ax10), (ax11, ax12, ax13, ax14, ax15), (ax16, ax17, ax18, ax19, ax20), (ax21, ax22, ax23, ax24, ax25) =  gs.subplots(sharex='True', sharey='True')
for row in range(nx):
    for col in range(ny):
        #print(r'col = {}, row = {}, $\omega_1/\omega_2$ = {}, $\phi_1 - \phi_2 =$ {}'.format(col, row, om1[col]/om2[row], (phi1[col]-phi2[row])/np.pi*180))
        #plt.gca().set_aspect('equal', adjustable='box')
        ax = fig.add_subplot(gs[row,col])
        #ax.set_box_aspect(1)
        ax.set_xlim(-d, d)
        ax.set_ylim(-d, d)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_xticklabels([])
        ax.set_yticklabels([])
#        ax.plot(xc(a[row], om1[row], phi1[row], t), yc(b[col], om2[col], phi2[col], t), 'k-')
        ax.plot(xc(a[col], om1[row], phi1[col], t), yc(b[row], om2[row], phi2[col], t), 'k-')
        if col == 0:
            ax.set_ylabel(ylabs[row])
        if row == ny-1:
            ax.set_xlabel(xlabs[col])
        #print(col, row, ':', a[row], om1[row], t)
        #plt.gca().set_aspect('equal', adjustable='box')
#ax.set_xlabel('x',fontsize=14)
#ax.set_ylabel('y',fontsize=14)

all_axes = fig.get_axes()
for ax in all_axes:
    for sp in ax.spines.values():
        sp.set_visible(False)
    if ax.is_first_row():
        ax.spines['top'].set_visible(True)
    if ax.is_last_row():
        ax.spines['bottom'].set_visible(True)
    if ax.is_first_col():
        ax.spines['left'].set_visible(True)
    if ax.is_last_col():
        ax.spines['right'].set_visible(True)

#plt.gca().set_aspect('equal', adjustable='box')
fig.text(0.06, 0.5, r"$\omega_x/\omega_y$", rotation="vertical", va="center", fontsize=14)
fig.text(0.45, 0.05, r"$|\phi_x - \phi_y|$", rotation="horizontal", va="center", fontsize=14)
plt.savefig('lissajous-multi.eps',bbox_inches='tight')
plt.show()
