Matplotlib lib 3d表面图,第4维为颜色

时间:2018-10-09 07:13:06

标签: python matplotlib

在遵循提供的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

1 个答案:

答案 0 :(得分:1)

您购买外部产品:

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的插值)如下所示:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as tri
from matplotlib.colors import Normalize

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)

# Create grid values first.
ngridx = 100
ngridy = 100
xi = np.linspace(x.min(), x.max(), ngridx)
yi = np.linspace(y.min(), y.max(), ngridy)

# Perform linear interpolation of the data (x,y)
# on a grid defined by (xi,yi)
triang = tri.Triangulation(x, y)
interpolator_z = tri.LinearTriInterpolator(triang, z)
interpolator_z1 = tri.LinearTriInterpolator(triang, z1)
Xi, Yi = np.meshgrid(xi, yi)
zi = interpolator_z(Xi, Yi)
z1i = interpolator_z1(Xi, Yi)

X, Y, Z, Z1 = xi, yi, zi, z1i

fig = plt.gcf()
ax1 = fig.add_subplot(111, projection='3d')

minn, maxx = z1.min(), z1.max()
norm = Normalize()
surf = ax1.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=cm.jet(norm(Z1)), vmin=minn, vmax=maxx, shade=False)

m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(Z1)
col = plt.colorbar(m)
plt.show()

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