0

I want two independent plots to be overlayed on one matplotlib figure.

So I plot a network graph from osmnx

import osmnx as ox
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
fig, ax = ox.plot_graph(G)

Then I plot the networkx graph with:

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

And I want one to be overlayed on the other nicely, however the coordinates are completely different. First one is lon, lat and the second is randomly generated from networkx.

How to scale them nicely so that they are visible?

I tried ax.twinx(), but it doesn't work for me.

PS. 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)

1 Answers1

1

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)

Note that in the latest OSMnx you can pass an ax into the plot_graph function. This should be released in the new version next week, or you can use the GitHub master branch in the meantime.

Regarding scaling your non-spatial graph, I do not believe that 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 example shows how and is easily translated into python.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • fwiw, you can easily extract the coordinates from the osmnx graph into a pos dictionary, which you can feed to the various networkx plotting functions. I tried, but could not display the ox and the nx graph together on the same ax. – warped Jun 25 '20 at 07:15