-1

I have created a graph using GraphFrame

g = GraphFrame (vertices, edges)

Apart from analyzing the graph using the queries and the properties offered by the GraphFrame, I would like to visualize the graph to use in a presentation.

Do you know of any tool/library / API / code that allows this visualization in a simple way?

Ransaka Ravihara
  • 1,786
  • 1
  • 13
  • 30

3 Answers3

2

Not a simple way, but you can use python-igraph library, https://igraph.org/. I used it from R but python should be similar. See simple example below. The main problem with all that tool, you should carefully select small subgraph to draw.

Install it:

#>pip install python-igraph

The simplest visualisation:

g = GraphFrame (vertices, edges)
from igraph import *
ig = Graph.TupleList(g.edges.collect(), directed=True)
plot(ig)
Artem Aliev
  • 1,362
  • 7
  • 12
2

Another way is to use the plot functionality from the graph module networkx

import networkx as nx
from graphframes import GraphFrame

def PlotGraph(edge_list):
    Gplot=nx.Graph()
    for row in edge_list.select('src','dst').take(1000):
        Gplot.add_edge(row['src'],row['dst'])

    plt.subplot(121)
    nx.draw(Gplot)


spark = SparkSession \
    .builder \
    .appName("PlotAPp") \
    .getOrCreate()

sqlContext = SQLContext(spark)

vertices = sqlContext.createDataFrame([
  ("a", "Alice", 34),
  ("b", "Bob", 36),
  ("c", "Charlie", 30),
  ("d", "David", 29),
  ("e", "Esther", 32),
("e1", "Esther2", 32),
  ("f", "Fanny", 36),
  ("g", "Gabby", 60),
    ("h", "Mark", 61),
    ("i", "Gunter", 62),
    ("j", "Marit", 63)], ["id", "name", "age"])

edges = sqlContext.createDataFrame([
  ("a", "b", "friend"),
  ("b", "a", "follow"),
  ("c", "a", "follow"),
  ("c", "f", "follow"),
  ("g", "h", "follow"),
  ("h", "i", "friend"),
  ("h", "j", "friend"),
  ("j", "h", "friend"),
    ("e", "e1", "friend")
], ["src", "dst", "relationship"])

g = GraphFrame(vertices, edges)
PlotGraph(g.edges)

see also PYSPARK: how to visualize a GraphFrame?

Alex Ortner
  • 1,097
  • 8
  • 24
0

Tried the igraph solution, but plot(ig) raised an error: AttributeError: Plotting not available; please install pycairo or cairocffi.

So I just saved the graph as SVG, this worked fine:

ig.write_svg('/tmp/ig.svg')

Cheers

saza
  • 460
  • 6
  • 7