Thank you for sharing the code.
Which software you are using? I mean Mode or FDTD to generate mode profile? Which monitor are you using to generate mode profile?
Have you tried visualizing mode data in Lumerical software.
eg: I am using Eigen solver to generate mode profile of waveguide.

You can refer the following to extract the data: using getdata to grab mode profiles (ansys.com)
Thank you