在matplotlib中,从XY平面中的一点向(x,y,z)点拉伸直线

2024-04-24 07:23:59 发布

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

我试图创建一条从xy平面的(x,y,0)点开始,以(x,y,z)值结束的直线。在

在matplotlib中可以这样吗?在

以下是我所拥有的一个例子: enter image description here

与我想要的相比: enter image description here

这里有点在(1,1,1)。基本上,我想知道它在平面上的延伸

希望通过这个例子我的问题能清楚地理解。在


Tags: matplotlib平面直线例子xy
2条回答

这没有任何意义。如果你在三维空间中工作,那么每个点都可以在三维空间中描述,包括你的起点。在三维空间中用两个坐标来描述事物本质上就是在描述一条直线;如果只显式地标记x和y坐标,那么所有可能的z值都是有效的,所以最终得到的是一条直线。

你要做的是使其中一个坐标(x,y,z)为零。

这是我所做的。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.cm as cm
import numpy as np

fig = plt.figure(figsize=(1[![enter image description here][1]][1]5,10))

ax = fig.add_subplot(111, projection='3d')

# points
xs = np.asarray([np.cos(i) for i in np.arange(0,np.pi/2,(np.pi/2)/10)])
ys = np.arange(0,10,1)
zs = np.arange(0,10,1)

# plot points
ax.scatter(xs=xs, ys=ys, zs=zs, s=200, c=zs, cmap=cm.viridis_r, alpha=1.0)

# create the line
zs_l = np.asarray([[i, -1] for i in zs])

# color list
cl = [cm.get_cmap('viridis_r')(i/zs_l.shape[0]) for i in range(zs_l.shape[0])]
# draw the lines 
for i, p in enumerate(zs_l):
    ax.plot(xs=[xs[i]]*2, ys=[ys[i]]*2, zs=zs_l[i], markersize=0, lw=3, c=cl[i])

Final plot.

相关问题 更多 >