Python 在csv文件中写入xyz坐标

Python 在csv文件中写入xyz坐标,python,csv,Python,Csv,我试图在csv文件中写入双圆的坐标(xyz坐标) 我现在已经编写了以下代码 import numpy as np import matplotlib.pyplot as plt import csv bLength=2.10e-10 numPoints=10 totalLength = bLength * numPoints Circumference = totalLength radius = Circumference / (2 * np.pi) totalAngle=360.0 ang

我试图在csv文件中写入双圆的坐标(xyz坐标)

我现在已经编写了以下代码

import numpy as np
import matplotlib.pyplot as plt
import csv

bLength=2.10e-10
numPoints=10
totalLength = bLength * numPoints
Circumference = totalLength
radius = Circumference / (2 * np.pi)
totalAngle=360.0
angle = 360.0/numPoints

# define a function frange to include fraction
def frange(start, stop, step):
    i = start
    while i < stop:
        yield i
        i += step
# getting the x, y co-ordinates of the two circles displaced by angle/2)    
for i in frange(0.0, 360.0, angle):
    plt.plot(radius * np.cos(i), radius * np.sin(i), 'bo')
    for j in frange(angle/2, 360.0, angle):
        plt.plot(radius * np.cos(j), radius * np.sin(j), 'bo')

plt.show() 

最终输出应包含三列。csv的前两个coumns将来自上面的“for循环”。csv文件的第三列应该是某个常量(例如0或2.1)。我如何才能做到这一点?

稍微清理一下数学和python,这应该可以:

import numpy as np
import matplotlib.pyplot as plt

bLength=2.10e-10
numPoints=10
radius = bLength*numPoints / (2 * np.pi)
theta = np.linspace(0,2*np.pi,numPoints,endpoint=False)
dtheta=theta[1]-theta[0]
x0,y0=np.cos(theta),np.sin(theta)
x1,y1=np.cos(theta+dtheta/2),np.sin(theta+dtheta/2)
plt.plot(x0,y0)
plt.plot(x1,y1)
cons=np.ones(x0.shape)*10
np.savetxt('circle.csv',np.c_[x0,y0,cons],delimiter=',')

linspace
函数中,我使用了
endpoint=False
来避免重复相同的值,但在绘图中这会使它看起来不连续。您可以复制它,在打包它们以编写时使用
np.c_x0[:-1],y0[:-1],cons[:-1]

附加到列表中,然后使用
np.array(list)
,然后使用
np.savetxt
编写数组。您可以使用numpy数组来简化代码。如果你知道半径:
t=np.arange(0,2*np.pi,粒度)
x,y=radius*np.cos(t),radius*np.sin(t)
你能告诉我们为什么有两个循环吗?外环生成一个圆,但内环为外环中的每个角度生成一个额外的圆!然而你提到你正在画一个“圆”。你想要一个或多个圆吗?@anishtain4我需要两个小距离分开的圆的坐标。第二个循环用于生成第二个循环(与第一个循环的距离很小)。@不,所有的东西都在同一个循环中。谢谢你的清晰代码。这只产生一半的坐标。图(A)和(c)中提供了我需要xyz坐标的双环模型:@现象:如你在问题中所提到的,这写的是“A”圆和常数的坐标。两个圆圈都要吗?只需将
savetxt
中的数组更改为
out\u array=np.r\ux0,y0,cons0],np.c\ux1,y1,cons1]
。根据需要更改代码应该很容易。
import numpy as np
import matplotlib.pyplot as plt

bLength=2.10e-10
numPoints=10
radius = bLength*numPoints / (2 * np.pi)
theta = np.linspace(0,2*np.pi,numPoints,endpoint=False)
dtheta=theta[1]-theta[0]
x0,y0=np.cos(theta),np.sin(theta)
x1,y1=np.cos(theta+dtheta/2),np.sin(theta+dtheta/2)
plt.plot(x0,y0)
plt.plot(x1,y1)
cons=np.ones(x0.shape)*10
np.savetxt('circle.csv',np.c_[x0,y0,cons],delimiter=',')