如何显示/可视化由GraphFrame创建的图形?

时间:2019-01-15 17:32:21

标签: pyspark visualization graphframes

我已经使用GraphFrame创建了一个图形(g = GraphFrame(顶点,边))。除了使用GraphFrame提供的查询和属性来分析图形外,我还要可视化图形以在演示文稿中使用它。

您知道有任何工具/库/ API /代码可以通过简单的方式实现这种可视化吗?

g = GraphFrame(顶点,边)

2 个答案:

答案 0 :(得分:0)

这不是一种简单的方法,但是您可以使用python-igraph库https://igraph.org/。我从R使用它,但是python应该类似。请参阅下面的简单示例。所有这些工具的主要问题是,您应该仔细选择要绘制的小子图。

安装:

#>pip install python-igraph

最简单的可视化:

g = GraphFrame (vertices, edges)
from igraph import *
ig = Graph.TupleList(g.edges.collect(), directed=True)
plot(ig)

答案 1 :(得分:0)

另一种方法是使用图形模块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)

另请参阅PYSPARK: how to visualize a GraphFrame?