错误发生,因为plt.plot(x, y)
预计x
和y
是列表或数组具有长,而你给了它一个浮动。您可以通过在方括号内附上[atom1[i]]
使其成为一个列表来避免这种情况。
但是,通常最好避免这种情况,因为它不清楚发生了什么。不是循环通过每个原子,而是将它们全部粘合到一个数组中,然后绘制数组的列。您甚至可以在创建原子时发现,只要首先在数组中创建原子即可。例如:
from matplotlib import pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Define several atoms, these are numpy arrays of length 3
# randomly pulled from a uniform distribution between -1 and 1
atom1 = np.random.uniform(-1, 1, 3)
atom2 = np.random.uniform(-1, 1, 3)
atom3 = np.random.uniform(-1, 1, 3)
atom4 = np.random.uniform(-1, 1, 3)
# Define a list of colors to plot atoms
colors = ['r', 'g', 'b', 'k']
# Here all the atoms are stacked into a (4, 3) array
atoms = np.vstack([atom1, atom2, atom3, atom4])
ax = plt.subplot(111, projection='3d')
# Plot scatter of points
ax.scatter3D(atoms[:, 0], atoms[:, 1], atoms[:, 2], c=colors)
plt.show()
我添加了颜色,因为它有助于查看哪个原子是哪个。
非常感谢您的详细解答。这是非常有用的,但我也需要知道,如果我可以在原子之间绘制矢量。即,例如,我想绘制从atom1到atom2的矢量。 – user2804070
而不是使用'scatter'只是使用'ax.plot(atoms [:, 0],atoms [:, 1],atoms [:,2])''。这在原子中的每个点之间绘制一条线。您将必须决定如何分割它,例如'atoms [0:1,0]'给出前两个原子的x坐标。 – Greg