python - 如何使用 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
在所有图表中为相同的值使用相同的颜色?
解决方案
推荐阅读
- python - 通过在 django 中检查数组中的元素入口来过滤查询集
- regex - 文件中的 Unix 子字符串替换
- jquery - 如何在 jQuery 中通过 id 和 value 定位复选框
- java - 任务中的错误答案 - 算法中的错误
- java - Java场景构建器场景变化,不同的包
- java - 我无法使用共享意图共享图像并且无法创建目录
- python - 如何将数组中的值随机分配给dict.key?
- sql - 如何在 T-SQL 中循环不同数据库中的查询?
- java - 不同 Java 类型擦除和泛型类型格式的优点
- azure-cosmosdb - 如何为 ReadDocumentAsync (Azure Cosmos Db) 设置超时