Alan Valejo
Alan Valejo

Reputation: 1315

Saving plots in igraph as eps format

Saving plots in igraph as eps format.

I need save a plots in igraph as image, but eps format.

I have this:

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)

I try this:

(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)

But (1) and (2) not work, can someone help me?

Upvotes: 3

Views: 2687

Answers (2)

Tamás
Tamás

Reputation: 48071

EPS is not very different from PS; basically, EPS (encapsulated PostScript) files are just PS files with a few additional restrictions. One such restriction is that the EPS file must contain a BoundingBox DSC comment that specifies the "bounds" of the drawing. You can usually convert a PS file to an EPS file with an appropriate converter that "infers" the bounds and places the additional comment in the file. So, I would first try plotting to PS and then convert it to EPS using ps2eps. An alternative is to produce an SVG drawing and then ask an SVG-aware tool (such as Inkscape) to convert it to EPS. Inkscape even has command line arguments that allow you to do this in a batch script.

Upvotes: 1

DrV
DrV

Reputation: 23530

Unfortunately, igraph's plot only supports PDF, PNG, SVG, and PS. So that is why your method #1 fails.

As what comes to your method #2, there is nothing to draw the igraph graph onto the matplotlib drawing areas. It is, however, possible to draw an igraph graph onto a Cairo SVG canvas created by matplotlib (the renderer context needs to be given as an argument to plot).

The latter trick is outlined here: https://lists.nongnu.org/archive/html/igraph-help/2010-06/msg00098.html

So, it is possible while not very straightforward. Another question is if you could avoid the use of EPS, it is usually possible with modern tools.

Upvotes: 2

Related Questions