在对线条绘制 [英] plotting lines in pairs

查看:159
本文介绍了在对线条绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中,我需要绘制对点(节点)之间的线(边缘)的网络项目。我目前使用这个matplotlib.pyplot,但问题是,pyplot.plot(X,Y​​)的(x [0],Y [0]),然后继续到(x开始[1],Y [1 ])等结果
我有元组为节点的连接的一个单独的列表:搜索
边= [(0,1),(0,2),(3,2),(2,1)...(M,N)〕,它指的是独立的节点索引。
问题是,我需要用动画的matplotlib.animation东西。

要我用ax.add_line只需添加节点之间的线(静态图片)(Line2D中([X1,X2],[Y1,Y2])),但我无法弄清楚如何得到这个方法的工作与animation.FuncAnimation()。

有些哑code:

 进口matplotlib.pyplot如PLT边= [(0,1),(2,3),(3,0),(2,1)]X = [-5,0,5,0]
Y = [0,5,0,-5]LX = []
LY = []
在边缘的边缘:
    lx.append(X [边缘[0])
    lx.append(X [边缘[1]])
    ly.append(γ[边缘[0])
    ly.append(γ[边缘[1]])plt.figure()
plt.plot(X,Y​​,ロ)
plt.plot(LX,LY, - ,颜色=#000000)
plt.show()

(这个形象和下方的下一个例子)

如果我改用以下内容:

 进口matplotlib.pyplot如PLT
从pylab进口的Line2D,GCA边= [(0,1),(2,3),(3,0),(2,1)]X = [-5,0,5,0]
Y = [0,5,0,-5]plt.figure()
AX = GCA()
在边缘的边缘:
    ax.add_line(Line2D中([X [边缘[0]],X [边缘[1]]],[Y [边缘[0]],Y [边缘[1]]],颜色=#000000))ax.plot(X,Y​​,ロ)
plt.show()

这一切工作,我需要的方式:
范例。结果
不幸的是,这是不可能的(据我所知)动画过程中。我需要的是随后是绘制单个节点对之间的线的方式。

非常不好的问题制定的,我知道,但我希望有人理解并能够帮助。

感谢您!


解决方案

 >>>边= [(0,1),(0,2),(3,2),(2,1)]
>>>
>>> XX = [在边缘X [0]对于x]
[0,0,3,2]
>>> YY = [X [1]中的x的边缘]
[1,2,2,1]
>>>行= ax.plot(XX,YY,RO-')

然后只给这剧情和动画的结果。这里是一个例子(有很多)。

I'm working on a network project in which I need to draw lines (edges) between pairs of points (nodes). Currently I'm using matplotlib.pyplot for this, but the problem is that pyplot.plot(x, y) starts at (x[0], y[0]) and then continues to (x[1], y[1]) etc.
I have a separate list of tuples for the connections of the nodes:
edges=[(0,1), (0,2), (3,2), (2,1)...(m,n)], which refers to indices of the separate nodes. The problem is that I need to animate the thing with matplotlib.animation.

To just add lines between nodes (static picture) I was using ax.add_line(Line2D([x1, x2], [y1, y2])), but I can't figure out how to get this method to work with animation.FuncAnimation().

Some dummy code:

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

(Image of this and the next example below)

If I instead use the following:

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

it all works the way I need: Examples.
Unfortunately, this is not possible (afaik) during animation. What I need then is is a way to plot lines between individual pairs of nodes.

Very bad problem formulation, I know, but I hope that someone understands and is able to help.

Thank you!

解决方案

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

Then just feed this to plot and animate the result. Here's one example (there are many).

这篇关于在对线条绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