简体   繁体   English

显示 3d 线的终点:Python 3D 图

[英]Showing end point of the 3d line: Python 3D plot

I made a 3D plot using the following code in python.我在 python 中使用以下代码制作了一个 3D 图。 Here three arrays x, y and z are used for the plot.这里三个数组 x、y 和 z 用于绘图。 I want to show the last point of the arrays (or the end point of the 3D line) in the plot.我想在图中显示数组的最后一个点(或 3D 线的终点)。 I used the approach I would use in 2d plotting, ie, I asked for plotting only the last points of each array using this command ax.plot(x[-1],y[-1],z[-1],'o') .我使用了在 2d 绘图中使用的方法,即,我要求使用此命令仅绘制每个数组的最后一个点ax.plot(x[-1],y[-1],z[-1],'o') But it doesn't work.但它不起作用。

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import

x=np.linspace(0,2*np.pi)
y=np.sin(x)
z=np.cos(x)

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, lw=1)
ax.plot(x[-1],y[-1],z[-1],'o') # This line doesn't work
plt.show()
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import

x=np.linspace(0,2*np.pi)
y=np.sin(x)
z=np.cos(x)

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, lw=1)
ax.scatter(x[-1],y[-1],z[-1],'-') # This should do the job
plt.show()

Add Color and Label添加颜色和标签

ax.scatter(x[-1],y[-1],z[-1],'-',c="yellow",label="End Point") 
plt.legend()
plt.show()

块引用

Additional explanation on why you were having an error :关于错误原因的附加说明:

You were telling python to draw you a ax.plot for 1 point.你告诉 python 给你画一个ax.plot 1 点。 Which is impossible, because you cant draw a line using 1 point only.这是不可能的,因为你不能只用 1 个点画一条line Therefore, you tell it to draw a scatter .因此,您告诉它绘制scatter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM