如何在 Matplotlib 中做一个 3D 圆 [英] How to do a 3D circle in Matplotlib

查看:38
本文介绍了如何在 Matplotlib 中做一个 3D 圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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 对象

You need a 3D enabled axes object

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