matplotlib中的3D旋转圆

k4aesqcs  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(174)

我绘制的具有n个生成点的圆在2D中看起来完全正常:

然而,当我试图绘制一个圆,其中一半的点增加z值,另一半的点减少z值时,它看起来像这样:

我想让它倾斜到xy平面。
你知道如何让这个圆更像圆吗?

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

pi = math.pi

x = []
y= []
z=[]

def PointsInCircum(r,n=1000):
    for j in range(-1,int(n/2)):
        z.append(j)
    for j in range(int(n/2),0,-1):
        z.append(j)
    return [(math.cos(2*pi/n*x)*r,math.sin(2*pi/n*x)*r) for x in range(0,n+1)]
    

xy = (PointsInCircum(100))
ax = plt.axes(projection ='3d')

for j in range(len(xy)):
    curr = xy[j]
    x.append(curr[0])
    y.append(curr[1])

print(len(x))
print(len(y))
print(len(z))

ax.scatter(x, y, z, 'green')

plt.show()

我希望我的“圆”是一个真实的的圆-具有正确的递增和递减z值。..

epggiuax

epggiuax1#

缺陷在于点的z坐标不会线性依赖于迭代次数,而是线性依赖于x(或y)坐标
如果从函数PointsInCircum中删除z计算,然后进行以下更改

m = 1 #assuming you want it to be inclined at 45deg
z = []
for j in range(len(xy)):
    curr = xy[j]
    x.append(curr[0])
    y.append(curr[1])
    z.append(m*curr[0])

相关问题