How to change networkx / matplotlib graphic attributes?

NetworkX includes features for plotting using matplotlib . This is an example of using a large IPython laptop (started with ipython3 notebook --pylab inline ):

enter image description here

Nice, for starters. But how can I influence drawing attributes such as coloring, line width, and marking? I have not worked with matplotlib before.

+6
source share
1 answer

IPython is a great tool for determining which functions (and objects) can perform. If you type

 [1]: import networkx as nx [2]: nx.draw? 

you see

Definition: nx.draw (G, pos = None, ax = None, hold = None, ** kwds)

 **kwds: optional keywords See networkx.draw_networkx() for a description of optional keywords. 

And if you therefore print

 [10]: nx.draw_networkx? 

you will see

 node_color: color string, or array of floats edge_color: color string, or array of floats width: float Line width of edges (default =1.0) labels: dictionary Node labels in a dictionary keyed by node of text labels (default=None) 

So, armed with this information and some experiments, it is not difficult to come to the following:

 import matplotlib.pyplot as plt import numpy as np import networkx as nx import string G = nx.generators.erdos_renyi_graph(18, 0.2) nx.draw(G, node_color = np.linspace(0,1,len(G.nodes())), edge_color = np.linspace(0,1,len(G.edges())), width = 3.0, labels = {n:l for n,l in zip(G.nodes(),string.ascii_uppercase)} ) plt.show() 

what gives

enter image description here

+11
source

All Articles