Python 显示图形而不使用pydot保存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4596962/
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
Display graph without saving using pydot
提问by
I am trying to display a simple graph using pydot.
我正在尝试使用 pydot 显示一个简单的图形。
My question is that is there any way to display the graph without writing it to a file as currently I use write function to first draw and then have to use the Image module to show the files.
我的问题是有什么方法可以在不将图形写入文件的情况下显示图形,因为目前我使用 write 函数先绘制,然后必须使用 Image 模块来显示文件。
However is there any way that the graph directly gets printed on the screen without being saved ??
但是,有没有办法将图形直接打印在屏幕上而不保存?
Also as an update I would like to ask in this same question that I observe that while the image gets saved very quickly when I use the show command of the Image module it takes noticeable time for the image to be seen .... Also sometimes I get the error that the image could'nt be opened because it was either deleted or saved in unavailable location which is not correct as I am saving it at my Desktop..... Does anyone know what's happening and is there a faster way to get the image loaded.....
同样作为更新,我想在同一个问题中提出我观察到,虽然当我使用 Image 模块的 show 命令时图像被保存得非常快,但需要花费大量时间才能看到图像......有时也是我收到无法打开图像的错误,因为它被删除或保存在不可用的位置,这是不正确的,因为我将它保存在我的桌面上..... 有谁知道发生了什么,有没有更快的方法获取图像加载.....
Thanks a lot....
非常感谢....
采纳答案by nimrodm
I'm afraid pydotuses graphvizto render the graphs. I.e., it runs the executable and loads the resulting image.
恐怕pydot用于graphviz渲染图形。即,它运行可执行文件并加载生成的图像。
Bottom line - no, you cannot avoid creating the file.
底线 - 不,您无法避免创建文件。
回答by Andre Holzner
Based on this answer(how to show images in python), here's a few lines:
基于这个答案(如何在 python 中显示图像),这里有几行:
gr = ... <pydot.Dot instance> ...
import tempfile, Image
fout = tempfile.NamedTemporaryFile(suffix=".png")
gr.write(fout.name,format="png")
Image.open(fout.name).show()
Imageis from the Python Imaging Library
Image来自Python 图像库
回答by Ioannis Filippidis
You can render the image from pydotby calling GraphViz's dotwithout writing any files to the disk. Then just plot it. This can be done as follows, assuming gis a pydotgraph:
您可以pydot通过调用GraphViz's来渲染图像,dot而无需将任何文件写入磁盘。然后只是绘制它。这可以按如下方式完成,假设g是一个pydot图表:
from cStringIO import StringIO
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import networkx as nx
# create a networkx graph
G = nx.MultiDiGraph()
G.add_nodes_from([1,2] )
G.add_edge(1, 2)
# convert from networkx -> pydot
pydot_graph = G.to_pydot()
# render pydot by calling dot, no file saved to disk
png_str = pydot_graph.create_png(prog='dot')
# treat the dot output string as an image file
sio = StringIO()
sio.write(png_str)
sio.seek(0)
img = mpimg.imread(sio)
# plot the image
imgplot = plt.imshow(img, aspect='equal')
plt.show(block=False)
This is particularly useful for directed graphs, because the matplotlibcapabilities of networkxare severely limitedfor such graphs.
这是为向图中特别有用,因为该matplotlib能力networkx被严重限制为这样的图。
See also this pull request, which introduces such capabilities directly to networkx. What remains is for someone to write an interface to load and plot the layout generated by GraphVizas the ones for MATLABon File Exchange GraphViz interface, MATLAB GraphViz Layout importer, GraphViz4Matlab.
另请参阅此拉取请求,它直接向networkx. 剩下的是人写负载的接口,并通过绘制生成的布局设计GraphViz为那些对MATLAB文件交换的GraphViz的接口,MATLAB GraphViz的布局进口商,GraphViz4Matlab。
回答by micahscopes
Here's a simple solution using IPython:
这是一个使用 IPython 的简单解决方案:
from IPython.display import Image, display
def view_pydot(pdot):
plt = Image(pdot.create_png())
display(plt)
Example usage:
用法示例:
import networkx as nx
to_pdot = nx.drawing.nx_pydot.to_pydot
pdot = to_pdot(nx.complete_graph(5))
view_pydot(pdot)

