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:

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
0

Comments

3 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, the nx.draw() function has been deprecated and replaced with the more flexible nx.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 using nx.draw_networkx().

    An example of a working code snippet would look following:

    import matplotlib.pyplot as plt
    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)

    # to display the graph
    # nx.draw(G)
    pos = nx.spring_layout(G)
    nx.draw_networkx(G, pos=pos)
    plt.margins(0.2)    
    plt.show()


    I hope this helps. 

    Best Regards,
    Tanjid

    0
    Comment actions Permalink
  • 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

    0
    Comment actions Permalink
  • 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:

    if show:
      plt.show()
    else:
     filename = 'structural imbalance {} {}.png'.format(sampler_type, region)
     plt.savefig(filename, facecolor='white')

     I hope that helps. 

    Best Regards,
    Tanjid

    0
    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post