0

I'm trying to draw a graph G with networkx, where the edge colors are given by the parameter parameter previously assigned to every edge. Some of the edges' parameter value is inf. This is drawn using the lowest color of the specified colorbar, I think.

I'd like to draw edges with inf value in another color, e.g. red. Is this possible? If yes, how would I go about it?

Here's the relevant part of my code:

def plot_graph_colored_edges(G, node_positions, parameter):
    '''
        plot graph G with node positions node_positions,
        color edges according to their parameter value
    '''
    # calculate range for assigning colors to edges
    param_list = []
    for edge in nx.to_edgelist(G):
        param_list.append(edge[2][parameter])
    vmin = min(param_list)
    vmax = max(param_list)

    # choose color map
    cmap = plt.cm.Greens

    # draw graph
    plt.clf()
    # workaround to create colorbar, since nx.draw_networkx returns None...
    sm = plt.cm.ScalarMappable(cmap=cmap,
                               norm=plt.Normalize(vmin=vmin, vmax=vmax))
    sm._A = []
    plt.colorbar(sm)

    # draw actual graph
    nx.draw(
            G,
            pos=node_positions,
            node_size=1,
            node_color='black',
            with_labels=False,
            edge_color=param_list,
            edge_cmap=cmap,
            edge_vmin=vmin,
            edge_vmax=vmax)

Searching for a solution, I found this answer about plotting NaNs with matplotlib's imshow. I tried adding in something along the lines of

    current_cmap = plt.cm.get_cmap()
    current_cmap.set_bad(color='red')

but it doesn't seem to change anything.

Based on comments by @michaelg and @Bonlenfum I tried converting the inf values to NaN. Unfortunately, the technique I linked did not work here either. Do I have to add anything but the above two lines of code?

Per request, here is the data I use. I didn't know how to get rid of the unneeded parameters, I hope that's not a problem.

