我制作了一个四面体分子的分子图,其中三个外部点(或原子)需要通过线连接到中心点。
从How can I connect points on a 3D scatter plot?我能够连接点,但它会产生不正确的行。
这是我的代码:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 1.2, 1.5, 1.5]
y = [1, 1.2, 2, 1.5, 1.5]
z = [.5, .5, .5, 1.2, 2]
a = []
b = []
c = []
for item in x:
a.append(float(item))
for item in y:
b.append(float(item))
for item in z:
c.append(float(item))
r = np.array(a)
s = np.array(b)
t = np.array(c)
ax.set_xlabel("x axis")
ax.set_ylabel("y axis")
ax.set_zlabel("z axis")
ax.scatter(r,s,zs = t, s=200)
ax.plot3D(r,s,z)
plt.show()
答案 0 :(得分:2)
如果你ax.plot3D(r,s,z)
,你正在绘制一条接一条连接5个点的线。你需要的是从每个点到你想要的点绘制一条线。
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 1.2, 1.5, 1.5]
y = [1, 1.2, 2, 1.5, 1.5]
z = [.5, .5, .5, 1.2, 2]
# Change the way you create the array.
# You can do it in several ways.
r = np.array(x, dtype=np.float)
s = np.array([float(i) for i in y])
t = np.array(z) * 1.0
ax.scatter(r,s,zs = t, s=200, label='True Position')
# Iterate over each point, and plot the line.
for x, y, z in zip(r, s, t):
ax.plot3D([x, 1.5], [y, 1.5], [z, 1.2], 'b')
plt.show()