Asked 7 years, 1 month ago
Viewed 90k times
99
76

Can anyone recommend a Python library that can do interactive graph visualization?

I specifically want something like d3.js but for python and ideally it would be 3D as well.

I have looked at:

  • NetworkX - it only does Matplotlib plots and those seem to be 2D. I didn't see any sort of interactiveness, like one that d3.js gives, such as pulling nodes around.
  • graph-tool - it does only 2D plots and has very slow interactive graphs.
akaihola

21.9k55 gold badges5151 silver badges6060 bronze badges
asked Oct 19 '12 at 15:26
Eiyrioü von Kauyf

3,07044 gold badges2626 silver badges3737 bronze badges
 
  • 1
    You would want to generate a graph in networkx and then manipulate in d3.js, if you're looking for a browser based version. – kreativitea Oct 19 '12 at 15:45
  •  
    @kreativitea ok .... how would I do that o-o ideally: Graph Data (via API Calls in Python) -> Python (Machine Learning Stuffs) -> Django / Something + D3.js (visualization) -> Pretty pictures and website :) – Eiyrioü von Kauyf Oct 21 '12 at 20:50

14 Answers

68

You could use d3py a python module that generate xml pages embedding d3.js script. For example :

import d3py
import networkx as nx import logging
logging.basicConfig(level=logging.DEBUG) G = nx.Graph()
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(3,2)
G.add_edge(3,4)
G.add_edge(4,2) # use 'with' if you are writing a script and want to serve this up forever
with d3py.NetworkXFigure(G, width=500, height=500) as p:
p += d3py.ForceLayout()
p.show()
Eric O Lebigot

70.7k3838 gold badges179179 silver badges229229 bronze badges
answered Jan 5 '13 at 23:59
Vincent Agnus

91677 silver badges99 bronze badges
 
  •  
    Worked for me, but I had to edit one of the lines to with d3py.NetworkXFigure(G, width=500, height=500, host="localhost") as p:. I checked out the latest commit of d3py at github (SHA: 4e92a90f4003370bc086e0f57b19fca1bd4e8fba) – xb. Jan 4 '14 at 18:57
  • 7
    Unfortunately d3py isn't being actively developed any more - Vincent is the modern equivalent (a Python interface to Vega/d3.js) but psychemedia's answer below (export networkx to json then render in d3.js) might be the cleanest. – A.Wan Nov 20 '14 at 0:27
  • 2
    Try altair-viz.github.io - the successor of d3py and vincent. See also stackoverflow.com/a/49695472/179014 . – asmaier Jan 23 at 13:42
38

Plotly supports interactive 2D and 3D graphing. Graphs are rendered with D3.js and can be created with a Python APImatplotlibggplot for PythonSeabornprettyplotlib, and pandas. You can zoom, pan, toggle traces on and off, and see data on the hover. Plots can be embedded in HTML, apps, dashboards, and IPython Notebooks. Below is a temperature graph showing interactivity. See the gallery of IPython Notebooks tutorials for more examples.

The docs provides examples of supported plot types and code snippets.

Specifically to your question, you can also make interactive plots from NetworkX.

For 3D plotting with Python, you can make 3D scatter, line, and surface plots that are similarly interactive. Plots are rendered with WebGL. For example, see a 3D graph of UK Swap rates.

Disclosure: I'm on the Plotly team.

answered Nov 6 '13 at 2:13
Mateo Sanchez

1,3111212 silver badges1515 bronze badges
 
  • 6
    Clearly the question aims at graphs in the sense of nodes connected by edges. This answer needlessly includes other visualization capabilities of plotly. – Lutz Büch Jul 23 '18 at 5:47
  •  
    @mateo-sanchez it is very unfortunate that Plotly has decided to terminate all academic and individual subscriptions to focus on corporate clients – Andreuccio Aug 20 at 12:41
20

Have you looked at vincent? Vincent takes Python data objects and converts them to Vega visualization grammar. Vega is a higher-level visualization tool built on top of D3. As compared to D3py, the vincent repo has been updated more recently. Though the examples are all static D3.

more info:


The graphs can be viewed in Ipython, just add this code

vincent.core.initialize_notebook()

