【问题标题】:legend in python networkxpython networkx中的图例
【发布时间】:2014-04-10 15:10:53
【问题描述】:

我有以下代码来绘制带节点的图形,但我未能添加正确的图例: (对不起,我无法发布图片,似乎我没有足够的声誉)

我想要一个有 4 种颜色的图例,例如“浅蓝色 = 过时,红色 = 草稿,黄色 = 重新发布,深蓝色 = 初始化”。

我见过一些“分散”的解决方案,但我认为它太复杂了。有没有办法用plt.legend(G.nodes) 做到这一点?

代码如下:

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
G=nx.Graph()
G.add_node("kind1")
G.add_node("kind2")
G.add_node("Obsolete")
G.add_node("Draft")
G.add_node("Release")
G.add_node("Initialisation")
val_map = {'kind1': 2,'kind2': 2,'Obsolete': 2,'Initialisation': 1,'Draft': 4,'Release': 3}
values = [val_map.get(node, 0) for node in G.nodes()]
nodes = nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values)

plt.legend(G.nodes())
plt.show() 

【问题讨论】:

    标签: python legend networkx


    【解决方案1】:

    您在使用nx.draw 时似乎出现了某种错误。尝试改用nx.draw_networkx。 然后在绘制图形时使用来自matplotlib 的轴来传递它。该轴应包含节点的标签和颜色,同时在 (0,0) 中绘制一个点 --> 这是棘手的部分。

    希望对您有所帮助!这是我运行的代码:

    import networkx as nx
    import matplotlib.pyplot as plt
    import numpy as np
    # For color mapping
    import matplotlib.colors as colors
    import matplotlib.cm as cmx
    
    G=nx.Graph()
    G.add_node("kind1")
    G.add_node("kind2")
    G.add_node("Obsolete")
    G.add_node("Draft")
    G.add_node("Release")
    G.add_node("Initialisation")
    
    # You were missing the position.
    pos=nx.spring_layout(G)
    val_map = {'kind1': 2, 
               'kind2': 2, 
               'Obsolete': 2, 
               'Initialisation': 1, 
               'Draft': 4, 
               'Release': 3}
    values = [val_map.get(node, 0) for node in G.nodes()]
    # Color mapping
    jet = cm = plt.get_cmap('jet')
    cNorm  = colors.Normalize(vmin=0, vmax=max(values))
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)
    
    # Using a figure to use it as a parameter when calling nx.draw_networkx
    f = plt.figure(1)
    ax = f.add_subplot(1,1,1)
    for label in val_map:
        ax.plot([0],[0],
                color=scalarMap.to_rgba(val_map[label]),
                label=label)
    
    # Just fixed the color map
    nx.draw_networkx(G,pos, cmap=jet, vmin=0, vmax=max(values),
                     node_color=values,
                     with_labels=False, ax=ax)
    
    # Here is were I get an error with your code                                                                                                                         
    #nodes = nx.draw(G, cmap=plt.get_cmap('jet'), node_color=values)                                                                             
    
    # Setting it to how it was looking before.                                                                                                              
    plt.axis('off')
    f.set_facecolor('w')
    
    plt.legend(loc='center')
    
    f.tight_layout()
    plt.show()
    

    一些有用的资源:

    1. http://pydoc.net/Python/networkx/1.0.1/networkx.drawing.nx_pylab/
    2. http://matplotlib.org/api/legend_api.html
    3. Using Colormaps to set color of line in matplotlib
    4. http://matplotlib.org/1.3.1/users/artists.html

    【讨论】:

    • 我还有一个问题。每次我绘制时,节点的位置都会改变。但是我想要节点的恒定位置(我不在乎每个节点的位置,我只希望如果我绘制几倍图,节点保持在同一位置)。 “pos=nx.spring_layout(G)”中是否有要添加的参数?我查看了networkx.github.io/documentation/latest/reference/generated/…,但没有看到可以作为解决方案的参数。谢谢你的帮助!!!
    • 当然。我想如果你使用 circle_layout 而不是 spring_layout 你会得到一些不变的东西。你可以在这里查看布局:networkx.github.io/documentation/latest/reference/drawing.html
    【解决方案2】:

    非常感谢您的帮助,但这并不是我想要的。我做了一些更改,所以我可以让颜色图例的名称与节点的名称不同。

    这是最终代码:

    import networkx as nx
    import matplotlib.pyplot as plt
    import numpy as np
    # For color mapping
    import matplotlib.colors as colors
    import matplotlib.cm as cmx
    
    G=nx.Graph()
    G.add_node("kind1")
    G.add_node("kind2")
    G.add_node("kind3")
    G.add_node("kind4")
    G.add_node("kind5")
    G.add_node("kind6")
    
    # You were missing the position.
    pos=nx.spring_layout(G)
    val_map = {'kind1': 2,'kind2': 2,'kind3': 2,'kind4': 1,'kind5':4,'kind6': 3}
    #I had this list for the name corresponding t the color but different from the node name
    ColorLegend = {'Obsolete': 2,'Initialisation': 1,'Draft': 4,'Release': 3}
    values = [val_map.get(node, 0) for node in G.nodes()]
    # Color mapping
    jet = cm = plt.get_cmap('jet')
    cNorm  = colors.Normalize(vmin=0, vmax=max(values))
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)
    
    # Using a figure to use it as a parameter when calling nx.draw_networkx
    f = plt.figure(1)
    ax = f.add_subplot(1,1,1)
    for label in ColorLegend:
        ax.plot([0],[0],color=scalarMap.to_rgba(ColorLegend[label]),label=label)
    
    # Just fixed the color map
    nx.draw_networkx(G,pos, cmap = jet, vmin=0, vmax= max(values),node_color=values,with_labels=True,ax=ax)
    
    # Setting it to how it was looking before.                                                                                                              
    plt.axis('off')
    f.set_facecolor('w')
    
    plt.legend()
    
    f.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2021-05-14
      • 2019-07-13
      • 2017-11-25
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多