2017-01-10 181 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