d = {1.0: {2.0: {'reactance': 0.059999999999999998, 'eta': 0.83909678373550689, 'flow': -9.1694698723315451, 'b_over_lambda': inf, 'weight': 16.666666666666668}, 3.0: {'reactance': 0.19, 'eta': 0.49047314849577706, 'flow': -14.360530127668513, 'b_over_lambda': inf, 'weight': 5.2631578947368425}}, 2.0: {1.0: {'reactance': 0.059999999999999998, 'eta': 0.83909678373550689, 'flow': -9.1694698723315451, 'b_over_lambda': inf, 'weight': 16.666666666666668}, 4.0: {'reactance': 0.17000000000000001, 'eta': 0.37921956001532092, 'flow': -15.627963158963901, 'b_over_lambda': inf, 'weight': 5.8823529411764701}, 5.0: {'reactance': 0.20000000000000001, 'eta': 0.58355218282638743, 'flow': -13.327708980010653, 'b_over_lambda': inf, 'weight': 5.0}, 6.0: {'reactance': 0.17999999999999999, 'eta': 0.37134303478394659, 'flow': -19.483797733356994, 'b_over_lambda': inf, 'weight': 5.5555555555555554}}, 3.0: {1.0: {'reactance': 0.19, 'eta': 0.49047314849577706, 'flow': -14.360530127668513, 'b_over_lambda': inf, 'weight': 5.2631578947368425}, 4.0: {'reactance': 0.040000000000000001, 'eta': 0.89273118915700589, 'flow': -11.960530127668466, 'b_over_lambda': inf, 'weight': 25.0}}, 4.0: {2.0: {'reactance': 0.17000000000000001, 'eta': 0.37921956001532092, 'flow': -15.627963158963901, 'b_over_lambda': inf, 'weight': 5.8823529411764701}, 3.0: {'reactance': 0.040000000000000001, 'eta': 0.89273118915700589, 'flow': -11.960530127668466, 'b_over_lambda': inf, 'weight': 25.0}, 12.0: {'reactance': 0.26000000000000001, 'eta': 0.61403349336337587, 'flow': 1.2697530878775052, 'b_over_lambda': 3.0290565015147966, 'weight': 3.8461538461538458}, 6.0: {'reactance': 0.040000000000000001, 'eta': 0.80940687806357203, 'flow': -21.258246374509891, 'b_over_lambda': inf, 'weight': 25.0}}, 5.0: {2.0: {'reactance': 0.20000000000000001, 'eta': 0.58355218282638743, 'flow': -13.327708980010653, 'b_over_lambda': inf, 'weight': 5.0}, 7.0: {'reactance': 0.12, 'eta': 0.75013130969583208, 'flow': -13.32770898001063, 'b_over_lambda': inf, 'weight': 8.3333333333333339}}, 6.0: {2.0: {'reactance': 0.17999999999999999, 'eta': 0.37134303478394659, 'flow': -19.483797733356994, 'b_over_lambda': inf, 'weight': 5.5555555555555554}, 4.0: {'reactance': 0.040000000000000001, 'eta': 0.80940687806357203, 'flow': -21.258246374509891, 'b_over_lambda': inf, 'weight': 25.0}, 7.0: {'reactance': 0.080000000000000002, 'eta': 0.83342087313055435, 'flow': -9.4722910199893366, 'b_over_lambda': inf, 'weight': 12.5}, 8.0: {'reactance': 0.040000000000000001, 'eta': 0.86541457345199835, 'flow': -24.745577899639326, 'b_over_lambda': 24.565475998899824, 'weight': 25.0}, 9.0: {'reactance': 0.20999999999999999, 'eta': 0.61668495900560116, 'flow': -4.7993677388878737, 'b_over_lambda': inf, 'weight': 4.7619047619047619}, 10.0: {'reactance': 0.56000000000000005, 'eta': 0.2376576513790267, 'flow': -2.7424958507930732, 'b_over_lambda': inf, 'weight': 1.7857142857142856}, 28.0: {'reactance': 0.059999999999999998, 'eta': 0.76994976284796102, 'flow': 1.0176884014427254, 'b_over_lambda': 16.37698399926655, 'weight': 16.666666666666668}}, 7.0: {5.0: {'reactance': 0.12, 'eta': 0.75013130969583208, 'flow': -13.32770898001063, 'b_over_lambda': inf, 'weight': 8.3333333333333339}, 6.0: {'reactance': 0.080000000000000002, 'eta': 0.83342087313055435, 'flow': -9.4722910199893366, 'b_over_lambda': inf, 'weight': 12.5}}, 8.0: {28.0: {'reactance': 0.20000000000000001, 'eta': 0.32707286725999196, 'flow': 5.2544221003606832, 'b_over_lambda': 0.95157943242831233, 'weight': 5.0}, 6.0: {'reactance': 0.040000000000000001, 'eta': 0.86541457345199835, 'flow': -24.745577899639326, 'b_over_lambda': 24.565475998899824, 'weight': 25.0}}, 9.0: {10.0: {'reactance': 0.11, 'eta': 0.79921593090769583, 'flow': -4.7993677388878861, 'b_over_lambda': inf, 'weight': 9.0909090909090917}, 11.0: {'reactance': 0.20999999999999999, 'eta': 0.99999999999999956, 'flow': -1.691768418476429e-14, 'b_over_lambda': inf, 'weight': 4.7619047619047619}, 6.0: {'reactance': 0.20999999999999999, 'eta': 0.61668495900560116, 'flow': -4.7993677388878737, 'b_over_lambda': inf, 'weight': 4.7619047619047619}}, 10.0: {17.0: {'reactance': 0.080000000000000002, 'eta': 0.88587066598343633, 'flow': -3.1781631185688246, 'b_over_lambda': inf, 'weight': 12.5}, 20.0: {'reactance': 0.20999999999999999, 'eta': 0.75604815509229739, 'flow': -5.5634585757720183, 'b_over_lambda': inf, 'weight': 4.7619047619047619}, 21.0: {'reactance': 0.070000000000000007, 'eta': 0.66908555958681726, 'flow': 2.916515482079026, 'b_over_lambda': 2.040886852390539, 'weight': 14.285714285714285}, 6.0: {'reactance': 0.56000000000000005, 'eta': 0.2376576513790267, 'flow': -2.7424958507930732, 'b_over_lambda': inf, 'weight': 1.7857142857142856}, 9.0: {'reactance': 0.11, 'eta': 0.79921593090769583, 'flow': -4.7993677388878861, 'b_over_lambda': inf, 'weight': 9.0909090909090917}, 22.0: {'reactance': 0.14999999999999999, 'eta': 0.34472314596697323, 'flow': 4.083242622580765, 'b_over_lambda': 0.95241386444891818, 'weight': 6.666666666666667}}, 11.0: {9.0: {'reactance': 0.20999999999999999, 'eta': 0.99999999999999956, 'flow': -1.691768418476429e-14, 'b_over_lambda': inf, 'weight': 4.7619047619047619}}, 12.0: {16.0: {'reactance': 0.20000000000000001, 'eta': 0.7146766649585925, 'flow': -9.3218368814312171, 'b_over_lambda': inf, 'weight': 5.0}, 4.0: {'reactance': 0.26000000000000001, 'eta': 0.61403349336337587, 'flow': 1.2697530878775052, 'b_over_lambda': 3.0290565015147966, 'weight': 3.8461538461538458}, 13.0: {'reactance': 0.14000000000000001, 'eta': 1.0, 'flow': 37.000000000000021, 'b_over_lambda': 0.19305019305019291, 'weight': 7.1428571428571423}, 14.0: {'reactance': 0.26000000000000001, 'eta': 0.53969541277500099, 'flow': -5.4527005152370398, 'b_over_lambda': inf, 'weight': 3.8461538461538458}, 15.0: {'reactance': 0.13, 'eta': 0.65679140068610276, 'flow': -9.755709515454182, 'b_over_lambda': inf, 'weight': 7.6923076923076916}}, 13.0: {12.0: {'reactance': 0.14000000000000001, 'eta': 1.0, 'flow': 37.000000000000021, 'b_over_lambda': 0.19305019305019291, 'weight': 7.1428571428571423}}, 14.0: {12.0: {'reactance': 0.26000000000000001, 'eta': 0.53969541277500099, 'flow': -5.4527005152370398, 'b_over_lambda': inf, 'weight': 3.8461538461538458}, 15.0: {'reactance': 0.20000000000000001, 'eta': 0.6459195482884621, 'flow': 0.74729948476293384, 'b_over_lambda': 6.6907579918727658, 'weight': 5.0}}, 15.0: {18.0: {'reactance': 0.22, 'eta': 0.74443140057288215, 'flow': -9.3365414242279687, 'b_over_lambda': inf, 'weight': 4.5454545454545459}, 12.0: {'reactance': 0.13, 'eta': 0.65679140068610276, 'flow': -9.755709515454182, 'b_over_lambda': inf, 'weight': 7.6923076923076916}, 14.0: {'reactance': 0.20000000000000001, 'eta': 0.6459195482884621, 'flow': 0.74729948476293384, 'b_over_lambda': 6.6907579918727658, 'weight': 5.0}, 23.0: {'reactance': 0.20000000000000001, 'eta': 0.7681958897824761, 'flow': 8.5281313935367535, 'b_over_lambda': 0.58629490673529827, 'weight': 5.0}}, 16.0: {17.0: {'reactance': 0.19, 'eta': 0.72894283171066321, 'flow': -5.8218368814312145, 'b_over_lambda': inf, 'weight': 5.2631578947368425}, 12.0: {'reactance': 0.20000000000000001, 'eta': 0.7146766649585925, 'flow': -9.3218368814312171, 'b_over_lambda': inf, 'weight': 5.0}}, 17.0: {16.0: {'reactance': 0.19, 'eta': 0.72894283171066321, 'flow': -5.8218368814312145, 'b_over_lambda': inf, 'weight': 5.2631578947368425}, 10.0: {'reactance': 0.080000000000000002, 'eta': 0.88587066598343633, 'flow': -3.1781631185688246, 'b_over_lambda': inf, 'weight': 12.5}}, 18.0: {19.0: {'reactance': 0.13, 'eta': 0.84898219124761187, 'flow': -6.1365414242279579, 'b_over_lambda': inf, 'weight': 7.6923076923076916}, 15.0: {'reactance': 0.22, 'eta': 0.74443140057288215, 'flow': -9.3365414242279687, 'b_over_lambda': inf, 'weight': 4.5454545454545459}}, 19.0: {18.0: {'reactance': 0.13, 'eta': 0.84898219124761187, 'flow': -6.1365414242279579, 'b_over_lambda': inf, 'weight': 7.6923076923076916}, 20.0: {'reactance': 0.070000000000000007, 'eta': 0.91868271836409798, 'flow': 3.3634585757719888, 'b_over_lambda': 4.2473287432818747, 'weight': 14.285714285714285}}, 20.0: {10.0: {'reactance': 0.20999999999999999, 'eta': 0.75604815509229739, 'flow': -5.5634585757720183, 'b_over_lambda': inf, 'weight': 4.7619047619047619}, 19.0: {'reactance': 0.070000000000000007, 'eta': 0.91868271836409798, 'flow': 3.3634585757719888, 'b_over_lambda': 4.2473287432818747, 'weight': 14.285714285714285}}, 21.0: {10.0: {'reactance': 0.070000000000000007, 'eta': 0.66908555958681726, 'flow': 2.916515482079026, 'b_over_lambda': 2.040886852390539, 'weight': 14.285714285714285}, 22.0: {'reactance': 0.02, 'eta': 0.90545301702480707, 'flow': 20.416515482079145, 'b_over_lambda': 2.1428849118147109, 'weight': 50.0}}, 22.0: {24.0: {'reactance': 0.17999999999999999, 'eta': 0.74163751225150298, 'flow': 2.9097581046598764, 'b_over_lambda': 1.9092843307691201, 'weight': 5.5555555555555554}, 10.0: {'reactance': 0.14999999999999999, 'eta': 0.34472314596697323, 'flow': 4.083242622580765, 'b_over_lambda': 0.95241386444891818, 'weight': 6.666666666666667}, 21.0: {'reactance': 0.02, 'eta': 0.90545301702480707, 'flow': 20.416515482079145, 'b_over_lambda': 2.1428849118147109, 'weight': 50.0}}, 23.0: {24.0: {'reactance': 0.27000000000000002, 'eta': 0.68706445120634296, 'flow': -7.4718686064632687, 'b_over_lambda': inf, 'weight': 3.7037037037037033}, 15.0: {'reactance': 0.20000000000000001, 'eta': 0.7681958897824761, 'flow': 8.5281313935367535, 'b_over_lambda': 0.58629490673529827, 'weight': 5.0}}, 24.0: {25.0: {'reactance': 0.33000000000000002, 'eta': 0.7417557744746629, 'flow': 4.1378894981965759, 'b_over_lambda': 0.73233058341063306, 'weight': 3.0303030303030303}, 22.0: {'reactance': 0.17999999999999999, 'eta': 0.74163751225150298, 'flow': 2.9097581046598764, 'b_over_lambda': 1.9092843307691201, 'weight': 5.5555555555555554}, 23.0: {'reactance': 0.27000000000000002, 'eta': 0.68706445120634296, 'flow': -7.4718686064632687, 'b_over_lambda': inf, 'weight': 3.7037037037037033}}, 25.0: {24.0: {'reactance': 0.33000000000000002, 'eta': 0.7417557744746629, 'flow': 4.1378894981965759, 'b_over_lambda': 0.73233058341063306, 'weight': 3.0303030303030303}, 26.0: {'reactance': 0.38, 'eta': 0.99999999999999922, 'flow': -3.4999999999999889, 'b_over_lambda': inf, 'weight': 2.6315789473684212}, 27.0: {'reactance': 0.20999999999999999, 'eta': 0.83566276557478492, 'flow': 7.6378894981965928, 'b_over_lambda': 0.6234581899920274, 'weight': 4.7619047619047619}}, 26.0: {25.0: {'reactance': 0.38, 'eta': 0.99999999999999922, 'flow': -3.4999999999999889, 'b_over_lambda': inf, 'weight': 2.6315789473684212}}, 27.0: {25.0: {'reactance': 0.20999999999999999, 'eta': 0.83566276557478492, 'flow': 7.6378894981965928, 'b_over_lambda': 0.6234581899920274, 'weight': 4.7619047619047619}, 28.0: {'reactance': 0.40000000000000002, 'eta': 0.68697669633292435, 'flow': -6.2721105018034065, 'b_over_lambda': inf, 'weight': 2.5}, 29.0: {'reactance': 0.41999999999999998, 'eta': 0.71428571428571319, 'flow': -6.0408163265306021, 'b_over_lambda': inf, 'weight': 2.3809523809523809}, 30.0: {'reactance': 0.59999999999999998, 'eta': 0.59183673469387654, 'flow': -6.9591836734693775, 'b_over_lambda': inf, 'weight': 1.6666666666666667}}, 28.0: {8.0: {'reactance': 0.20000000000000001, 'eta': 0.32707286725999196, 'flow': 5.2544221003606832, 'b_over_lambda': 0.95157943242831233, 'weight': 5.0}, 27.0: {'reactance': 0.40000000000000002, 'eta': 0.68697669633292435, 'flow': -6.2721105018034065, 'b_over_lambda': inf, 'weight': 2.5}, 6.0: {'reactance': 0.059999999999999998, 'eta': 0.76994976284796102, 'flow': 1.0176884014427254, 'b_over_lambda': 16.37698399926655, 'weight': 16.666666666666668}}, 29.0: {27.0: {'reactance': 0.41999999999999998, 'eta': 0.71428571428571319, 'flow': -6.0408163265306021, 'b_over_lambda': inf, 'weight': 2.3809523809523809}, 30.0: {'reactance': 0.45000000000000001, 'eta': 0.69387755102040727, 'flow': -3.6408163265306075, 'b_over_lambda': inf, 'weight': 2.2222222222222223}}, 30.0: {27.0: {'reactance': 0.59999999999999998, 'eta': 0.59183673469387654, 'flow': -6.9591836734693775, 'b_over_lambda': inf, 'weight': 1.6666666666666667}, 29.0: {'reactance': 0.45000000000000001, 'eta': 0.69387755102040727, 'flow': -3.6408163265306075, 'b_over_lambda': inf, 'weight': 2.2222222222222223}}}

