首页 > 解决方案 > 如何使用 matplotlib 对多个图形使用相同的颜色图

问题描述

我使用 networkx 进行社交网络分析,并使用它的内置功能来绘制网络的一部分。我根据学科绘制了 8 个不同的网络。为了给节点着色,我使用了 matplotlib 颜色图。这对每个情节都适用。

但是,我想在所有 8 个图表中使用相同的颜色,即 0.5 的值应始终指代相同的颜色。目前,实际颜色取决于子图中的其他颜色(例如,如果最大值为 0.5,它将导致不同的颜色,就好像 0.5 将是最小的数字一样)。

node_color变量是动态计算的,即它包含所有 8 个子图的不同值。


import networkx as nx

for discipline in disciplines:

    #this is the networkx graph
    K = nx.karate_club_graph() #just for illustration

    #in my code, it returns different graphs for each discipline, see below
    #K = filter_graph_by_discipline(discipline)

    plt.figure(figsize=(10,7))
    pos = nx.spring_layout(K, seed=1)

    #this returns a list of numercial values 
    node_color = [nx.betweenness_centrality(K)[v] for v in K]

    nx.draw_networkx(K, pos, node_color=node_color, cmap=plt.cm.tab10)

如何betweenness_centrality在所有图表中为相同的值使用相同的颜色?

标签: pythonmatplotlibnetworkxcolormap

解决方案


推荐阅读