使用 matplotlib 在一张图上绘制两个独立的图,x 和 y 上的比例不同

Plot on one fig two independent plots with matplotlib, different scale on x and y

我想将两个独立的绘图叠加在一个 matplotlib 图形上。

所以我从 osmnx

绘制了一个网络图
import osmnx as ox
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
fig, ax = ox.plot_graph(G)

然后我绘制 networkx 图:

nx.draw(nx.generators.barabasi_albert_graph(10,3), ax = ax)

而且我希望一个很好地叠加在另一个上,但是坐标完全不同。 第一个是 lon,lat,第二个是从 networkx 中随机生成的。

如何很好地缩放它们以便它们可见?

我尝试了 ax.twinx(),但它对我不起作用。

PS。对于网络,我可以为 osmnx 传递 ax nx.draw(G, ax = ax) 而不是,它仅从 plot fig, ax = ox.plot(G)

返回

For networks I can pass ax nx.draw(G, ax = ax) for osmnx not, it is returned from plot only fig, ax = ox.plot(G)

请注意,在 latest OSMnx 中,您可以将 ax 传递给 plot_graph 函数。这应该会在下周的新版本中发布,或者你可以同时使用 GitHub master 分支。

关于缩放您的非空间图,我不相信 networkx gives you any ability to plot nodes or edges at specific coordinates. You'd have to do that all manually. But, as to your specific question, you'd want to first normalize your non-spatial graph coordinates and then scale them to the range of your spatial OSMnx graph's coordinates. This matlab 示例展示了如何轻松地将其转换为 python。