This is in dict_of_dict format and can be read in via G = networkx.from_dict_of_dicts(d)

the node positions are:

node_positions = {1.0: (1300.0, 3513.0), 2.0: (1400.0, 4700.0), 3.0: (1609.0, 3897.0), 4.0: (2321.0, 3831.0), 5.0: (3889.0, 4873.0), 6.0: (3869.0, 3965.0), 7.0: (3865.0, 4397.0), 8.0: (4700.0, 3565.0), 9.0: (3600.0, 3526.0), 10.0: (3881.0, 3070.0), 11.0: (3300.0, 3522.0), 12.0: (2381.0, 2934.0), 13.0: (2073.0, 3081.0), 14.0: (1737.0, 1993.0), 15.0: (2321.0, 1977.0), 16.0: (2861.0, 2649.0), 17.0: (3381.0, 3037.0), 18.0: (2905.0, 1969.0), 19.0: (3245.0, 2241.0), 20.0: (3417.0, 2517.0), 21.0: (3685.0, 2273.0), 22.0: (4017.0, 2193.0), 23.0: (3029.0, 1684.0), 24.0: (3789.0, 1508.0), 25.0: (3905.0, 1192.0), 26.0: (3225.0, 1152.0), 27.0: (4025.0, 808.0), 28.0: (4600.0, 772.0), 29.0: (3917.0, 324.0), 30.0: (3053.0, 316.0)}
prazuber
  • 1,352
  • 10
  • 26
newtroks
  • 1
  • 3
  • Apparently, it is not possible to draw a graph with `inf` value as edge weight. Could you replace it with some finite values? – michaelg Mar 20 '18 at 12:47
  • without code to produce a sample graph, it is hard to answer definitively. See https://stackoverflow.com/help/mcve for guidance on what to include in questions. Having said that, a quick look at the code for [draw_networkx_edges](https://networkx.github.io/documentation/stable/_modules/networkx/drawing/nx_pylab.html#draw_networkx_edges) it looks like `edge_cmap` is only used if `edge_color` is **not** passed as a full specification for each edge. I might have read the logic wrongly though – Bonlenfum Mar 20 '18 at 17:28
  • something else to test is simply changing any occurance of np.inf for np.nan in param_list, as suggested by @michaelg, which would then presumably allow you to use the technique you linked – Bonlenfum Mar 20 '18 at 17:29

1 Answers1

0

After trying a lot of different things, I found out that the behavior of the colormap when it comes to nan values is actually not governed by the set_bad() routine but rather by set_under(). Thus, adding these two lines of code before saving the plot colors all nan-valued edges red:

cmap = copy(plt.cm.viridis) # choose whichever colormap you like here
cmap.set_under('r', 1.0)
newtroks
  • 1
  • 3