How to Plot Graph
Suppose I've created a graph per the max cut problem graphs in "Ocean Programs for Beginners", 2.1 Setting Up the Problem" like this:
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_edge(2, 1)
G.add_edge(3, 1)
G.add_edge(4, 2)
G.add_edge(4, 3)
G.add_edge(5, 3)
G.add_edge(5, 4)
I tried to display the graph using:
I tried to display the graph using:
nx.draw(G)
plt.margins(0.2)
plt.show()
In the Leap development environment.
The Draw()\Show() functions didn't display the graph.
How can I display the graph?
Thanks, Ed
In the Leap development environment.
The Draw()\Show() functions didn't display the graph.
How can I display the graph?
Thanks, Ed
Comments
Hi Ed,
Thank you for using D-Wave Systems.
Can you please confirm what you're defining `plt` as? I'm assuming that you're importing pyplot as plt, such as: `import matplotlib.pyplot as plt`.
It is possible that the graph isn't showing is because of `nx.draw(G)`, as a result of the version incompatibility between NetworkX and Matplotlib.
In older versions of NetworkX, the
nx.draw()function was the recommended method for drawing graphs. However, with the release of newer versions of NetworkX, thenx.draw()function has been deprecated and replaced with the more flexiblenx.draw_networkx()function.Therefore, if you are using an older version of NetworkX with a newer version of Matplotlib,
nx.draw(G)may not work as expected. In this case, you can try usingnx.draw_networkx().An example of a working code snippet would look following:
I hope this helps.
Best Regards,
Tanjid
Hi Tanjid,
I copied and pasted your example code exactly as you listed in your comment into a new .py file in the Leap IDE.
I ran the code. I did not see any plot of the graph.
I have Terminal tab selected.
I'm new to using this IDE. So, I don't know if I have to select a specific output window to see the plot.
Thank you for your help.
Also, any chance you could answer my other questions?
Ed
Hi Ed,
The code snippet would work with python IDE's. However, Leap IDE doesn't render graphs on the fly. You can render graphs into a file.
You can look at the structural imbalance as a reference:
I hope that helps.
Best Regards,
Tanjid
Please sign in to leave a comment.