簡體   English   中英

從 networkx 繪制以底圖 position 為中心的圖形

[英]Draw a graph from networkx centered on a basemap position

我在 map 上搜索 plot 多個子圖,每個子圖將以一個地理 position(或地塊的一個坐標)為中心。 節點本身沒有position(或者都屬於一個城市),但是每個子圖對應一個本地情況。

  • 我嘗試將 position 分配給每個子圖僅一個節點,默認情況下使用“居中”選項繪制剩余的圖
  • 我試圖從https://stackoverflow.com/a/29597209/839119到 plot 啟發 Z4757FE07FD492A8BE0EA6A760D683 上的分層圖

    # -*- coding: utf-8 -*- import networkx as nx import pygraphviz import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap as Basemap G1 = nx.Graph() G1.add_edge('a', 'b', weight=0.6) G1.add_edge('a', 'c', weight=0.2) G1.add_edge('c', 'd', weight=0.1) G1.add_edge('c', 'e', weight=0.7) G1.add_edge('c', 'f', weight=0.9) G1.add_edge('a', 'd', weight=0.3) G2 = nx.Graph() G2.add_edge('a', 'b', weight=0.9) G2.add_edge('a', 'f', weight=0.5) G2.add_edge('c', 'd', weight=0.1) G2.add_edge('c', 'e', weight=0.4) G2.add_edge('c', 'f', weight=0.2) G2.add_edge('a', 'd', weight=0.1) edges = G.edges() weights = [G[u][v]['weight'] for u,v in edges] # liste des poids des edges fig = plt.figure(figsize=(8, 8)) m = Basemap(projection='npstere',boundinglat=48,lon_0=270,resolution='l') m.etopo(scale=0.5, alpha=0.5) mx1,my1=m(-6.266155,53.350140) #would be long, lat coordinates of city 1 mx2,my2=m(-21.827774, 64.128288) #would be long, lat coordinates of city 2 nx.draw_networkx(G1,center=(mx1,my1),pos=nx.spring_layout(G1),node_size=200,node_color='green') nx.draw_networkx(G2,center=(mx2,my2),pos=nx.spring_layout(G2),node_size=200,node_color='red') plt.title("North Polar Stereographic Projection") plt.show()

通過單獨計算節點的位置,我可以很好地 plot 網絡。 您可以使用此代碼段代替您的相關部分再次嘗試。

# (other code above this line)
#
import numpy as np
# compute the positions here
# proper scaling (500000) is applied to the original positions ..
# .. obtained from xxx_layout() to get good spreading
pos1 = nx.spring_layout(G1)
for ea in pos1.keys():
    pos1[ea] =  np.array([mx1, my1]) + pos1[ea]*500000

pos2 = nx.circular_layout(G2)
for ea in pos2.keys():
    pos2[ea] =  np.array([mx2, my2]) + pos2[ea]*500000

nx.draw_networkx(G1, pos=pos1, node_size=100, node_color='green')
nx.draw_networkx(G2, pos=pos2, node_size=100, node_color='red')
#
# (more code below this line)

output plot:

在此處輸入圖像描述

編輯

替代版本:

import numpy as np
# compute the positions here
# proper scaling (500000) is applied to the original positions ..
# .. obtained from xxx_layout() to get good spreading

pos1 = nx.spring_layout(G1, scale=500000, center=[mx1, my1])
pos2 = nx.circular_layout(G2, scale=500000, center=[mx2, my2])

nx.draw_networkx(G1, pos=pos1, node_size=100, node_color='green')
nx.draw_networkx(G2, pos=pos2, node_size=100, node_color='red')
#
# (more code below this line)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM