使用权重使用NetworkX绘制图形

时间:2016-12-01 18:44:13

标签: matplotlib networkx

我有一个边缘列表:

package com.security.shiro

import com.security.AuthenticateTrait
import com.ws.RemoteUserAccessor
import com.ws.RequestAccessor

import javax.servlet.ServletRequest
import javax.servlet.ServletResponse

/**
 * Created by esarrazin on 11/30/2016.
 */
class IEBugFilter3 extends NegotiateAuthenticationFilter implements RemoteUserAccessor, AuthenticateTrait, RequestAccessor {

/**
 * Returns <code>false</code> If the giant piece of sh1t IE chooses to think that it will need to re-authenticate
 * and not send the body to us.  Here we will deny access (as IE expects - big turd) and have waffle re-negotiate
 * this useless sh1t-bag.  Then and only then will IE choose to give us the body of the POST.  This problem is intermittent
 * and there is no telling when IE will use its infinite wisdom to do this, so please don't think if you comment this
 * out it is still working without.
 *
 * Not a silver bullet, but here is a link to some reading about this:
 * https://blogs.msdn.microsoft.com/ieinternals/2010/11/21/challenge-response-authentication-and-zero-length-posts/
 *
 * @return Returns true if isAccessAllowed
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        !isRequestExpectingToReAuth(request, response)
}

private boolean isRequestExpectingToReAuth(ServletRequest request, ServletResponse response) {
    if (request?.request?.request?.coyoteRequest?.methodMB == 'POST' && toHttp(request).getHeader('content-length') == '0') {
        System.out.println("Found the dreaded zero length body and attempting to recover from it. $request.remoteHost")
        return true
    }

    return false
}

如果索引0是节点,则列表中的索引1是节点,索引2是权重值。

我想做的是这样的事情:

[[0,0,0], [0,1,1], [0,2,1], [2,3,2], ....[n,m,t]]

方向无关紧要,在编辑器中垂直绘制更容易。 我想使用matplotlib导出它。

谢谢!

1 个答案:

答案 0 :(得分:1)

您提供的边缘列表是否代表您的所有数据?如果是,你甚至不需要权重来绘制你想要的图像(给出你的例子)。

在下面的代码中,我使用graphviz_layout来计算图形/树的布局。请注意,代码是为Python 2编写的。同样,我只使用边缘信息而不考虑权重。

import networkx as nx
import matplotlib.pyplot as plt

data = [[0,0,0], [0,1,1], [0,2,1], [2,3,2]]
G = nx.Graph()

for row in data:
    G.add_edge(row[0], row[1])

pos = nx.graphviz_layout(G, prog='dot')  # compute tree layout
nx.draw(G, pos, with_labels=True, node_size=900, node_color='w')  # draw tree and show node names
plt.show()  # show image

输出:

enter image description here