如何在matplotlib中使节点标签更加可见

mer*_*xes 5 python matplotlib

我正在使用networkxmatplotlib绘制网络图.我已经为每个节点关联了一个浮点形式的标签(最多两个小数点).我希望标签在图表中更加醒目.是否有任何类型的解决方法可以提供更好的标签可见性?

Matplotlib图

更新:我在这里找到了类似的问题,并尝试应用该解决方案.事实证明,解决方案的效果非常糟糕.

MatplotLib尝试解决方案

代码如下:

label_ratio = 1.0/8.0
    pos_labels = {} 
    #For each node in the Graph
    for node in network.graph.nodes():
        #Get the node's position from the layout
        x,y = network.position[node]
        #Get the node's neighbourhood
        N = network.graph[node]
        #Find the centroid of the neighbourhood. The centroid is the average of the Neighbourhood's node's x and y coordinates respectively.
        #Please note: This could be optimised further
        cx = sum(map(lambda x:pos[x][0], N)) / len(pos)
        cy = sum(map(lambda x:pos[x][1], N)) / len(pos)
        #Get the centroid's 'direction' or 'slope'. That is, the direction TOWARDS the centroid FROM aNode.
        slopeY = (y-cy)
        slopeX = (x-cx)
        #Position the label at some distance along this line. Here, the label is positioned at about 1/8th of the distance.
        pos_labels[node] = (x+slopeX*label_ratio, y+slopeY*label_ratio)

nx.draw(G, pos, ax=axis, node_size=20, with_labels=False)
nx.draw_networkx_labels(G, pos_labels, labels, font_size=7, font_color='b', ax=axis)
Run Code Online (Sandbox Code Playgroud)

Spa*_*ine 5

NetworkX不足以绘制大型图形,因为它仅提供可视化图形的基本功能.

在您的情况下,增加节点大小似乎不可避免地使节点标签更加可见.一旦尺寸增大,相对位置就会出现问题.我建议你首先使用Gephi来获得更好的布局.

以下是基本步骤.

  • 步骤1.将NetworkX图形导出为适当的格式,例如 .gramph
  • 第2步.Gephi中的布局
  • 步骤3.使用Matplotlib绘图或直接从Gephi导出

请参阅NetworkX应用说明:可视化图形以获取详细说明的更好方法


使用提问者提供的图形文件,下图是从Gephi导出的(使用布局YifanHu,手动拖动一些节点, - >预览(例如调整文本大小) - >导出).它比NetworkX好吗?

在此输入图像描述