在 Python 中绘制二维方格原子的最佳方法?
Best way to plot a 2D square lattice of atoms in Python?
问题总结:
我正在研究一个物理问题,我想绘制一个 2D 原子晶格,其中节点已使用此处所示的箭头连接 2D lattice figure。
我尝试过的:
我试过使用 grid_2d_graph from NetworkX, taking help from this 但无法让它按我希望的那样工作。我使用的代码如下:
G = nx.grid_2d_graph(4,4)
pos = dict( (n, n) for n in G.nodes() )
nx.draw_networkx(G, pos=pos)
plt.axis('off')
plt.show()
这产生了以下 image,这与我的想法不符。
这是使用 matplotlib
的 arrow
的简单方法。它要求网格(图形)表示为字典,其中键是网格上的点(节点)坐标,值是 outgoing 箭头(边)的相邻点应该绘制。当网格的大小发生变化时,您可能需要使用 w
、h
等来控制绘制元素的大小。
grid = { # point(x, y), outgoing connections [points]
(0, 0): [(0, 1), (1, 0)],
(0, 1): [],
(1, 0): [],
(1, 1): [(1, 0), (0, 1)]
}
w = 0.005 # Errorwidth
h = 0.05 # Errorhead width
fig, ax = plt.subplots()
for point, connections in grid.items():
for outgoing in connections:
dx = outgoing[0] - point[0]
dy = outgoing[1] - point[1]
ax.arrow(point[0], point[1],
dx / 2, dy / 2,
width=w, head_width=h,
facecolor="k",
zorder=0)
ax.arrow(point[0] + dx / 2,
point[1] + dy / 2,
dx / 2, dy / 2,
width=w, head_width=0,
facecolor="k",
zorder=0)
ax.plot(*point,
marker="o", markersize=10, markeredgecolor="k",
markerfacecolor="red",
zorder=1)
当我想到 Jan 的回答时,我正在尝试使用箭袋图。我修改了他的代码以使用箭袋图
def plot_vector(p1,p2):
p1 = np.array(p1)
p2 = np.array(p2)
dp = p2-p1
plt.quiver(p1[0], p1[1], dp[0], dp[1],angles='xy', scale_units='xy', scale=1, headwidth = 5, headlength = 7)
grid = { # point(x, y), outgoing connections [points]
(0, 0): [(0, 1), (1, 0)],
(0, 1): [],
(1, 0): [],
(1, 1): [(1, 0), (0, 1)]
}
fig, ax = plt.subplots()
for point, connections in grid.items():
for outgoing in connections:
plot_vector(point,outgoing)
plt.plot(*point,
marker="o", markersize=10, markeredgecolor="k",
markerfacecolor="red",
zorder=1)
plt.show()
问题总结: 我正在研究一个物理问题,我想绘制一个 2D 原子晶格,其中节点已使用此处所示的箭头连接 2D lattice figure。
我尝试过的:
我试过使用 grid_2d_graph from NetworkX, taking help from this
G = nx.grid_2d_graph(4,4)
pos = dict( (n, n) for n in G.nodes() )
nx.draw_networkx(G, pos=pos)
plt.axis('off')
plt.show()
这产生了以下 image,这与我的想法不符。
这是使用 matplotlib
的 arrow
的简单方法。它要求网格(图形)表示为字典,其中键是网格上的点(节点)坐标,值是 outgoing 箭头(边)的相邻点应该绘制。当网格的大小发生变化时,您可能需要使用 w
、h
等来控制绘制元素的大小。
grid = { # point(x, y), outgoing connections [points]
(0, 0): [(0, 1), (1, 0)],
(0, 1): [],
(1, 0): [],
(1, 1): [(1, 0), (0, 1)]
}
w = 0.005 # Errorwidth
h = 0.05 # Errorhead width
fig, ax = plt.subplots()
for point, connections in grid.items():
for outgoing in connections:
dx = outgoing[0] - point[0]
dy = outgoing[1] - point[1]
ax.arrow(point[0], point[1],
dx / 2, dy / 2,
width=w, head_width=h,
facecolor="k",
zorder=0)
ax.arrow(point[0] + dx / 2,
point[1] + dy / 2,
dx / 2, dy / 2,
width=w, head_width=0,
facecolor="k",
zorder=0)
ax.plot(*point,
marker="o", markersize=10, markeredgecolor="k",
markerfacecolor="red",
zorder=1)
当我想到 Jan 的回答时,我正在尝试使用箭袋图。我修改了他的代码以使用箭袋图
def plot_vector(p1,p2):
p1 = np.array(p1)
p2 = np.array(p2)
dp = p2-p1
plt.quiver(p1[0], p1[1], dp[0], dp[1],angles='xy', scale_units='xy', scale=1, headwidth = 5, headlength = 7)
grid = { # point(x, y), outgoing connections [points]
(0, 0): [(0, 1), (1, 0)],
(0, 1): [],
(1, 0): [],
(1, 1): [(1, 0), (0, 1)]
}
fig, ax = plt.subplots()
for point, connections in grid.items():
for outgoing in connections:
plot_vector(point,outgoing)
plt.plot(*point,
marker="o", markersize=10, markeredgecolor="k",
markerfacecolor="red",
zorder=1)
plt.show()