How to store images of graphs in a PDF?

asked 2024-03-18 07:57:45 +0100

licheng gravatar image

updated 2024-03-18 09:27:39 +0100

I know that I can convert a single graph to PDF using the command.

s = 'J~f[MN@PQg?'
g = Graph(s, sparse=True); 
g.plot(layout="planar").save("g.pdf")

I want to highlight the edges of some graphs in red and then store them separately in a PDF with different pages.

s = 'J~f[MN@PQg?'
g = Graph(s, sparse=True); 
g.plot(layout="planar",save_pos=True)
g.delete_vertex(2)
H = graphs.CycleGraph(10)
L = { tuple(g.edges(labels=False,sort_vertices=True,sort=True)) for g in g.subgraph_search_iterator(H, induced=False) }
print(len(L))
cylce_lists = list(map(list, L))
def plot_with_highlight(G, highlight_edges):
    highlight_vertices = list(set().union(*highlight_edges))
    plot(G, edge_colors={'red': highlight_edges}, vertex_colors={'red': highlight_vertices}).show()
for highlight_edges in cylce_lists:
    plot_with_highlight(g, highlight_edges)

I got six figures. My goal is to print. It would be perfect if two images could be placed on each row of a single PDF.

edit retag flag offensive close merge delete

Comments

A couple suggestions :

  • You might put your figures in separate files, then merging them with an external tool such as pdftk (I use the free software version disributed by Debian).

  • You may want to create graphics arrays to group your figures.

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-03-18 13:29:17 +0100 )edit

Nice. It seems that we must add return in plot_with_highlight.

def plot_with_highlight(G, highlight_edges):
    highlight_vertices = list(set().union(*highlight_edges))
    return plot(G,edge_colors={'red': highlight_edges}, vertex_colors={'red': highlight_vertices})

Now it is ok.

s=[]
for highlight_edges in cylce_lists:
    plot_with_highlight(g, highlight_edges)
    s.append(plot_with_highlight(g, highlight_edges))
G=graphics_array(s)
#G.show(axes=False, figsize=20)
#gm=graphics_array(ma).show(axes=False, figsize=20)
ma = [s[i:i+3] for i in range(0, len(s), 3)]
gm=graphics_array(ma)
gm.save("gma.pdf", dpi=500, axes=False,figsize=18)
licheng gravatar imagelicheng ( 2024-03-19 01:44:41 +0100 )edit