python - 从 networkx 绘制以底图位置为中心的图形
问题描述
我正在寻找在地图上绘制多个子图,每个子图将以一个地理位置(或一个地块的一个坐标)为中心。节点本身没有位置(或者它们都属于一个城市),但每个子图对应一个本地情况。
- 我尝试为每个子图仅分配一个位置,默认情况下使用“居中”选项绘制剩余的图
我试图从https://stackoverflow.com/a/29597209/839119中得到启发,在某个位置上绘制层次图,但没有成功
# -*- 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()
解决方案
通过分别计算节点的位置,我可以很好地绘制网络。您可以使用此代码段代替您的相关部分再次尝试。
# (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)
输出图:
编辑
替代版本:
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)
推荐阅读
- html - 即使没有要滚动的内容,Xcode WKWebView HTML 内容也会滚动
- svg - SVG路径贝塞尔曲线
- python - 无法获取部署在 Flask 上的 Python 脚本的输入
- java - JUnit 5 @ParamterizedTest {arguments} 与 {argumentsWithNames} 占位符
- android - Slack 像底页对话框片段
- python - ValueError: 层序贯_10 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。Alexnet(cnn) + LSTM
- java - 是否可以使用 Java 下载整个 YouTube 播放列表,更具体地说是 Android Studio
- java - 如何在 Java 中禁用 linting?
- ruby-on-rails - Rspec ArgumentError:参数数量错误(给定 2,预期为 0)
- laravel - 如何在 Laravel 中获取分页查询的总和