Skip to content

Commit

Permalink
Merge pull request #24 from NelleV/no_edges_to_plot
Browse files Browse the repository at this point in the history
plot_network fails if there is no edges to plot.
  • Loading branch information
NelleV committed Apr 2, 2018
2 parents 485f0ab + e2c4901 commit af475f0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@



all:

test:
py.test grave
2 changes: 1 addition & 1 deletion examples/plot_layout_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
The default layouts available through Grave may not be sufficient for ones
need. Hence, Grave also support custom layouts.
"""

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
Expand Down
4 changes: 2 additions & 2 deletions grave/_layout.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import networkx as nx


known_layouts = {
KNOWN_LAYOUTS = {
"spring": nx.spring_layout,
"circular": nx.circular_layout,
"random": nx.random_layout,
Expand All @@ -15,6 +15,6 @@ def _apply_layout(layout, graph):
if callable(layout):
return layout(graph)
elif isinstance(layout, str):
return known_layouts[layout](graph)
return KNOWN_LAYOUTS[layout](graph)
else:
raise ValueError("Dunno what do do with this")
13 changes: 12 additions & 1 deletion grave/grave.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def _generate_node_artist(pos, styles, *, ax):

def _generate_straight_edges(edges, pos, styles, *, ax):
N = len(edges)
if not N:
return None, None
proto_edge = next(iter(edges))
edge_pos = [None] * N
edge_indx = [None] * N
Expand All @@ -84,7 +86,7 @@ def _generate_straight_edges(edges, pos, styles, *, ax):
edge_indx[j] = (u, v)
key_map = {'color': 'colors',
'width': 'linewidths',
'style': 'linestyle'}
'style': 'linestyle',}

renamed_properties = {key_map[k]: v
for k, v in properties.items()}
Expand Down Expand Up @@ -373,6 +375,15 @@ def plot_network(graph, layout="spring",
----------
graph : networkx graph object
layout : string or callable, optional, default: "spring"
Specifies the type of layout to use for plotting.
It must be one of "spring", "circular", "random", "kamada_kawai",
"shell", "spectral", or a callable.
If a callable is given, it is used to compute the 2D coordinates from
the graph object, and should return a FIXME.
"""
if node_style is None:
node_style = {}
Expand Down
14 changes: 10 additions & 4 deletions grave/tests/test_grave.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import networkx as nx

from grave import plot_network
import matplotlib as mpl
mpl.use('PS', warn=False)

import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = False

from grave import plot_network

def test_smoke_graph():
graph = nx.barbell_graph(10, 14)
plot_network(graph)


def test_empty_graph():
G = nx.barbell_graph(10, 14)
plot_network(G)
def test_graph_no_edges():
graph = nx.Graph()
graph.add_node(0)
plot_network(graph)

0 comments on commit af475f0

Please sign in to comment.