python自定义规律绘制,在python中绘制自定义图

I wanna draw something like this :

0e3880ab13d1b8c8d4826ed910e4f02f.png

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

and here is the source code:

#!/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.

Also how do I label the edge circles and their distance from the centre?

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)

解决方案

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

a807fe848b72f7537dcf1b2edd85e644.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值