diff options
| author | Jarrod Millman <jarrod.millman@gmail.com> | 2017-07-21 08:50:40 -0700 |
|---|---|---|
| committer | Dan Schult <dschult@colgate.edu> | 2017-07-21 11:50:40 -0400 |
| commit | 1e6df4c105fa7a1e442da72ddb009bb253dc7d4e (patch) | |
| tree | d728526ccca673a762f90f174223cb558c86e02d /examples/subclass | |
| parent | 7be0d8f33b46d37264ae181cb2a9fadb310fe687 (diff) | |
| download | networkx-1e6df4c105fa7a1e442da72ddb009bb253dc7d4e.tar.gz | |
Refactor examples (#2527)
* Fix unix_email and plot more examples
* Fix doctest
* Fix more examples
* Fix AntiGraph.degree generator error in examples
* Style/consistency changes and plot more examples
Diffstat (limited to 'examples/subclass')
| -rw-r--r-- | examples/subclass/plot_antigraph.py (renamed from examples/subclass/antigraph.py) | 19 | ||||
| -rw-r--r-- | examples/subclass/plot_printgraph.py (renamed from examples/subclass/printgraph.py) | 12 |
2 files changed, 19 insertions, 12 deletions
diff --git a/examples/subclass/antigraph.py b/examples/subclass/plot_antigraph.py index 1c2b4d13..87017651 100644 --- a/examples/subclass/antigraph.py +++ b/examples/subclass/plot_antigraph.py @@ -17,12 +17,13 @@ algorithms. """ # Author: Jordi Torrents <jtorrents@milnou.net> -# Copyright (C) 2015-2016 by +# Copyright (C) 2015-2017 by # Jordi Torrents <jtorrents@milnou.net> # All rights reserved. # BSD license. import networkx as nx from networkx.exception import NetworkXError +import matplotlib.pyplot as plt __all__ = ['AntiGraph'] @@ -64,7 +65,7 @@ class AntiGraph(nx.Graph): set(self.adj) - set(self.adj[n]) - set([n])) def neighbors(self, n): - """Return an iterator over all neighbors of node n in the + """Return an iterator over all neighbors of node n in the dense graph. """ @@ -111,19 +112,20 @@ class AntiGraph(nx.Graph): nodes_nbrs = ((n, {v: self.all_edge_dict for v in set(self.adj) - set(self.adj[n]) - set([n])}) for n in self.nodes()) + elif nbunch in self: + nbrs = set(self.nodes()) - set(self.adj[nbunch]) - {nbunch} + return len(nbrs) else: nodes_nbrs = ((n, {v: self.all_edge_dict for v in set(self.nodes()) - set(self.adj[n]) - set([n])}) for n in self.nbunch_iter(nbunch)) if weight is None: - for n, nbrs in nodes_nbrs: - yield (n, len(nbrs) + (n in nbrs)) # return tuple (n,degree) + return ((n, len(nbrs)) for n, nbrs in nodes_nbrs) else: # AntiGraph is a ThinGraph so all edges have weight 1 - for n, nbrs in nodes_nbrs: - yield (n, sum((nbrs[nbr].get(weight, 1) for nbr in nbrs)) + - (n in nbrs and nbrs[n].get(weight, 1))) + return ((n, sum((nbrs[nbr].get(weight, 1)) for nbr in nbrs)) + for n, nbrs in nodes_nbrs) def adjacency_iter(self): """Return an iterator of (node, adjacency set) tuples for all nodes @@ -175,3 +177,6 @@ if __name__ == '__main__': # AntiGraph is a ThinGraph, so all the weights are 1 assert sum(d for n, d in A.degree()) == sum(d for n, d in A.degree(weight='weight')) assert sum(d for n, d in G.degree(nodes)) == sum(d for n, d in A.degree(nodes)) + + nx.draw(Gnp) + plt.show() diff --git a/examples/subclass/printgraph.py b/examples/subclass/plot_printgraph.py index 31004d20..3a88d610 100644 --- a/examples/subclass/printgraph.py +++ b/examples/subclass/plot_printgraph.py @@ -7,7 +7,7 @@ Example subclass of the Graph class. """ # Author: Aric Hagberg (hagberg@lanl.gov) -# Copyright (C) 2004-2016 by +# Copyright (C) 2004-2017 by # Aric Hagberg <hagberg@lanl.gov> # Dan Schult <dschult@colgate.edu> # Pieter Swart <swart@lanl.gov> @@ -16,12 +16,11 @@ Example subclass of the Graph class. # __docformat__ = "restructuredtext en" -from networkx import Graph - -from networkx.exception import NetworkXException, NetworkXError -import networkx.convert as convert from copy import deepcopy +import matplotlib.pyplot as plt +import networkx as nx +from networkx import Graph class PrintGraph(Graph): """ @@ -133,3 +132,6 @@ if __name__ == '__main__': H2 = G.subgraph(range(4), copy=False) print(list(H1.edges())) print(list(H2.edges())) + + nx.draw(G) + plt.show() |
