python绘制一个球体与圆柱相接
时间: 2023-12-12 21:08:55 浏览: 145
这里是一个Python绘制球体与圆柱相接的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 创建3D坐标轴
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制球体
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x = np.cos(u)*np.sin(v)
y = np.sin(u)*np.sin(v)
z = np.cos(v)
ax.plot_surface(x, y, z, cmap='Reds')
# 绘制圆柱
z = np.linspace(-1, 1, 100)
r = np.sqrt(1 - z**2)
theta = np.linspace(0, 2*np.pi, 100)
x = r * np.cos(theta)
y = r * np.sin(theta)
ax.plot(x, y, z, color='blue')
# 设置坐标轴范围
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
plt.show()
```
运行代码后,会得到一个球体与圆柱相接的3D图形,如下图所示:
![球体与圆柱相接的3D图形](https://img-blog.csdnimg.cn/20210904140504995.png)
阅读全文