如何使用matplotlib对多个图形使用相同的颜色映射

2024-06-25 07:10:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用networkx进行社交网络分析,并使用它的内置功能绘制部分网络。我画了8个不同的网络基于学科。对于节点着色,我使用matplotlib colormap。这适用于每个地块。你知道吗

但是,我希望在所有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值使用相同的颜色?你知道吗


Tags: inpos网络networkxnode图形for颜色