使用三个列表在python中绘制3d [英] plot 3d in python using three lists
问题描述
我按照此链接绘制了3D图形. 我的问题是我已经有3个X,Y,Z列表
I followed this link to plot the 3D figure. My problem is I have already 3 lists for X, Y, Z
X.shape(n,),Y.shape(n,),Z.shape(n,)
X.shape (n,) , Y.shape (n,) , Z.shape (n,)
如何通过链接将这些列表传递到surf = ax.plot_surface(X, Y, Z)
中,以显示这些变量均具有以下形状
How to pass these lists into surf = ax.plot_surface(X, Y, Z)
as link show each of these variables have the following shape
X.shape(n,n),Y.shape(n,n),Z.shape(n,n)
X.shape (n,n) , Y.shape (n,n) , Z.shape (n,n)
如果我通过这些坐标,每个形状均为(n,),则3d图形将显示为空,将不会绘制任何点!
If I passed these coordinate as them each one shape is (n,) then the 3d figure will appear as empty there is no points will be plotted!
我尝试如下使用np.meshgrid
,但是这种方式只能在一个平面上显示一个表面,而不是3d点!
I tried to use the np.meshgrid
as following but this way will show only one surface in one plane instead of 3d points!
X,Y,Z = np.meshgrid(X,Y,Z)
X = X[0]
Y = Y[0]
Z = Z[0]
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
推荐答案
解决方案将取决于数据的组织方式.
The solution will depend on how the data is organized.
如果X
和Y
数据已经定义了网格,则可以轻松地将它们重塑为四边形网格.例如
If the X
and Y
data already define a grid, they can be easily reshaped to a quadrilateral grid. E.g.
#x y z
4 1 3
6 1 8
8 1 -9
4 2 10
6 2 -1
8 2 -8
4 3 8
6 3 -9
8 3 0
4 4 -1
6 4 -8
8 4 8
可以使用
plot_surface
>
can plotted as a plot_surface
using
ax = fig.gca(projection='3d')
ax.plot_surface(X.reshape(4,3), Y.reshape(4,3), Z.reshape(4,3))
任意数据
(a)如果数据不在四边形网格上,则可以将数据插值到网格上. matplotlib本身使用matplotlib.mlab.griddata
提供了一种这样做的方法.
Arbitrary data
(a) In case the data is not living on a quadrilateral grid, one can interpolate the data on a grid. One method to do so is provided by matplotlib itself, using matplotlib.mlab.griddata
.
import matplotlib.mlab
xi = np.linspace(4, 8, num=10)
yi = np.linspace(1, 4, num=10)
zi = matplotlib.mlab.griddata(X, Y, Z, xi, yi, interp='linear')
ax.plot_surface(xi, yi, zi)
(b)最后,无需使用四边形网格即可完全绘制表面.可以使用 plot_trisurf
.
(b) Finally, one can plot a surface completely without the use of a quadrilateral grid. This can be done using plot_trisurf
.
plt.plot_trisurf(X,Y,Z)
此答案是我对等高线图的答案的改编版本.
This answer is an adapted version of my answer for contour plots.
这篇关于使用三个列表在python中绘制3d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!