0

I'm generating graphs using NetworkX and I plot some data using pyplot. Can I show them on one plot using subplot or something similar? I managed to subplot two pyplot-generated plots on one figure, I can't get two NetworkX-generated, nor mixed.

Here's the example code I'm testing it on:

import networkx as nx
import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10]
y = [3,2,5,7,3,6,8,5,2,5]

fig = plt.figure()
sp1 = fig.add_subplot(1,1,1)
sp1.plot(x,y)
sp2 = fig.add_subplot(1,2,2)

G = nx.Graph()
G.add_edge(1,2,color='r',weight=2)
G.add_edge(2,3,color='b',weight=4)
G.add_edge(3,4,color='g',weight=6)

pos = nx.circular_layout(G)

edges = G.edges()
colors = [G[u][v]['color'] for u,v in edges]
weights = [G[u][v]['weight'] for u,v in edges]

# nx.draw(G, pos, edges=edges, edge_color=colors, width=weights)

sp2.draw(G)

plt.show()
4
  • 1
    nx.draw has an ax argument for this kind of situations: nx.draw(G, pos, ax=sp2, ...) Commented Mar 28, 2020 at 2:44
  • Thanks, I somehow missed it reading through the reference pdf. It does work perfectly now, thank you! Commented Mar 28, 2020 at 13:53
  • 1
    Does this answer your question? Interaction between networkx and matplotlib Commented Mar 28, 2020 at 19:36
  • Yes, as well. I only needed the ax argument though. Thanks anyway! Commented Mar 28, 2020 at 23:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.