python - 添加圆柱体绘图

标签 python matplotlib plot

我想在我的 3D 散点图中添加一个透明圆柱体。我该怎么做?

这是我用来制作情节的代码:

fig = plt.figure(2, figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

ax.scatter(X, Y, Z, c=Z,cmap=plt.cm.Paired)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.xticks()

最佳答案

今天,我必须在我的项目中做同样的事情,即在结果中添加一个透明圆柱体。这是我最终得到的代码。所以分享给大家,学习一下

import numpy as np

def data_for_cylinder_along_z(center_x,center_y,radius,height_z):
    z = np.linspace(0, height_z, 50)
    theta = np.linspace(0, 2*np.pi, 50)
    theta_grid, z_grid=np.meshgrid(theta, z)
    x_grid = radius*np.cos(theta_grid) + center_x
    y_grid = radius*np.sin(theta_grid) + center_y
    return x_grid,y_grid,z_grid

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Xc,Yc,Zc = data_for_cylinder_along_z(0.2,0.2,0.05,0.1)
ax.plot_surface(Xc, Yc, Zc, alpha=0.5)

plt.show()

你会得到这个美丽的身影。 enter image description here

关于python - 添加圆柱体绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26989131/

相关文章:

python - 使用 matplotlib 在折线图中查找不同形状的多个数组之间的值分布

matlab - 显示多个数据时更好的错误栏

r - 创建按绘图类型封装的 ggplot2 图表

python - 寻找一种按休息时间分割类次的算法

python - python 中缺少 lxml 模块?

python - 无法将字符串转换为 float - Pandas 阅读专栏

python - 在 df.plot(kind ='hist') 中指定 bean 的数量

Python信号量永远挂起

Python 正则表达式 - 子串匹配

python - 将图例添加到网络图中以解释节点的着色