The interaction between networkx and matplotlib

I try networkx and visualization in matplotlib, am I confused because I don’t understand how they interact with each other? Simple example

import matplotlib.pyplot import networkx as nx G=nx.path_graph(8) nx.draw(G) matplotlib.pyplot.show() 

Where will I tell pyplot that I want to draw a graph G? I assume nx.draw uses something like matplotlib.pyplot. {Plot, etc.} So, if I want to draw 2 graphs:

 import matplotlib.pyplot import networkx as nx G=nx.path_graph(8) E=nx.path_graph(30) nx.draw(G) matplotlib.pyplot.figure() nx.draw(E) matplotlib.pyplot.show() 

Then ... a little experiment

 import networkx as nx G=nx.path_graph(8) E=nx.path_graph(30) nx.draw(G) import matplotlib.pyplot matplotlib.pyplot.figure() nx.draw(E) import matplotlib.pyplot as plt plt.show() 

Please do not kill me about this stupid code, I'm just trying to understand how network graphics draw matplotlib something before it is imported!

PS: Sorry for my English.

+6
source share
1 answer

Just create two different axes if you want to draw graphs separately or create one Axes object and pass it to nx.draw . For instance:

 G = nx.path_graph(8) E = nx.path_graph(30) # one plot, both graphs fig, ax = subplots() nx.draw(G, ax=ax) nx.draw(E, ax=ax) 

To obtain:

enter image description here

If you need two different shape objects, create them separately, for example:

 G = nx.path_graph(8) E = nx.path_graph(30) # two separate graphs fig1 = figure() ax1 = fig1.add_subplot(111) nx.draw(G, ax=ax1) fig2 = figure() ax2 = fig2.add_subplot(111) nx.draw(G, ax=ax2) 

getting:

enter image description hereenter image description here

Finally, you can create a subtitle if you want, for example:

 G = nx.path_graph(8) E = nx.path_graph(30) pos=nx.spring_layout(E,iterations=100) subplot(121) nx.draw(E, pos) subplot(122) nx.draw(G, pos) 

as a result of:

enter image description here

Whatever it costs, it seems that the ax argument to nx.draw useless when using the matplotlib API if you want to create subheadings outside of pylab , because nx.draw has some calls to gca , which makes it dependent on the pylab interface. In fact, I did not understand why this is so, I just thought that I would like to point out this.

The source code for nx.draw pretty simple:

 try: import matplotlib.pylab as pylab except ImportError: raise ImportError("Matplotlib required for draw()") except RuntimeError: print("Matplotlib unable to open display") raise cf=pylab.gcf() cf.set_facecolor('w') if ax is None: if cf._axstack() is None: ax=cf.add_axes((0,0,1,1)) else: ax=cf.gca() # allow callers to override the hold state by passing hold=True|False b = pylab.ishold() h = kwds.pop('hold', None) if h is not None: pylab.hold(h) try: draw_networkx(G,pos=pos,ax=ax,**kwds) ax.set_axis_off() pylab.draw_if_interactive() except: pylab.hold(b) raise pylab.hold(b) return 
  • The figure is taken from the environment using gcf .
  • Then the Axes object is added to the shape if it does not exist, otherwise you will get it from the environment using gca .
  • Make complexion white
  • enable hold in
  • draw it using the internal function
  • turn off axis
  • Finally, if we are in interactive mode, draw it and re-raise any exceptions that were caught.
+13
source

Source: https://habr.com/ru/post/952943/


All Articles