如何在Matplotlib中制作3D圆 [英] How to do a 3D circle in Matplotlib
问题描述
我是Python的初学者.我正在尝试使用与Z轴相切的matplotlib绘制一个圆.我知道如何在3D中绘制一个球体,但不知道如何在3D绘图中绘制一个圆/环.有人可以帮助我提供代码吗?预先感谢!
I am a beginner in Python. I'm trying to plot a circle using matplotlib that has tangent to Z axis. I know how to draw a sphere in 3D but don't know how to draw a circle/ring in 3D plot. Can someone help me with the code? Thanks in advance!
推荐答案
您需要常规的导入以及3D工具包
You need the usual imports, plus the 3D toolkit
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
您需要启用3D的 axes
对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
您需要一个包含在平面 y-z
You need a circle, contained in the plane y-z
theta = np.linspace(0, 2 * np.pi, 201)
y = 10*np.cos(theta)
z = 10*np.sin(theta)
现在我们可以绘制原始圆,并举例说明一些绕 z 轴旋转且其中心也位于固定距离(等于圆弧)的圆.半径)从 z 轴开始,以便它们与之相切
now we can plot the original circle and, as an example, a number of circles rotated about the z-axis and whose centers are also placed at a fixed distance (equal to the c ircles'radius) from the z-axis, so that they are tangent to it
for i in range(18):
phi = i*np.pi/9
ax.plot(y*np.sin(phi)+10*np.sin(phi),
y*np.cos(phi)+10*np.cos(phi), z)
最终,我们放置了一个垂直轴和一个图例
eventually we place a vertical axis and a legend
ax.plot((0,0),(0,0), (-10,10), '-k', label='z-axis')
ax.legend()
现在该看看我们得到了什么
It's time to see what we got
plt.show()
这篇关于如何在Matplotlib中制作3D圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!