%load_ext cypher
%config CypherMagic
CypherMagic options ----------------- CypherMagic.auto_html=<Bool> Current: False Return a D3 representation of the graph instead of regular result sets CypherMagic.auto_limit=<Int> Current: 0 Automatically limit the size of the returned result sets CypherMagic.auto_networkx=<Bool> Current: False Return Networkx MultiDiGraph instead of regular result sets CypherMagic.auto_pandas=<Bool> Current: False Return Pandas DataFrame instead of regular result sets CypherMagic.data_contents=<Bool> Current: True Bring extra data to render the results as a graph CypherMagic.display_limit=<Int> Current: 0 Automatically limit the number of rows displayed (full result set is still stored) CypherMagic.feedback=<Bool> Current: True Print number of rows affected CypherMagic.rest=<Bool> Current: False Return full REST representations of objects inside the result sets CypherMagic.short_errors=<Bool> Current: True Don't display the full traceback on Neo4j errors CypherMagic.style=<Unicode> Current: u'DEFAULT' Set the table printing style to any of prettytable's defined styles (currently DEFAULT, MSWORD_FRIENDLY, PLAIN_COLUMNS, RANDOM)
%cypher match (n)-[r]-() delete n, r
6 relationship deleted. 6 nodes deleted.
[]
%%cypher
create
// Nodes
(Neo:Crew {name:'Neo'}),
(Morpheus:Crew {name: 'Morpheus'}),
(Trinity:Crew {name: 'Trinity'}),
(Cypher:Crew:Matrix {name: 'Cypher'}),
(Smith:Matrix {name: 'Agent Smith'}),
(Architect:Matrix {name:'The Architect'}),
// Relationships
(Neo)-[:KNOWS]->(Morpheus),
(Neo)-[:LOVES]->(Trinity),
(Morpheus)-[:KNOWS]->(Trinity),
(Morpheus)-[:KNOWS]->(Cypher),
(Cypher)-[:KNOWS]->(Smith),
(Smith)-[:CODED_BY]->(Architect);
7 labels added. 6 nodes created. 6 properties set. 6 relationships created.
[]
%cypher match (n)-[r]-() return n, count(r) as degree order by degree desc
6 rows affected.
n | degree |
---|---|
{u'name': u'Morpheus'} | 3 |
{u'name': u'Cypher'} | 2 |
{u'name': u'Neo'} | 2 |
{u'name': u'Trinity'} | 2 |
{u'name': u'Agent Smith'} | 2 |
{u'name': u'The Architect'} | 1 |
results = %cypher match (n)-[r]-() return n.name as name, type(r) as rel, count(r) as degree order by degree desc
9 rows affected.
%matplotlib inline
results.get_dataframe()
name | rel | degree | |
---|---|---|---|
0 | Morpheus | KNOWS | 3 |
1 | Cypher | KNOWS | 2 |
2 | Agent Smith | KNOWS | 1 |
3 | Neo | LOVES | 1 |
4 | Trinity | LOVES | 1 |
5 | Neo | KNOWS | 1 |
6 | Trinity | KNOWS | 1 |
7 | The Architect | CODED_BY | 1 |
8 | Agent Smith | CODED_BY | 1 |
results.plot()
[<matplotlib.lines.Line2D at 0x7f707f29b3d0>]
results.bar()
<Container object of 9 artists>
results.pie()
([<matplotlib.patches.Wedge at 0x7f03e1ec9f90>, <matplotlib.patches.Wedge at 0x7f03e1e58a50>, <matplotlib.patches.Wedge at 0x7f03e1e63410>, <matplotlib.patches.Wedge at 0x7f03e1e63d90>, <matplotlib.patches.Wedge at 0x7f03e1e70750>, <matplotlib.patches.Wedge at 0x7f03e1e7d110>, <matplotlib.patches.Wedge at 0x7f03e1e7da90>, <matplotlib.patches.Wedge at 0x7f03e1e8a450>, <matplotlib.patches.Wedge at 0x7f03e1e8add0>], [<matplotlib.text.Text at 0x7f03e1e584d0>, <matplotlib.text.Text at 0x7f03e1e58fd0>, <matplotlib.text.Text at 0x7f03e1e63990>, <matplotlib.text.Text at 0x7f03e1e70350>, <matplotlib.text.Text at 0x7f03e1e70cd0>, <matplotlib.text.Text at 0x7f03e1e7d690>, <matplotlib.text.Text at 0x7f03e1e7df50>, <matplotlib.text.Text at 0x7f03e1e8a9d0>, <matplotlib.text.Text at 0x7f03e1e16390>])
for i in range(1, 5):
%cypher match (n) return n, n.name limit {i}
1 rows affected. 2 rows affected. 3 rows affected. 4 rows affected.
results.draw()
(<networkx.classes.multidigraph.MultiDiGraph at 0x7f707f154790>, <matplotlib.axes._subplots.AxesSubplot at 0x7f707f1bf2d0>, None)
results.graph
<networkx.classes.multidigraph.MultiDiGraph at 0x7f707f09d2d0>
results.dataframe
name | rel | degree | |
---|---|---|---|
0 | Morpheus | KNOWS | 3 |
1 | Cypher | KNOWS | 2 |
2 | Agent Smith | KNOWS | 1 |
3 | Neo | LOVES | 1 |
4 | Trinity | LOVES | 1 |
5 | Neo | KNOWS | 1 |
6 | Trinity | KNOWS | 1 |
7 | The Architect | CODED_BY | 1 |
8 | Agent Smith | CODED_BY | 1 |
print(results.csv())
name,rel,degree Morpheus,KNOWS,3 Cypher,KNOWS,2 Agent Smith,KNOWS,1 Neo,LOVES,1 Trinity,LOVES,1 Neo,KNOWS,1 Trinity,KNOWS,1 The Architect,CODED_BY,1 Agent Smith,CODED_BY,1
from cypher import run
run("match (n)-[r]-() return n.name as name, type(r) as rel, count(r) as degree order by degree desc")
9 rows affected.
name | rel | degree |
---|---|---|
Morpheus | KNOWS | 3 |
Cypher | KNOWS | 2 |
Agent Smith | KNOWS | 1 |
Neo | LOVES | 1 |
Trinity | LOVES | 1 |
Neo | KNOWS | 1 |
Trinity | KNOWS | 1 |
The Architect | CODED_BY | 1 |
Agent Smith | CODED_BY | 1 |