gpt4 book ai didi

python - 成对绘制线条

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:55 24 4
gpt4 key购买 nike

我正在开发一个网络项目,其中我需要在点对(节点)之间绘制线条(边缘)。目前我正在为此使用 matplotlib.pyplot ,但问题是 pyplot.plot(x, y) 从 (x[0], y[0]) 开始,然后继续到 (x[1], y[1 ]) 等等
我有一个单独的用于节点连接的元组列表:
Edges=[(0,1), (0,2), (3,2), (2,1)...(m,n)],指的是单独节点的索引。问题是我需要用 matplotlib.animation 来制作动画。

为了在节点之间添加线条(静态图片),我使用了 ax.add_line(Line2D([x1, x2], [y1, y2])),但我不知道如何让这个方法工作使用animation.FuncAnimation()。

一些虚拟代码:

import matplotlib.pyplot as plt

edges = [(0,1), (2,3), (3,0), (2,1)]

x = [-5, 0, 5, 0]
y = [0, 5, 0, -5]

lx = []
ly = []
for edge in edges:
lx.append(x[edge[0]])
lx.append(x[edge[1]])
ly.append(y[edge[0]])
ly.append(y[edge[1]])

plt.figure()
plt.plot(x, y, 'ro')
plt.plot(lx, ly, '-', color='#000000')
plt.show()

(此示例和下面的下一个示例的图像)

如果我使用以下内容:

import matplotlib.pyplot as plt
from pylab import Line2D, gca

edges = [(0,1), (2,3), (3,0), (2,1)]

x = [-5, 0, 5, 0]
y = [0, 5, 0, -5]

plt.figure()
ax = gca()
for edge in edges:
ax.add_line(Line2D([x[edge[0]], x[edge[1]]], [y[edge[0]], y[edge[1]]], color='#000000'))

ax.plot(x, y, 'ro')
plt.show()

一切都按照我需要的方式进行: Examples .
不幸的是,这在动画期间是不可能的(据我所知)。我需要的是一种在各个节点对之间绘制线条的方法。

我知道问题的表述非常糟糕,但我希望有人理解并能够提供帮助。

谢谢!

最佳答案

>>> edges=[(0,1), (0,2), (3,2), (2,1)]
>>>
>>> xx = [x[0] for x in edges]
[0, 0, 3, 2]
>>> yy = [x[1] for x in edges]
[1, 2, 2, 1]
>>> line, = ax.plot(xx, yy, 'ro-')

然后只需将其提供给 plot 并为结果设置动画。这是one example (有很多)。

关于python - 成对绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17698824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com