def compute_edge_colors(G): """ Compute the edge colors. NOTE: This function is copied from the nxviz package! """ data = [G.graph.edges[n][G.edge_color] for n in G.edges] data_reduced = sorted(list(set(data))) dtype = infer_data_type(data) n_grps = num_discrete_groups(data) if dtype == "categorical" or dtype == "ordinal": if n_grps <= 8: cmap = get_cmap(cmaps["Accent_{0}".format(n_grps)].mpl_colormap) else: cmap = n_group_colorpallet(n_grps) elif dtype == "continuous" and not is_data_diverging(data): cmap = get_cmap(cmaps["weights"]) for d in data: idx = data_reduced.index(d) / n_grps G.edge_colors.append(cmap(idx)) # Add colorbar if required. if len(data_reduced) > 1 and dtype == "continuous": G.sm = plt.cm.ScalarMappable( cmap=cmap, norm=plt.Normalize( vmin=min(data_reduced), vmax=max(data_reduced), ), ) G.sm._A = [] return cmap
def test_num_discrete_groups(): assert num_discrete_groups(categorical) == 3 assert num_discrete_groups(ordinal) == 5