2017-01-10 183 views
0

我正在制作四面体分子的分子图,其中三个外部点(或原子)需要通过线连接到中心点。将点连接到三维散点上的中心点Python

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() 

我想所有的点连接到中央点(x = 1.5,Y = 1.5,Z = 1.2)。以下是目前为止的样子: plot

回答

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() 

3d_line_conection_example