将圆柱体添加到绘图 [英] Add cylinder to plot

查看:116
本文介绍了将圆柱体添加到绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to add a transparent cylinder to my 3D scatter plot. How can I do it?

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

This is the code I am using to make the plot:

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

推荐答案

一种可能的方法是使用plot_surface.调整此博客文章中给出的解决方案然后

One possible method is to use the plot_surface. Adapting the solution given in this blog post then have

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

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

# Scatter graph
N = 100
X = np.random.uniform(-1, 1, N)
Y = np.random.uniform(-1, 1, N)
Z = np.random.uniform(-2, 2, N)
ax.scatter(X, Y, Z)

# Cylinder
x=np.linspace(-1, 1, 100)
z=np.linspace(-2, 2, 100)
Xc, Zc=np.meshgrid(x, z)
Yc = np.sqrt(1-Xc**2)

# Draw parameters
rstride = 20
cstride = 10
ax.plot_surface(Xc, Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.plot_surface(Xc, -Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()

我添加了一些最小的表面配置,可以通过参考

I've added some minimal configuration of the surface, better can be achieved by consulting the docs.

这篇关于将圆柱体添加到绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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