gpt4 book ai didi

python - 在 3D 曲面/等高线图中标记投影的最小值和最大值

转载 作者:行者123 更新时间:2023-12-04 16:10:52 33 4
gpt4 key购买 nike

我在这里使用标准的 matplotlib surfaceplot 作为示例。

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)][1]][1]

Example plot with two extrema

我想在轮廓上各自的位置用“X”标记曲面的两个极值。如何实现?

我试过:

max_column = np.argmax(np.max(Z, axis=0))
max_row = np.argmax(np.max(Z, axis=1))

min_column = np.argmin(np.min(Z, axis=0))
min_row = np.argmin(np.min(Z, axis=1))

target = [max_row,max_column,0]


ax.plot([target[0]],[target[1]],[0],'r',marker = u'X',markersize = 8)

我想我不知何故需要投影坐标。

另外,我想在极值所在的 2D 平面上用线画一条细线交叉线。

最佳答案

首先需要找出Z数组的最小值和最大值对应的点。
然后,您可以绘制这些点,其中将其中一个坐标设置为与等高线的相应 offset 的值,这样就可以投影它们。

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)

# calc index of min/max Z value
xmin, ymin = np.unravel_index(np.argmin(Z), Z.shape)
xmax, ymax = np.unravel_index(np.argmax(Z), Z.shape)

# min max points in 3D space (x,y,z)
mi = (X[xmin,ymin], Y[xmin,ymin], Z.min())
ma = (X[xmax, ymax], Y[xmax, ymax], Z.max())

# Arrays for plotting,
# first row for points in xplane, last row for points in 3D space
Ami = np.array([mi]*4)
Ama = np.array([ma]*4)
for i, v in enumerate([-40,40,-100]):
Ami[i,i] = v
Ama[i,i] = v

#plot points.
ax.plot(Ami[:,0], Ami[:,1], Ami[:,2], marker="o", ls="", c=cm.coolwarm(0.))
ax.plot(Ama[:,0], Ama[:,1], Ama[:,2], marker="o", ls="", c=cm.coolwarm(1.))

ax.view_init(azim=-45, elev=19)
plt.savefig(__file__+".png")
plt.show()

enter image description here

关于python - 在 3D 曲面/等高线图中标记投影的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911928/

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