使用 python 和 networkx 进行大图可视化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17381006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
large graph visualization with python and networkx
提问by CodeKingPlusPlus
I am having trouble with large graph visualization in python and networkx. The graph is wish to visualize is directed, and has an edge and vertex set size of 215,000 From the documenation (which is linked at the top page) it is clear that networkx supports plotting with matplotlib
and GraphViz. In matplotlib
and networkx the drawing is done as follows:
我在 python 和networkx中遇到大图可视化问题。希望可视化的图形是有向的,边和顶点集大小为 215,000 从文档(在首页链接)很明显 networkx 支持使用matplotlib
GraphViz 进行绘图。在matplotlib
和 networkx 中绘制如下:
import
networkx as nx
import matplotlib.pyplot as plt
#Let g be a graph that I created
nx.draw(g)
I get a memory error after nx.draw(g)
, afterwards you would normally do plt.show()
or plt.[some_function] to save the file in a format for efficient and so forth.
之后出现内存错误nx.draw(g)
,之后您通常会执行plt.show()
或 plt.[some_function] 以高效格式保存文件等等。
Next I tried GraphViz. From the wikipedia pagethe dot
format is used for directed graphs and I created a dot file:
接下来我尝试了 GraphViz。从维基百科页面,该dot
格式用于有向图,我创建了一个点文件:
nx.write_dot(g, "g.dot")
This worked well and I had a dot file in my current directory that is 12 megabytes. Next I ran the dot
program (part of graphviz to create a postscript file):
这很有效,我在当前目录中有一个 12 兆字节的点文件。接下来我运行dot
程序(graphviz 的一部分来创建一个 postscript 文件):
dot -Tps g.dot -o g.ps
This slows down my computer, runs for a few minutes and then display Killed
in the terminal. So it never could execute... While reading the documentation for graphviz it seems that only undirected graphs were supported for large graph visualization.
这会减慢我的计算机速度,运行几分钟,然后Killed
在终端中显示。所以它永远无法执行......在阅读graphviz的文档时,似乎只有无向图才支持大图可视化。
Question: With these two unsuccessful attempts can anyone show me how to visualize my large graph using python and networkx with about 215,000 vertices and 215,000 edges? I suspect as with Graphviz I will have to output into an intermediate format (although this shouldn't be that hard it won't be as easy as a builtin function) and then use another tool to read the intermediate format and then output a visualization.
问题:由于这两次不成功的尝试,谁能告诉我如何使用 python 和 networkx 可视化我的大图,大约有 215,000 个顶点和 215,000 个边?我怀疑与 Graphviz 一样,我将不得不输出为中间格式(尽管这应该不会那么难,但不会像内置函数那样容易),然后使用另一种工具读取中间格式,然后输出可视化.
So, I am looking for the following:
所以,我正在寻找以下内容:
- Output graph from networkx into an intermediate format
- With new package/software/tool (ideally python-interactive) read intermediate format and visualize the large graph
- 从 networkx 输出图为中间格式
- 使用新的包/软件/工具(最好是 python-interactive)读取中间格式并可视化大图
If you need more information let me know!
如果您需要更多信息,请告诉我!
采纳答案by Vikram
from matplotlib import pylab
import networkx as nx
def save_graph(graph,file_name):
#initialze Figure
plt.figure(num=None, figsize=(20, 20), dpi=80)
plt.axis('off')
fig = plt.figure(1)
pos = nx.spring_layout(graph)
nx.draw_networkx_nodes(graph,pos)
nx.draw_networkx_edges(graph,pos)
nx.draw_networkx_labels(graph,pos)
cut = 1.00
xmax = cut * max(xx for xx, yy in pos.values())
ymax = cut * max(yy for xx, yy in pos.values())
plt.xlim(0, xmax)
plt.ylim(0, ymax)
plt.savefig(file_name,bbox_inches="tight")
pylab.close()
del fig
#Assuming that the graph g has nodes and edges entered
save_graph(g,"my_graph.pdf")
#it can also be saved in .svg, .png. or .ps formats
This answers your first issue. Networkx does not have the facility to zoom into nodes. Use Gephi for this functionality. Gephi accepts an edge list in CSV format and produces a visualization, where zooming can be done interactively.
这回答了你的第一个问题。Networkx 没有放大节点的功能。将 Gephi 用于此功能。Gephi 接受 CSV 格式的边缘列表并生成可视化,其中可以交互地进行缩放。