Search code examples
pythonmatplotlibplotmatplotlib-3d

3D rotated circle in matplotlib


My ploted circle with n-generated points looks completely fine in 2D:

Generated circle in 2D (x,y)

However when i tried to plot the circle with half of the points increasing z value and half of them decreasing z value it looks like this:

xyz circle

xz view

I want to make it tilted to the xy plane.

Any idea how to make the circle more circle-like?

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()

I want my "circle" to be a real circle - with correct increasing and decreasing z values...


Solution

  • The bug is that the z coordinate for the points will not be linearly dependent on the iteration count, but rather it will be linearly dependent on the x (or y) coordinate

    If you remove the z calculation from the function PointsInCircum , and then make the following changes

    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])