Skip to content

Commit

Permalink
Improve plots (networkx#2579)
Browse files Browse the repository at this point in the history
Improve plots
  • Loading branch information
jarrodmillman authored Aug 4, 2017
1 parent ca2df82 commit 9250791
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
47 changes: 30 additions & 17 deletions doc/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -500,23 +500,10 @@ To test if the import of ``networkx.drawing`` was successful draw ``G`` using on
.. nbplot::

>>> G = nx.petersen_graph()
>>> nx.draw(G)

.. nbplot::

>>> nx.draw_random(G)

.. nbplot::

>>> nx.draw_circular(G)

.. nbplot::

>>> nx.draw_spectral(G)

.. nbplot::

>>> nx.draw_shell(G, nlist=[range(5,10), range(5)])
>>> plt.subplot(121)
>>> nx.draw(G, with_labels=True, font_weight='bold')
>>> plt.subplot(122)
>>> nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')

when drawing to an interactive display. Note that you may need to issue a
Matplotlib
Expand All @@ -527,6 +514,32 @@ command if you are not using matplotlib in interactive mode (see
`Matplotlib FAQ <http://matplotlib.org/faq/installing_faq.html#matplotlib-compiled-fine-but-nothing-shows-up-when-i-use-it>`_
).

.. nbplot::

>>> options = {
... 'node_color': 'black',
... 'node_size': 100,
... 'width': 3,
... }
>>> plt.subplot(221)
>>> nx.draw_random(G, **options)
>>> plt.subplot(222)
>>> nx.draw_circular(G, **options)
>>> plt.subplot(223)
>>> nx.draw_spectral(G, **options)
>>> plt.subplot(224)
>>> nx.draw_shell(G, nlist=[range(5,10), range(5)], **options)

You can find additional options via :func:`draw_networkx` and layouts
via :func:`layout`.
You can use multiple shells with :func:`draw_shell`.

.. nbplot::
>>> G = nx.dodecahedral_graph()
>>> shells = [[2, 3, 4, 5, 6], [8, 1, 0, 19, 18, 17, 16, 15, 14, 7], [9, 10, 11, 12, 13]]
>>> nx.draw_shell(G, nlist=shells, **options)


To save drawings to a file, use, for example

>>> nx.draw(G)
Expand Down
9 changes: 8 additions & 1 deletion examples/graph/plot_football.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,12 @@
for n, d in G.degree():
print('%s %d' % (n, d))

nx.draw(G)
options = {
'node_color': 'black',
'node_size': 50,
'line_color': 'grey',
'linewidths': 0,
'width': 0.1,
}
nx.draw(G, **options)
plt.show()
9 changes: 8 additions & 1 deletion examples/graph/plot_roget.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,12 @@ def roget_graph():
UG = G.to_undirected()
print(nx.number_connected_components(UG), "connected components")

nx.draw_circular(UG)
options = {
'node_color': 'black',
'node_size': 1,
'line_color': 'grey',
'linewidths': 0,
'width': 0.1,
}
nx.draw_circular(UG, **options)
plt.show()
30 changes: 15 additions & 15 deletions networkx/drawing/nx_pylab.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def draw(G, pos=None, ax=None, **kwds):
Examples
--------
>>> G=nx.dodecahedral_graph()
>>> G = nx.dodecahedral_graph()
>>> nx.draw(G)
>>> nx.draw(G,pos=nx.spring_layout(G)) # use spring layout
>>> nx.draw(G, pos=nx.spring_layout(G)) # use spring layout
See Also
--------
Expand Down Expand Up @@ -237,10 +237,10 @@ def draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds):
--------
>>> G = nx.dodecahedral_graph()
>>> nx.draw(G)
>>> nx.draw(G, pos=nx.spring_layout(G)) # use spring layout
>>> nx.draw(G, pos=nx.spring_layout(G)) # use spring layout
>>> import matplotlib.pyplot as plt
>>> limits = plt.axis('off') # turn of axis
>>> limits = plt.axis('off') # turn of axis
Also see the NetworkX drawing examples at
http://networkx.readthedocs.io/en/latest/auto_examples/index.html
Expand Down Expand Up @@ -340,8 +340,8 @@ def draw_networkx_nodes(G, pos,
Examples
--------
>>> G=nx.dodecahedral_graph()
>>> nodes=nx.draw_networkx_nodes(G,pos=nx.spring_layout(G))
>>> G = nx.dodecahedral_graph()
>>> nodes = nx.draw_networkx_nodes(G, pos=nx.spring_layout(G))
Also see the NetworkX drawing examples at
http://networkx.readthedocs.io/en/latest/auto_examples/index.html
Expand Down Expand Up @@ -472,8 +472,8 @@ def draw_networkx_edges(G, pos,
Examples
--------
>>> G=nx.dodecahedral_graph()
>>> edges=nx.draw_networkx_edges(G,pos=nx.spring_layout(G))
>>> G = nx.dodecahedral_graph()
>>> edges = nx.draw_networkx_edges(G, pos=nx.spring_layout(G))
Also see the NetworkX drawing examples at
http://networkx.readthedocs.io/en/latest/auto_examples/index.html
Expand Down Expand Up @@ -520,16 +520,16 @@ def draw_networkx_edges(G, pos,
and cb.iterable(edge_color) \
and len(edge_color) == len(edge_pos):
if np.alltrue([cb.is_string_like(c)
for c in edge_color]):
for c in edge_color]):
# (should check ALL elements)
# list of color letters such as ['k','r','k',...]
edge_colors = tuple([colorConverter.to_rgba(c, alpha)
for c in edge_color])
elif np.alltrue([not cb.is_string_like(c)
for c in edge_color]):
for c in edge_color]):
# If color specs are given as (rgb) or (rgba) tuples, we're OK
if np.alltrue([cb.iterable(c) and len(c) in (3, 4)
for c in edge_color]):
for c in edge_color]):
edge_colors = tuple(edge_color)
else:
# numbers (which are going to be mapped with a colormap)
Expand Down Expand Up @@ -682,8 +682,8 @@ def draw_networkx_labels(G, pos,
Examples
--------
>>> G=nx.dodecahedral_graph()
>>> labels=nx.draw_networkx_labels(G,pos=nx.spring_layout(G))
>>> G = nx.dodecahedral_graph()
>>> labels = nx.draw_networkx_labels(G, pos=nx.spring_layout(G))
Also see the NetworkX drawing examples at
http://networkx.readthedocs.io/en/latest/auto_examples/index.html
Expand Down Expand Up @@ -800,8 +800,8 @@ def draw_networkx_edge_labels(G, pos,
Examples
--------
>>> G=nx.dodecahedral_graph()
>>> edge_labels=nx.draw_networkx_edge_labels(G,pos=nx.spring_layout(G))
>>> G = nx.dodecahedral_graph()
>>> edge_labels = nx.draw_networkx_edge_labels(G, pos=nx.spring_layout(G))
Also see the NetworkX drawing examples at
http://networkx.readthedocs.io/en/latest/auto_examples/index.html
Expand Down

0 comments on commit 9250791

Please sign in to comment.