我正在尝试绘制带有xlist和ylist的3D表面图,它们是一维列表,zlist是列表列表。
xlist= [0, 0.1, 0.22173, 0.3, 0.4, 0.5, 0.6]
ylist = [0, 0.1,0.4,1,2,5]
zlists = [[0, 0.0100954, 0.05117122, 0.0952171, 0.1628218419, 0.1245, 0.0731856],
[[0, 0.0101496, 0.0516, 0.09716544, 0.16126, 0.1025817, 0.059077],
[[0, 0.01096289, 0.05788095, 0.137201, 0.1133218, 0.0638898, 0.0334927],
[[0, 0.0155434802394, 0.10213, 0.120433828182, 0.0620560791526, 0.0318, 0.019],
[[0, 0.031145105337, 0.12922959, 0.064018969907, 0.021701508055, 0.006237178, 0.002],
[[0, 0.11832666, 0.02912328, 0.00511592, 0.0004291, 0.00005, 0.000006]]
我找到了一个名为Axes3D.plot_surface的合适函数,它可以将数据转换为曲面图:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots
然而,它要求xlist和ylist也是2D数组。我怎样才能将xlist和ylist转换为所需的2D数组格式,以便能够轻松地用于Axes3D.plot_surface函数。
答案 0 :(得分:1)
使用np.meshgrid
从xlist
和ylist
生成2D网格数组:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
xlist = [0, 0.1, 0.22173, 0.3, 0.4, 0.5, 0.6]
ylist = [0, 0.1, 0.4, 1, 2, 5]
Z = np.array([
[0, 0.0100954, 0.05117122, 0.0952171, 0.1628218419, 0.1245, 0.0731856],
[0, 0.0101496, 0.0516, 0.09716544, 0.16126, 0.1025817, 0.059077],
[0, 0.01096289, 0.05788095, 0.137201, 0.1133218, 0.0638898, 0.0334927],
[0, 0.0155434802394, 0.10213, 0.120433828182, 0.0620560791526, 0.0318, 0.019],
[0, 0.031145105337, 0.12922959, 0.064018969907, 0.021701508055, 0.006237178, 0.002],
[0, 0.11832666, 0.02912328, 0.00511592, 0.0004291, 0.00005, 0.000006]])
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
X, Y = np.meshgrid(xlist, ylist)
ax.plot_surface(X, Y, Z, alpha=0.5, rstride=1, cstride=1)
ax.scatter(xlist[3], ylist[3], Z[3,3], s=50, c='r')
plt.show()
顺便说一句,source code for the example中显示的the plot_surface documentation也使用meshgrid
。这将是找到这个答案的另一种方式。