在python中绘制自定义图 [英] Drawing a custom diagram in python

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

问题描述

我想画这样的东西:

我能找到的最接近的东西是NetworkX Edge Colormap:

The closest thing to this I could find was NetworkX Edge Colormap:

http://networkx.github.io/documentation/latest /examples/drawing/edge_colormap.html

这是源代码:

    #!/usr/bin/env python
"""
Draw a graph with matplotlib, color edges.
You must have matplotlib>=87.7 for this to work.
"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
try:
    import matplotlib.pyplot as plt
except:
    raise

import networkx as nx

G=nx.star_graph(20)
pos=nx.spring_layout(G)
colors=range(20)
nx.draw(G,pos,node_color='#A0CBE2',edge_color=colors,width=4,edge_cmap=plt.cm.Blues,with_labels=False)
plt.savefig("edge_colormap.png") # save as png
plt.show() # display

在试用了源代码之后,我不知道如何对边缘圆与中心的距离进行硬编码.现在是随机的.

After playing around with their source code, I can't figure out how to hardcode distance of the edge circles from the centre. Right now its random.

我还如何标记边缘圆及其与中心的距离?

我知道位置来自pos=nx.spring_layout(G).因此,我查看了spring_layout属性,发现可以通过使用pos变量来指定位置,该变量是一个字典,以节点为键,值作为列表. ( https://networkx.github.io /documentation/latest/reference/generation/networkx.drawing.layout.spring_layout.html )

I know for position comes from pos=nx.spring_layout(G). So I looked at the spring_layout attribute and found that position can be specified by using a pos variable which is a dictionary with nodes as keys and values as a list. (https://networkx.github.io/documentation/latest/reference/generated/networkx.drawing.layout.spring_layout.html)

但是即使执行以下操作,结果也是随机边缘:

But even when I do the following result is random edges :

ap = {'uniwide':[55,34,1],'eduram':[34],'uniwide_webauth':[20,55,39],'uniwide_guest':[55,34],'tele9751_lab':[100],'HomeSDN':[100],'TP-LINK':[39]}

pos=nx.spring_layout(G,pos=ap)  

推荐答案

您可以使用pos字典显式设置节点位置. 例如

You can set the node positions explicitly with the pos dictionary. For example

import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edge('center',1)
G.add_edge('center',2)
G.add_edge('center',3)
G.add_edge('center',4)

pos = {'center':(0,0),
       1:(1,0),
       2:(0,1),
       3:(-1,0),
       4:(0,-1)
       }

nx.draw(G, pos=pos, with_labels=True)
plt.show()

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

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