Matplotlib 和 Networkx - 绘制自循环节点 [英] Matplotlib and Networkx - drawing a self loop node

查看:27
本文介绍了Matplotlib 和 Networkx - 绘制自循环节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能,我想画一个自循环.我该怎么做?
边缘存在,但我认为在这个例子中只是一个点是 (1,1),我无法添加节点的名称.我的目标是从邻接矩阵画一个图.有没有更好的方法来做到这一点?

将 networkx 导入为 nx导入 matplotlib.pyplot 作为 plt从 matplotlib.patches 导入 FancyArrowPatch, Circle将 numpy 导入为 npdef draw_network(G,pos,ax,sg=None):对于 G 中的 n:c=Circle(pos[n],radius=0.05,alpha=0.7)ax.add_patch(c)G.node[n]['patch']=cx,y=pos[n]看过={}对于 G.edges(data=True) 中的 (u,v,d):n1=G.node[u]['补丁']n2=G.node[v]['补丁']弧度=0.1如果 (u,v) 在看到:rad=seen.get((u,v))rad=(rad+np.sign(rad)*0.1)*-1阿尔法=0.5颜色='k'e = FancyArrowPatch(n1.center,n2.center,patchA=n1,patchB=n2,箭头样式='-|>',connectionstyle='arc3,rad=%s'%rad,突变规模=10.0,lw=2,阿尔法=阿尔法,颜色=颜色)看到[(u,v)]=radax.add_patch(e)返回 eG=nx.MultiDiGraph([(1,2),(1,1),(1,2),(2,3),(3,4),(2,4),(1,2),(1,2),(1,2),(2,3),(3,4),(2,4)])pos=nx.spring_layout(G)ax=plt.gca()draw_network(G,pos,ax)ax.autoscale()plt.axis('相等')plt.axis('关闭')plt.show()

解决方案

看来你的方法使用 matplotlib 相当先进,但我仍然建议使用专门的图形绘图库(

请注意,您还可以从 ipython shell 中轻松调用绘图:https://stackoverflow.com/a/14945560

I have this function and I want to draw a self loop. How can I do that?
The edge exists but I think it's just a point in this exemple is (1,1) and I couldn't add the name of nodes. My goal is from adjacency matrix draw a graph. Is there there is better way to do this?

import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch, Circle
import numpy as np

def draw_network(G,pos,ax,sg=None):

    for n in G:
        c=Circle(pos[n],radius=0.05,alpha=0.7)
        ax.add_patch(c)
        G.node[n]['patch']=c
        x,y=pos[n]
    seen={}
    for (u,v,d) in G.edges(data=True):
        n1=G.node[u]['patch']
        n2=G.node[v]['patch']
        rad=0.1
        if (u,v) in seen:
            rad=seen.get((u,v))
            rad=(rad+np.sign(rad)*0.1)*-1
        alpha=0.5
        color='k'

        e = FancyArrowPatch(n1.center,n2.center,patchA=n1,patchB=n2,
                            arrowstyle='-|>',
                            connectionstyle='arc3,rad=%s'%rad,
                            mutation_scale=10.0,
                            lw=2,
                            alpha=alpha,
                            color=color)
        seen[(u,v)]=rad
        ax.add_patch(e)
    return e


G=nx.MultiDiGraph([(1,2),(1,1),(1,2),(2,3),(3,4),(2,4),
                (1,2),(1,2),(1,2),(2,3),(3,4),(2,4)]
                )

pos=nx.spring_layout(G)
ax=plt.gca()
draw_network(G,pos,ax)
ax.autoscale()
plt.axis('equal')
plt.axis('off')

plt.show()

解决方案

It seems that your approach is quite advanced a use of matplotlib, but I would still recommend using a specialized graph plotting library (as does the networkx documentation(. As graphs get bigger, more problems arise -- but problems that have already been solved in those libraries.

A "go-to" option is graphviz, which handles drawing multi-graphs reasonably well. You can write dot files from networkx graphs, and then use one of the graph drawing tools (e.g. dot, neato, etc).

Here is an example, building on graph attributes and multigraph edge attributes:

import networkx as nx
from networkx.drawing.nx_agraph import to_agraph 

# define the graph as per your question
G=nx.MultiDiGraph([(1,2),(1,1),(1,2),(2,3),(3,4),(2,4), 
    (1,2),(1,2),(1,2),(2,3),(3,4),(2,4)])

# add graphviz layout options (see https://stackoverflow.com/a/39662097)
G.graph['edge'] = {'arrowsize': '0.6', 'splines': 'curved'}
G.graph['graph'] = {'scale': '3'}

# adding attributes to edges in multigraphs is more complicated but see
# https://stackoverflow.com/a/26694158                    
G[1][1][0]['color']='red'

A = to_agraph(G) 
A.layout('dot')                                                                 
A.draw('multi.png')   

Note that you can also easily invoke the drawing from within an ipython shell: https://stackoverflow.com/a/14945560

这篇关于Matplotlib 和 Networkx - 绘制自循环节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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