简体   繁体   中英

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

I made a 3D plot using the following code in python. Here three arrays x, y and z are used for the plot. I want to show the last point of the arrays (or the end point of the 3D line) in the plot. 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') . 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. Which is impossible, because you cant draw a line using 1 point only. Therefore, you tell it to draw a scatter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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