Warm tip: This article is reproduced from stackoverflow.com, please click

发布于 2020-03-27 10:18:25

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!

Questioner

Higa

Viewed

79

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
```

You need a 3D enabled `axes`

object

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

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

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

Higa2019-07-03 21:53:19Thank you for your help and time. Could you please help me to solve out one more question? I have also posted the question. You can click on the link to go to that question. stackoverflow.com/q/56868672/11728630

gboffi2019-07-03 22:02:00@HirenGadhiya I have updated my answer because I have

alreadyfound your other question... I hope that my answer is more close to what you've really asked. — I will comment on the other question but I'm not going to answer because I think it's quite off-topic wrt programming. Ciao.gboffi2019-07-03 22:30:08I got a possibly well deserved downvote and so I ask

"How could I improve my answer?"