Create list of x,y,z coordinates using loop in Python

Viewed 1113

I would like to create 3-dimensional array including coordinates of the points in cartesian axis system. My user input is the following:

import pandas as pd
import numpy as np

rows = int(input("Please Enter the Total Number of Rows  : "))
columns = int(input("Please Enter the Total Number of Columns  : "))
levels = int(input("Please Enter the Total Number of Levels  : "))

dx = int(input("Please Enter the Pitch in x Direction  : "))
dy = int(input("Please Enter the Pitch in y Direction  : "))
dz = int(input("Please Enter the Pitch in z Direction  : "))

x0 = int(input("Please Enter the Origin Coordinate in x Direction  : "))
y0 = int(input("Please Enter the Origin Coordinate in y Direction  : "))
z0 = int(input("Please Enter the Origin Coordinate in z Direction  : "))

I want to start at point with coordinates [x0,y0,z0] and then create the 3D array based on the inserted number of rows,columns, levels (3rd dimension) and spacing between the points (dx,dy,dz)

I am think about the loop

Data=np.zeros(('rows','columns','levels'))
for i in range(rows):
    for j in range(columns):
        for k in range(levels):

but no idea how to write this code. Can anyone help? Output should be in form of 3D Array.

Many Thanks Lukas

1 Answers

Set x, y, z as vectors

x = x0 + np.arange(rows)    * dx
y = y0 + np.arange(columns) * dy
z = z0 + np.arange(levels)  * dz

Now make 3x arrays of shape (rows, columns, levels) with np.meshgrid

X, Y, Z = np.meshgrid(x, y, z)

Now you can make your scalar field just using those variables

Data = X**2 + Y**2 + Z**2  # for example

Your Data will now be a (rows, columns, levels)-shaped array, with dimensional axes corresponding to x, y, z

Related