0

I have this code in python for generating x, y, z positions in an unitary sphere. The problem is that in the output file they are not arranged in separate columns like x y z.

from numpy import random, cos, sin, sqrt, pi 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 

def rand_sphere(n):  
    z = 2 * random.rand(n) - 1   # uniform in -1, 1 
    t = 2 * pi * random.rand(n)   # uniform in 0, 2*pi 
    x = sqrt(1 - z**2) * cos(t) 
    y = sqrt(1 - z**2) * sin(t)   
    return x, y, z

x, y, z = rand_sphere(200)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')  
ax.scatter(x, y, z)
plt.savefig('sphere.png')
#plt.show()

Outfile=open('output.txt','w') 
Outfile.write('This line will be text\n')  
Outfile.write('\n') 
Outfile.write(repr(rand_sphere(200))) 
Outfile.close() 

The other problem is that before the x y z columns I need to repeat m=10 for each line;

Means that each line in the output file must be as following (without comma between them):

This line will be text
m    x     y     z
20  ...   ...   ... 
.
.
.
(200 lines) 

So, I want to have three separate position columns plus one m=10 column.

dove
  • 37
  • 6
  • 5
    Your question is really about how to represent a tuple without showing commas. Most of the text in your question is irrelevant. Write a small script to try and get just this issue working, and then integrate into your main code. See how to create a [mcve]. – Peter Wood Dec 13 '18 at 12:06
  • Possible duplicate of [How to transform a tuple to a string of values without comma and parentheses](https://stackoverflow.com/questions/17426386/how-to-transform-a-tuple-to-a-string-of-values-without-comma-and-parentheses) – Peter Wood Dec 13 '18 at 12:06
  • Please first read the question carefully. One problem is to separate them because when I run the code in the output the numbers are uninterrupted in a line not in three columns as I explained well above!!! – dove Dec 13 '18 at 12:17

2 Answers2

1

This will give you a nice aligned output.

from numpy import random, cos, sin, sqrt, pi 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 

def rand_sphere(n):  
    z = 2 * random.rand(n) - 1   # uniform in -1, 1 
    t = 2 * pi * random.rand(n)   # uniform in 0, 2*pi 
    x = sqrt(1 - z**2) * cos(t) 
    y = sqrt(1 - z**2) * sin(t)   
    return x, y, z

def col_align(x, y, z):
    data = '{:>3} {:>6} {:>6} {:>6}\n'.format('m', 'x', 'y', 'z')
    for i in range(len(x)):
        data += '{: 3d} {: 05.3f} {: 05.3f} {: 05.3f}\n'.format(10, x[i], y[i], z[i]) 
    return data

x, y, z = rand_sphere(200)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')  
ax.scatter(x, y, z)
plt.savefig('sphere.png')
plt.show()

Outfile=open('output.txt','w') 
Outfile.write('This line will be text\n')  
Outfile.write('\n')
Outfile.write(col_align(x, y, z)) 
Outfile.close()

I just used your script and working a bit with the format output before writing the file. Have a look here https://pyformat.info/ for the details about the format string method, in case you wish to can adjust the floating point precision and spaces to suit your needs.

Just for reference, here are the first lines of my output.txt file:

This line will be text

  m      x      y      z
 10  0.554 -0.826  0.105
 10 -0.501 -0.816 -0.287
 10 -0.774 -0.515 -0.368
 10 -0.537  0.672 -0.510
 10  0.869  0.291  0.401
 10 -0.511  0.806  0.299
 10  0.488 -0.770 -0.412
Valentino
  • 7,291
  • 6
  • 18
  • 34
-1

here you go

x, y, z = rand_sphere(200)
m = 10
# creating lists and storing them in res
# each list contains the elements of a line
res = [ [m, x[i], y[i], z[i]] for i in range(len(x)) ]
data = "This line will be text"
# concatenating line elements 
for elem in res:
    data = "{}\n{}".format( data, ','.join(str(num) for num in elem) ) 

with open(my_file, 'w') as file:
    file.write(data)
Mohamed Benkedadra
  • 1,964
  • 3
  • 21
  • 48