you can probably replace these lines with something like np.ogrid/np.mgrid. They are made to create grids like this, so it's probably worth having a look into.