# Generate xyz coordinates of a tube made of multiple rings

In this code, I generate the coordinates of a tube made of multiple rings. Finally, I write those x,y,z coordinates in a .csv file. I want to simplify this code (maybe using a for loop?).

import numpy as np
import matplotlib.pyplot as plt

bLength=1.6
numPoints=10
radius = bLength*numPoints / (2 * np.pi)
theta = np.linspace(0,2*np.pi,numPoints,endpoint=False)
dtheta=theta[1]-theta[0]

#creating the x, y coordinates of the first ring
#creating the x, y coordinates of the second ring
#above first ring and second ring will come alternatively to form a tube.
#plt.plot(x0,y0)
#plt.show()
#plt.plot(x1,y1)
#plt.show()
#now generating the z-coordinate of the first ring, second ring, third ring, etc.
cons0=np.ones(x0.shape)*0
cons1=np.ones(x1.shape)*2
cons2=np.ones(x0.shape)*4
cons3=np.ones(x1.shape)*6
cons4=np.ones(x0.shape)*8
cons5=np.ones(x1.shape)*10
cons6=np.ones(x0.shape)*12
cons7=np.ones(x1.shape)*14
cons8=np.ones(x0.shape)*16
cons9=np.ones(x1.shape)*18

###Now saving the x, y, z coordinates in csv files.

np.savetxt('cooRing00.csv',np.c_[x0,y0,cons0],delimiter=' ',fmt='%10f')
np.savetxt('cooRing01.csv',np.c_[x1,y1,cons1],delimiter=' ',fmt='%10f')
np.savetxt('cooRing02.csv',np.c_[x0,y0,cons2],delimiter=' ',fmt='%10f')
np.savetxt('cooRing03.csv',np.c_[x1,y1,cons3],delimiter=' ',fmt='%10f')
np.savetxt('cooRing04.csv',np.c_[x0,y0,cons4],delimiter=' ',fmt='%10f')
np.savetxt('cooRing05.csv',np.c_[x1,y1,cons5],delimiter=' ',fmt='%10f')
np.savetxt('cooRing06.csv',np.c_[x0,y0,cons6],delimiter=' ',fmt='%10f')
np.savetxt('cooRing07.csv',np.c_[x1,y1,cons7],delimiter=' ',fmt='%10f')
np.savetxt('cooRing08.csv',np.c_[x0,y0,cons8],delimiter=' ',fmt='%10f')
np.savetxt('cooRing09.csv',np.c_[x1,y1,cons9],delimiter=' ',fmt='%10f')

###Now, I have the x, y, z coordinates in many files (based on the length of the tube I want.

• Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. – Sᴀᴍ Onᴇᴌᴀ Dec 6 '18 at 15:48
• Okay @SᴀᴍOnᴇᴌᴀ. – phenomenon Dec 6 '18 at 18:30

For the generation of your z-coordinate you can write something like:

def generate_z_coordinate(n, x0, x1):
"""Generate the z-coordinate for the n first ring.
:param n: the number of ring
:param x0: x0 coordinate
:param x1: x1 coordinate
:return: A list of the z-coordinate for the n rings
"""
tmp = 0
rings = []
for i in range(0,n):
if i % 2 == 0:
x = x0
else:
x = x1
rings.append(np.ones(x.shape) * tmp)
tmp += 2
return rings


I used tmp because I lack a better name, but there is probably a better way to name it.

And saving in the csv:

def save(filename, rings, x0, y0, x1, y1):
"""Save rings coordinate into filename
:param filename: Name of the file to save data in
:param rings: list of z-coordinate for the rings
:param x0: x0 coordinate
:param y0: y0 coordinate
:param x1: x1 coordinate
:param y1: y1 coordinate
"""
for i, elt in enumerate(rings):
name = filename + str(i) + '.csv'
if i % 2 == 0:
x = x0
y = y0
z = rings[i]
else:
x = x1
y = y1
z = rings[i]

np.savetxt(name, np.c_[x, y, z], delimiter=' ', fmt='%10f')


I am not sure it is more readable than your version but it's more scalable. Depend on what you need.

Also try to be consistant in your code and follow Python style guide(see PEP8):

• write bLength = 1.6 instead of bLength=1.6
• theta = np.linspace(0, 2 * np.pi, numPoints, endpoint=False)

You can use flake8 or black or PyLint to lint your code

• I fixed the functions, is it working for you know? – Julien Rousé Nov 29 '18 at 13:40
• I fixed the generation of the filename try it again. Your filename was cooRing00.csv etc my function is more like cooRing0.csv (without the trailing 0 before the number, maybe that trigger the error? I cant find if savetext need the file to already exist – Julien Rousé Dec 6 '18 at 13:40
• cooxyz.csv need to be a string, 'cooxyz.csv' and the function add csv automatically, try save("cooxyz", rings, x0, y0, x1, y1) – Julien Rousé Dec 6 '18 at 15:46
• I added the change name = filename + str(i) + '.csv'. Still I get following error: save(cooxyz, rings, x0, y0, x1, y1) NameError: name 'cooxyz' is not defined – phenomenon Dec 6 '18 at 16:10