Home > Software engineering >  How to do a 3D circle in Matplotlib around a rotation axis
How to do a 3D circle in Matplotlib around a rotation axis

Time:12-23

I'm trying to plot circles using matplotlib that were I can give a rotation axis. I already found this post here on stack overflow where someone provided a code for rotating tangential to the z-axes. How can I move this to rotate around the origin? At the moment, my code is looking like this:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

def circ_3d(num_proj, start_rotation_axis, deg_list = [0,45,90]):

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    theta = np.linspace(0, 2 * np.pi, num_proj)
    
    if start_rotation_axis == 'z':
       x = np.cos(theta)
       y = np.sin(theta)
       for i in range(len(deg_list)):
          degree = deg_list[i]
          phi = np.deg2rad(degree)
          ax.plot(x ,
                y, x*np.sin(phi)  )
    else:
        raise Exception("Enter a valid axis")

    ax.plot((0,0),(0,0), (-radius,radius), '-k', label='z-axis', color = 'green')
    ax.plot((-radius,radius),(0,0), (0,0), '-k', label='x-axis', color = 'cyan')
    ax.plot((0,0),(-radius,radius), (0,0), '-k', label='y-axis', color = 'red')
    ax.legend()
    plt.show()

By calling circ_3d(200,'z'), I get this picture. Can someone help me to understand where the shift is coming from and how to avoid it?

I updated the code and get now the following.

So the remaining question is: given a circle in the x-y plane (degree = 0), how can I tilt the next circle over 45 degree (x-y plane to z-axes), such that the center of the circle is still (0,0,0) and it does not evolve to an ellipse.

Thanks in advance!

CodePudding user response:

Finally, I managed to find the answer by myself. For anyone who is also interested in it, i will post it here:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

def circ_3d(radius,num_proj, start_rotation_axis, cycle_list = [0]):

  fig = plt.figure()
  ax = fig.add_subplot(111, projection='3d')

  theta = np.linspace(0, 2 * np.pi, num_proj)
  degree_step = 360/len(cycle_list)
  if start_rotation_axis == 'x':
      y = np.cos(theta)
      z = np.sin(theta)
      for i in range(len(cycle_list)):
          degree = cycle_list[i]
          phi = np.deg2rad(degree  90)
          ax.plot(y*np.cos(phi),
                  y * np.sin(phi) , z , label = degree  )
  elif start_rotation_axis == 'y':
      x = np.cos(theta)
      z = np.sin(theta)
      for i in range(len(cycle_list)):
          degree = cycle_list[i]
          phi = np.deg2rad(degree  90)
          ax.plot(x * np.sin(phi), x * np.cos(phi)
                   , z, label = degree  )
  elif start_rotation_axis == 'z':
      x = np.cos(theta)
      y = np.sin(theta)
      for i in range(len(cycle_list)):
          degree = cycle_list[i]
          phi = np.deg2rad(degree   90)
          ax.plot(x,y * np.sin(phi) ,
                   y*np.cos(phi), label = degree  )
  else:
      raise Exception("Enter a valid axis")

  ax.plot((0,0),(0,0), (-radius,radius), '-k', label='z-axis',   color = 'green')
  ax.plot((-radius,radius),(0,0), (0,0), '-k', label='x-axis', color = 'cyan')
  ax.plot((0,0),(-radius,radius), (0,0), '-k', label='y-axis', color = 'red')
  ax.legend()
  plt.show()

circ_3d(5,200,'x',[0,45,90])

By running this script, you will get this plot.

  • Related