将 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,没有什么可以将 igraph
图形绘制到 matplotlib
绘图区域。但是,可以将 igraph
图形绘制到由 matplotlib
创建的 Cairo SVG Canvas 上(需要将渲染器上下文作为参数提供给 plot
)。
此处概述了后一种技巧:https://lists.nongnu.org/archive/html/igraph-help/2010-06/msg00098.html
因此,这是可能的,但不是很简单。另一个问题是,如果您可以避免使用 EPS,通常可以使用现代工具。
关于python - 将 igraph 中的图保存为 eps 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878741/