def get_edge_colors_by_attr(G, attr, num_bins=5, cmap='viridis', start=0, stop=1):
"""
Get a list of edge colors by binning some continuous-variable attribute into
quantiles.
Parameters
----------
G : networkx multidigraph
attr : string
the name of the continuous-variable attribute
num_bins : int
how many quantiles
cmap : string
name of a colormap
start : float
where to start in the colorspace
stop : float
where to end in the colorspace
Returns
-------
list
"""
if num_bins is None:
num_bins=len(G.edges())
bin_labels = range(num_bins)
attr_values = pd.Series([data[attr] for u, v, key, data in G.edges(keys=True, data=True)])
cats = pd.qcut(x=attr_values, q=num_bins, labels=bin_labels)
colors = get_colors(num_bins, cmap, start, stop)
edge_colors = [colors[cat] for cat in cats]
return edge_colors
评论列表
文章目录