Python 将igraph中的绘图保存为eps格式

Python 将igraph中的绘图保存为eps格式,python,matplotlib,igraph,Python,Matplotlib,Igraph,将igraph中的绘图保存为eps格式 我需要在igraph中保存一个绘图作为图像,但不是eps格式 我有这个: def _plot(g, membership=None): visual_style = {} visual_style["vertex_size"] = 24 visual_style["layout"] = g.layout("kk") visual_style["bbox"] = (400, 300) visual_style["mar

将igraph中的绘图保存为eps格式

我需要在igraph中保存一个绘图作为图像,但不是eps格式

我有这个:

def _plot(g, membership=None):
    visual_style = {}
    visual_style["vertex_size"] = 24
    visual_style["layout"] = g.layout("kk")
    visual_style["bbox"] = (400, 300)
    visual_style["margin"] = 20
    for vertex in g.vs():
        vertex["label"] = vertex.index + 1
    if membership is not None:
        for vertex in g.vs():
            if(membership[vertex.index] == 0):
                vertex["color"] = "gray"
            else:
                vertex["color"] = "white"
            if(membership[vertex.index] == 0):
                vertex["shape"] = "circle"
            else:
                vertex["shape"] = "rectangle"
        visual_style["vertex_color"] = g.vs["color"]
        visual_style["vertex_shape"] = g.vs["shape"]
    visual_style["vertex_label_color"] = "black"
    visual_style["vertex_label_dist"] = -0.4
    igraph.plot(g, **visual_style)

if __name__ == "__main__":
    g = igraph.Nexus.get("karate")
    cl = g.community_fastgreedy()
    membership = cl.as_clustering(2).membership
    _plot(g, membership)
我试试这个:

(1)
igraph.plot(g,'graph.eps',**visual_style)

(2)
import matplotlib.pyplot as plt
igraph.plot(g,'graph.eps',**visual_style)
plt.savefig('graph.eps', format='eps', dpi=1000)

但是(1)和(2)不起作用,有人能帮我吗?

不幸的是,
igraph
plot
只支持PDF、PNG、SVG和PS。这就是你的方法1失败的原因

至于您的方法#2,在
matplotlib
绘图区域上绘制
igraph
图形是不可能的。但是,可以在由
matplotlib
创建的Cairo SVG画布上绘制
igraph
图形(渲染器上下文需要作为
plot
的参数提供)

后一个技巧概述如下:


因此,这是可能的,但不是很简单。另一个问题是,如果您可以避免使用EPS,那么使用现代工具通常是可行的。

EPS与PS没有太大区别;基本上,EPS(封装的PostScript)文件只是带有一些附加限制的PS文件。此类限制之一是EPS文件必须包含指定图形“边界”的边界框DSC注释。通常可以使用适当的转换器将PS文件转换为EPS文件,该转换器“推断”边界并在文件中放置附加注释。所以,我会先尝试打印到PS,然后使用。另一种方法是生成SVG图形,然后要求SVG感知工具(例如)将其转换为EPS。甚至还有允许您在批处理脚本中执行此操作的命令行参数