基于networkx中边缘颜色的图例 [英] Legend based on edge color in networkx

查看:795
本文介绍了基于networkx中边缘颜色的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在networkx中基于边缘颜色(而不是节点颜色)创建图例?

Is there a way to create a legend in networkx based on edge color (as opposed to by node color)?

这是我的图

plt.figure(figsize = (15, 10))
G = nx.from_pandas_dataframe(df, 'From', 'To', ['Order', 'Colors'])
edge_labels = nx.get_edge_attributes(G, 'Order')
nx.draw_networkx(G, with_labels = False, node_color = 'black', alpha = 0.5, node_size = 3, linewidths = 1, edge_color = df['Colors'], edge_cmap = 
plt.cm.Set2)
plt.show()

在这种情况下,['Order']是边缘的描述符,而['Color']是映射到['Order']中每个值的唯一整数,后者正在根据Set2颜色图创建边缘颜色.

In this, ['Order'] is a descriptor of the edge and ['Color'] is a unique integer mapped to each value in ['Order'], which is working to create the edge colors based on the Set2 colormap.

我可以通过以下方式获得边缘标签: edge_labels = nx.get_edge_attributes(G, 'Order') 但是我怎么把它变成一个传奇?

I can get the edge labels with something like: edge_labels = nx.get_edge_attributes(G, 'Order') but how do I put this into a legend?

如果有帮助,我很乐意分享数据并完成代码!

I'm happy to share the data and complete code if helpful!

推荐答案

一种实现此目的的方法是这样的答案对图例中LineCollection的每个成员使用代理.

One way you can do it is in the spirit of this SO answer which uses proxies for each member of a LineCollection in the legend.

您可以通过使用逐步图形绘制功能来获得LineCollection,分别绘制节点和边缘(例如

You can get the LineCollection by using the step-by-step graph drawing functions, drawing the nodes and edges separately (e.g. draw_networkx_nodes doc.)

import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.lines import Line2D

# make a test graph
n = 7 # nodes
m = 5 # edges
G = nx.gnm_random_graph(n, m)
# and define some color strings (you'll get this array from the dataframe)
_c = 'rgbcmky' * m # way too many colors, trim after
clrs = [c for c in _c[:m]]

plt.ion()

plt.figure(figsize = (9, 7), num=1); plt.clf()
# draw the graph in several steps
pos = nx.spring_layout(G)
h1 = nx.draw_networkx_nodes(G, pos=pos, node_color = 'black',
                            alpha = 0.9, node_size = 300, linewidths=6)
# we need the LineCollection of the edges to produce the legend (h2)
h2 = nx.draw_networkx_edges(G, pos=pos, width=6, edge_color=clrs)

# and just show the node labels to check the labels are right!
h3 = nx.draw_networkx_labels(G, pos=pos, font_size=20, font_color='c')


#https://stackoverflow.com/questions/19877666/add-legends-to-linecollection-plot - uses plotted data to define the color but here we already have colors defined, so just need a Line2D object.
def make_proxy(clr, mappable, **kwargs):
    return Line2D([0, 1], [0, 1], color=clr, **kwargs)

# generate proxies with the above function
proxies = [make_proxy(clr, h2, lw=5) for clr in clrs]
# and some text for the legend -- you should use something from df.
labels = ["{}->{}".format(fr, to) for (fr, to) in G.edges()]
plt.legend(proxies, labels)

plt.show()

这将产生类似以下内容的内容:

This produces something like:

这篇关于基于networkx中边缘颜色的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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