gpt4 book ai didi

python - 如何在 MatPlotLib 中注释/突出显示 3d 图

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:13 25 4
gpt4 key购买 nike

我想做这样的事情:http://matplotlib.sourceforge.net/_images/86cbd17f31.png在 3d 空间中。基本上我想突出显示曲面图的一部分。有什么建议吗?

最佳答案

edit3(这取代了一个非常错误的先前答案)

再次更新。见评论

如果向下钻取到绘图生成的多边形集合,则可以修改曲面图的面颜色。它做了一些神奇的着色并根据 zorder 重新排序颜色列表,所以我在弄清楚如何将指定的着色保持在未突出显示的区域但仍然能够索引感兴趣的区域时遇到了一些麻烦。这是一种有效的方法。我希望你想要阴影面而不是某种 3D 半透明柱。这也可以做到,但我认为很难分辨突出显示的内容并且定义 zorder 真的很棘手。

enter image description here

enter image description here

import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.patches import Rectangle, PathPatch

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

xlo = X.min()
xhi = X.max()
ylo = Y.min()
yhi = Y.max()
zlo = -2
zhi = 2

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=1, zorder=100)
cset = ax.contour(X, Y, Z, zdir='z', offset=zlo, alpha=0.0)

def highlight((xmin, xmax),(ymin, ymax)):
# draw highlight on xz plane
p1 = Rectangle((ymin,zlo),(ymax-ymin),(zhi-zlo), color='y', alpha=0.5, zorder=0)
ax.add_patch(p1)
art3d.pathpatch_2d_to_3d(p1, z=xlo, zdir='x')

# draw highlight on yz plane
p2 = Rectangle((xmin,zlo),(xmax-xmin),(zhi-zlo), color='y', alpha=0.5, zorder=0)
ax.add_patch(p2)
art3d.pathpatch_2d_to_3d(p2, z=yhi, zdir='y')

# define a region to highlight
highlight = (X>xmin)&(X<xmax)&(Y>ymin)&(Y<ymax)
coll = ax.collections[0]
# get the original color shading (if you want to keep that effect)
colors = coll._facecolors_original
#colors = coll.get_facecolors()
# they are stored as a list for some reason so get the flat indicies
for idx in np.where(highlight[:-1,:-1].flat)[0]:
# and modifly one-by-one
color = colors[idx][:]
colors[idx][0] = color[2] # swap red with blue
colors[idx][3] = color[0]
colors[idx][4] = .2 #change alpha
# re-set the face colors
coll.set_facecolors(colors)

highlight((-3,0),(-3,1))

ax.set_xlim3d(xlo, xhi)
ax.set_ylim3d(ylo, yhi)
ax.set_zlim3d(zlo, zhi)

plt.show()

关于python - 如何在 MatPlotLib 中注释/突出显示 3d 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com