Or output to JSON where you can view the JSON output graph in the Vega online editor (http://trifacta.github.io/vega/editor/) or view them on your Python server locally. More info on viewing can be found in the pypi link above.

Not sure when, but the Pandas package should have D3 integration at some point. http://pandas.pydata.org/developers.html

Bokeh is a Python visualization library that supports interactive visualization. Its primary output backend is HTML5 Canvas and uses client/server model.

examples: http://continuumio.github.io/bokehjs/

answered Oct 10 '13 at 19:28
sk8asd123

1,2351111 silver badges1111 bronze badges
 
17

One recipe that I have used (described here: Co-Director Network Data Files in GEXF and JSON from OpenCorporates Data via Scraperwiki and networkx ) runs as follows:

  • generate a network representation using networkx
  • export the network as a JSON file
  • import that JSON into to d3.js. (networkx can export both the tree and graph/network representations that d3.js can import).

The networkx JSON exporter takes the form:

from networkx.readwrite import json_graph
import json
print json.dumps(json_graph.node_link_data(G))

Alternatively you can export the network as a GEXF XML file and then import this representation into the sigma.js Javascript visualisation library.

from xml.etree.cElementTree import tostring
writer=gf.GEXFWriter(encoding='utf-8',prettyprint=True,version='1.1draft')
writer.add_graph(G)
print tostring(writer.xml)
answered Mar 16 '13 at 23:56
psychemedia

3,77944 gold badges3737 silver badges6969 bronze badges
 
16

Another option is bokeh which just went to version 0.3.

answered Nov 21 '13 at 18:37
MrDrFenner

68088 silver badges1010 bronze badges
 
7

For those who recommended pyd3, it is no longer under active development and points you to vincent. vincent is also no longer under active development and recommends using altair.

So if you want a pythonic d3, use altair.

answered Nov 11 '16 at 14:54
Wes

38844 silver badges1212 bronze badges
 
5

Check out python-nvd3. It is a python wrapper for nvd3. Looks cooler than d3.py and also has more chart options.

answered Aug 23 '13 at 13:07
richie

8,46888 gold badges3939 silver badges5858 bronze badges
 
4

I would suggest using mpld3 which combines D3js javascript visualizations with matplotlib of python.

The installation and usage is really simple and it has some cool plugins and interactive stuffs.

http://mpld3.github.io/

answered Feb 9 '15 at 11:45
Ram

54466 silver badges1818 bronze badges
 
4
answered Apr 6 '18 at 14:50
asmaier

8,09977 gold badges5858 silver badges8888 bronze badges
 
2

Plotly can do some cool stuffs for you 

https://plot.ly/

Produces highly interactive graphs that can be easily embedded withing the HTML pages for your private server or website using its off line API.

Update: I am note sure about its 3D plotting capabilities, for 2D graphs is awesome Thanks

answered May 17 '17 at 5:26
jax

2,05544 gold badges2020 silver badges4343 bronze badges
 
  • 2
    Note that this is chart visualization... The question requests graph visualization. (I appreciate these phrases are commonly conflated!) – j6m8 Mar 3 '18 at 22:18
2

You can also choose to serialize your data and then visualize it in D3.js, as done here: Use Python & Pandas to Create a D3 Force Directed Network Diagram (It comes with a jupyter notebook as well!)

Here is the gist. You serialize your graph data in this format:

import json
json_data = {
"nodes":[
{"name":"Myriel","group":1},
{"name":"Napoleon","group":1},
{"name":"Mlle.Baptistine","group":1},
{"name":"Mme.Magloire","group":1},
{"name":"CountessdeLo","group":1},
],
"links":[
{"source":1,"target":0,"value":1},
{"source":2,"target":0,"value":8},
{"source":3,"target":0,"value":10},
{"source":3,"target":2,"value":6},
{"source":4,"target":0,"value":1},
{"source":5,"target":0,"value":1},
]
}
filename_out = 'graph_data.json'
json_out = open(filename_out,'w')
json_out.write(json_data)
json_out.close()

Then you load the data in with d3.js:

d3.json("pcap_export.json", drawGraph);

For the routine drawGraph I refer you to the link, however.

Pearly Spencer

19.7k2121 gold badges5454 silver badges101101 bronze badges
answered Jun 13 '18 at 9:06
Lutz Büch

18811 silver badge99 bronze badges
 
  •  
    I edited it now, but I didn't include the drawGraph routine which itself calls drawLinks and drawNodes. It would just be too cumbersome and the elements only make sense in the context of the whole html file. – Lutz Büch Jun 13 '18 at 10:18
1

There is an interesting port of NetworkX to Javascript that might do what you want. See http://felix-kling.de/JSNetworkX/

answered Oct 24 '12 at 22:31
Aric

18.1k44 gold badges4949 silver badges6262 bronze badges
 
1

See:

Is there a good interactive 3D graph library out there?

The accepted answer suggests the following program, which apparently has python bindings: http://ubietylab.net/ubigraph/

Edit

I'm not sure about the interactivity of NetworkX, but you can definitely make 3D graphs. There is at least one example in the gallery:

http://networkx.lanl.gov/examples/drawing/edge_colormap.html

And another example in the 'examples'. This one, however, requires that you have Mayavi.

http://networkx.lanl.gov/examples/3d_drawing/mayavi2_spring.html

answered Oct 19 '12 at 15:33
juniper-

4,41655 gold badges2727 silver badges4848 bronze badges
 
0

I've got a good example of automatically generating D3.js network diagrams using Python here: http://brandonrose.org/ner2sna

The cool thing is that you end up with auto-generated HTML and JS and can embed the interactive D3 chart in a notebook with an IFrame

answered Jul 11 '17 at 1


Python equivalent of D3.js的更多相关文章

  1. MongoDB with D3.js

    MongoDB with D3.js I consider interactive data visualization to be the critical tool for exploration ...

  2. d3.js path路径

    转自:http://www.d3js.cn/?p=68 svg的path标签被称为”可以组成任何形状的形状” SVG Path可以绘制任何形状的图形,包括矩形,圆形,椭圆,折线,多边形,直线,曲线等. ...

  3. D3.js学习(七)

    上一节中我们学会了如何旋转x轴标签以及自定义标签内容,在这一节中,我们将接触动画(transition) 首先,我们要在页面上添加一个按钮,当我们点击这个按钮时,调用我们的动画.所以,我们还需要在原来 ...

  4. D3.js学习(一)

    从今天开始我将和大家一起学习D3.js(Data-Driven Documents),由于国内关于D3的学习资料少之又少,所以我觉得很有必要把自己学习过程记录下来,供同学们参考,如果文章有有哪些表达有 ...

  5. D3.js学习记录

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. D3.js部署node环境开发

    总结一段D3.js部署node环境的安装过程 准备阶段: 首先电脑上要安装node环境,这个阶段过滤掉,如果node环境都不会装,那就别玩基于node环境搞的其他东西了. 搭建环境: 我在自己的F:系 ...

  7. d3.js读书笔记-1

    d3.js入门 d3入门 D3是一个强大的数据可视化工具,它是基于Javascript库的,用于创建数据可视化图形.在生成可视化图形的过程中,需要以下几步: 把数据加载到浏览器的内存空间: 把数据绑定 ...

  8. 【 D3.js 进阶系列 — 6.1 】 缩放的应用(Zoom)

    缩放(Zoom)是另一种重要的可视化操作,主要是使用鼠标的滚轮进行. 1. zoom 的定义 缩放是由 d3.behavior.zoom() 定义的. var zoom = d3.behavior.z ...

  9. [资料搜集狂]D3.js数据可视化开发库

    偶然看到一个强大的D3.js,存档之. D3.js 是近年来十分流行的一个数据可视化开发库. 采用BSD协议 源码:https://github.com/mbostock/d3 官网:http://d ...

随机推荐

  1. UVA 1473 Dome of Circus

    https://cn.vjudge.net/problem/UVA-1473 题目 给出一些点,问包含这些点的最小圆锥(要求顶点在y轴,底面圆心在原点)的体积 题解 因为圆锥对称,所以可以把所有点旋转 ...

  2. python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作

    pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...

  3. Leetcode 90. 子集 II

    地址  https://leetcode-cn.com/problems/subsets-ii/ 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重 ...

  4. LG4377 「USACO2018OPEN」Talent Show 分数规划+背包

    问题描述 LG4377 题解 有 \(n\) 个物品,每个物品有两个权值 \(a,b\) 需要确定一组 \(w_i \in [0,1]\) ,使得 \(\frac{\sum{w_i \times a_ ...

  5. 1+x 证书 Web 前端开发中级理论考试(试卷 7 ) 答案

    1+x 证书 Web 前端开发中级理论考试(试卷 7 ) 答案 转载请注明来源:妙笔生花个人博客http://blog.zh66.club/index.php/archives/438/ 官方QQ群 ...

  6. IT兄弟连 Java语法教程 数组 经典案例

    案例需求: 编程实现双色球中奖号码的生成 1)应用知识: ●  数组的声明 ●  数组的使用 ●  for循环 2)需求解析: 在该程序中,需要定义一个长度为7的数组,用来存储中奖号码,使用Rando ...

  7. Map拼接URL地址

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * @Author: hoje * Des ...

  8. Netty — 线程模型

    一.前言 众所周知,netty是高性能的原因源于其使用的是NIO,但是这只是其中一方面原因,其IO模型上决定的.另一方面源于其线程模型的设计,良好的线程模型设计,能够减少线程上下文切换,减少甚至避免锁 ...

  9. Java SPI机制实战详解及源码分析

    背景介绍 提起SPI机制,可能很多人不太熟悉,它是由JDK直接提供的,全称为:Service Provider Interface.而在平时的使用过程中也很少遇到,但如果你阅读一些框架的源码时,会发现 ...

  10. c#日期和时间戳互转

    using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespac ...