python - 在 python 中绘制自定义图表

标签 python matplotlib networkx

我想画这样的东西:

我能找到的最接近的东西是 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 (<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="046c6563666176634468656a682a636b72" rel="noreferrer noopener nofollow">[email protected]</a>)"""
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

在研究了他们的源代码之后,我不知道如何对边缘圆与中心的距离进行硬编码。现在它是随机的。

另外,如何标记边缘圆及其距中心的距离?

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

但即使我这样做,以下结果也是随机边缘:

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 字典显式设置节点位置。 例如

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

enter image description here

关于python - 在 python 中绘制自定义图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318677/

相关文章:

python - strftime 函数 pandas - Python

python - 找不到文件错误 : [WinError 2] The system cannot find the file specified:

python - 使用 Networkx 在图形的 matplotlib 上高效制作动画

python - NetworkX中节点的上行和下行

python - pandas - 如何合并 DataFrame 中的选定行

python - python实现中的梯度下降问题

python - 尝试部署 django 时出现静态文件问题

python - 是否可以在 matplotlib 中为边缘颜色分配颜色图?

python - pylab.show() 忘记了之前的图

python - 使用 node2vec 创建嵌入