问题描述
我正在尝试绘制3D表面图形,其中xlist和ylist是一维列表,而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" rel ="nofollow noreferrer"> plot_surface文档也使用了 meshgrid
.那是找到这个答案的另一种方式.
I am trying to plot a 3D surface graph with an xlist and a ylist which are one dimensional lists and zlist which is a list of lists.
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]]
I have found a suitable function called Axes3D.plot_surface that can convert the data into a surface plot: http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots
However it requires the xlist and ylist to be 2D arrays too. How could I transform the xlist and ylist into the required 2D array formats to be able to use readily for the Axes3D.plot_surface function.
Use np.meshgrid
to generate 2D grid arrays from xlist
and ylist
:
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()
By the way, the source code for the example shown in the plot_surface documentation uses meshgrid
too. That would be another way to find this answer.
这篇关于使用列表绘制3D表面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!