首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ax.plot_surface会覆盖后续的ax.scatter

ax.plot_surface会覆盖后续的ax.scatter
EN

Stack Overflow用户
提问于 2020-07-12 14:11:24
回答 1查看 104关注 0票数 0

我正在尝试绘制一个与点云相交的曲面图像。下面是我希望得到的输出:

下面是我得到的信息:

如您所见,曲面似乎覆盖了散点图。我尝试在平面周围将散点图一分为二,如下所示:

代码语言:javascript
复制
break_idx = np.searchsorted(ts, img_ts)

ax.scatter(xs[0:break_idx], ts[0:break_idx], ys[0:break_idx], zdir='z',
   c=colors[0:break_idx], facecolors=colors[0:break_idx])

ax.plot_surface(y, img_ts, x, rstride=5, cstride=5, facecolors=img, alpha=1)

ax.scatter(xs[break_idx:-1], ts[break_idx:-1], ys[break_idx:-1], zdir='z',
   c=colors[break_idx:-1], facecolors=colors[break_idx:-1])

然而,这对我来说并不起作用。你知道可能发生了什么吗?

+++编辑+++

感谢@JohanC的建议,不幸的是,它导致了同样的问题,在这里以GIF格式可视化:

从我的结果和评论来看,这只是matplotlibs 3D绘图能力的一个缺陷/限制。哦,是时候学习使用mayavi了.

使用mayavi的最小工作示例可以在here中找到,尽管它在stackoverflow上的原因是我遇到了类似的问题。

EN

回答 1

Stack Overflow用户

发布于 2020-07-12 22:12:03

也许您的阵列tsxsyscolors没有通过增加ts来很好地排序?此外,np.searchsorted返回一个数组,因此您不能直接将其结果用作[0:break_idx]。也许您可以接受返回数组的第一个元素:break_idx = np.searchsorted(ts, img_ts)[0]

正如评论中所说,matplotlib不支持完整的3D,如果你需要漂亮的3D隐藏,Mayavi是通常的建议之一。

但是,在您的示例中,如果不将查看位置移动到平面后面,则可以先绘制平面后面的点,然后绘制平面,然后再绘制前面的点。这就是问题中的代码试图做的事情。

要选择平面前面的点,一个更简单的解决方案是只创建一个索引列表:front_indices = ts < img_ts。如果这给出了错误的结果,只需将<切换为>即可。使用[front_indices]仅选择前面的值,使用[~front_indices]选择平面另一侧的值。

代码语言:javascript
复制
front_indices = ts < img_ts  # supposing ts is a numpy array, and img_ts is one special value

ax.scatter(xs[~front_indices], ts[~front_indices], ys[~front_indices], zdir='z',
   c=colors[~front_indices])

ax.plot_surface(y, img_ts, x, rstride=5, cstride=5, facecolors=img, alpha=1)

ax.scatter(xs[front_indices], ts[front_indices], ys[front_indices], zdir='z',
   c=colors[front_indices])

下面是一个更完整的示例:

代码语言:javascript
复制
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.art3d as art3d
import numpy as np

N = 10000
xs = np.random.uniform(4, 6, N)
ys = np.random.uniform(4, 6, N)
ts = np.random.uniform(1, 9, N)
colors = np.random.uniform(0, 1, N)
img_ts = 5

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

front_indices = ts < img_ts

ax.scatter(xs[~front_indices], ts[~front_indices], ys[~front_indices], zdir='z',
           c=colors[~front_indices], cmap='seismic', s=1)

# Draw a circle on the y=5 'wall'
p = Circle((5, 5), 4)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=img_ts, zdir="y")

ax.scatter(xs[front_indices], ts[front_indices], ys[front_indices], zdir='z',
           c=colors[front_indices], cmap='seismic', s=1)

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
ax.set_xlabel('x')
ax.set_ylabel('t')
ax.set_zlabel('y')

ax.view_init(elev=15, azim=33)
plt.show()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62857702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档