如何在networkx中绘制inf值?

问题描述 投票:0回答:1

我正在尝试用networkx绘制图形G,其中边缘颜色由先前分配给每个边缘的参数parameter给出。一些边的参数值是inf。我认为这是使用指定颜色条的最低颜色绘制的。

我想用另一种颜色的inf值绘制边缘,例如红色。这可能吗?如果是的话,我该怎么做呢?

这是我的代码的相关部分:

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)

寻找解决方案,我发现this回答关于使用matplotlib的imshow绘制NaNs。我尝试添加一些东西

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

但它似乎没有改变任何东西。

根据@michaelg和@Bonlenfum的评论,我尝试将inf值转换为NaN。不幸的是,我链接的技术也没有在这里工作。我是否必须添加除上述两行代码之外的任何内容?

根据请求,这是我使用的数据。我不知道如何摆脱不必要的参数,我希望这不是问题。

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}}}

这是dict_of_dict格式,可以通过G = networkx.from_dict_of_dicts(d)读取

节点位置是:

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)}
python matplotlib networkx colormap
1个回答
0
投票

在尝试了很多不同的事情之后,我发现当涉及到nan值时,colormap的行为实际上不受set_bad()例程的约束,而是受set_under()控制。因此,在保存绘图颜色之前添加这两行代码会使所有nan值边缘变为红色:

cmap = copy(plt.cm.viridis) # choose whichever colormap you like here
cmap.set_under('r', 1.0)
© www.soinside.com 2019 - 2024. All rights reserved.