Matplotlib lib三维曲面图,第4维为

2024-09-27 07:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在遵循了here提供的解决方案之后,我发现该图看起来不像我期望的那样。在

z轴最大值不大于5.89。然而,据我所见,Z轴取Z1的值。在

我想知道问题出在哪里。在

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

x = [10.0, 14.0, 18.0, 14.0, 6.0, 6.0, 2.0, 18.0, 18.0, 6.0, 18.0, 14.0, 10.0, 10.0, 6.0, 6.0, 10.0, 14.0, 2.0, 18.0, 10.0, 14.0]
y = [1.8, 1.4, 1.2, 2.0, 2.0, 1.4, 2.0, 1.8, 2.0, 1.8, 1.6, 1.8, 2.0, 1.2, 1.6, 1.2, 1.6, 1.2, 1.8, 1.4, 1.4, 1.6]
z = [1.22, 2.14, 1.66, 0.7, 2.86, 5.89, 3.85, 0.45, 0.4, 4.28, 0.6, 0.92, 0.67, 3.52, 5.25, 4.94, 1.37, 3.76, 4.75, 0.95, 1.99, 1.41]
z1 = [29.0, 26.72, 26.71, 31.33, 29.46, 24.84, 32.54, 31.43, 33.84, 28.14, 29.84, 31.34, 30.51, 25.0, 25.73, 24.06, 27.09, 26.89, 29.85, 28.93, 26.58, 27.53]


# domains
x = np.array(x)
y = np.array(y)
z = np.array(z)
z1 = np.array(z1)

# convert to 2d matrices
Z = np.outer(z.T, z)        # 50x50
Z1 = np.outer(z1.T, z1)        # 50x50
X, Y = np.meshgrid(x, y)    # 50x50

# fourth dimention - colormap
# create colormap according to x-value (can use any 50x50 array)
color_dimension = Z1 # change to desired fourth dimension
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors = m.to_rgba(color_dimension)

# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.canvas.draw()
fig.savefig('test.pdf')

enter image description here


Tags: toimportmatplotlibnpfigpltaxarray
1条回答
网友
1楼 · 发布于 2024-09-27 07:33:02

你拿外面的产品:

Z = np.outer(z.T, z)        # 50x50

因此Z矩阵的最大值是5.89*5.89=34.69,你的图似乎是正确的。在

您可能希望使用scipy或matplotlib中的griddata方法在网格上插值不规则间隔的数据,请参见Contour plot of irregularly spaced data。在

正如this post中建议的那样,您也可以使用griddata作为颜色。完整的例子(这里有来自matplotlib.tri的插值)如下所示:

^{pr2}$

感谢ImportanceOfBeingErnest指出matplotlib中的griddata不推荐使用,并提供了指向当前示例的链接。在

相关问题 更多 >

    热门问题