Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export graphs in a single PDF file #430

Open
lichengzhang1 opened this issue Oct 24, 2023 · 3 comments · May be fixed by #451
Open

Export graphs in a single PDF file #430

lichengzhang1 opened this issue Oct 24, 2023 · 3 comments · May be fixed by #451
Assignees
Labels
enhancement New feature or request external user External user issue

Comments

@lichengzhang1
Copy link

lichengzhang1 commented Oct 24, 2023

Hello,

I see that when converting all the images to PDF, they are all separate. In fact, in academic writing, sometimes we need to put them together (though usually not too many, typically less than 200 images). I find it relatively easy to do this using Mathematica.

g = Import["http://users.cecs.anu.edu.au/~bdm/data/highlyirregular12.g6"];

Grid[Partition[
  Table[Labeled[
    Graph[g[[i]], PlotTheme -> "Monochrome", 
     VertexSize -> .17 {1, 1}, ImageSize -> {50, 30}, 
     VertexStyle -> GrayLevel[.6]], 
    Style[i, FontFamily -> "Baskerville"], Bottom], {i, Length[g]}], 
  UpTo[10]]]

image

See also https://mathematica.stackexchange.com/questions/271553/how-to-fit-many-graphs-neatly-into-a-paper.

I'm wondering if this can also be done with this software or with Python. Of course, a single-page PDF may not accommodate all of them, so it needs to be intelligently adjusted, including the number of images in each row and the number of columns on each page.

@lichengzhang1 lichengzhang1 added enhancement New feature or request external user External user issue labels Oct 24, 2023
@atilaajones
Copy link
Contributor

Hello @lichengzhang1. We have already evaluated your proposal and will implement it for the next version of the software.

Thank you for your cooperation. It's always a pleasure to receive feedback and suggestions from users.

@atilaajones atilaajones changed the title Can all the images of graphs be neatly arranged in a single PDF file? Export graphs in a single PDF file Dec 6, 2023
@atilaajones atilaajones linked a pull request Dec 11, 2023 that will close this issue
@lichengzhang1
Copy link
Author

lichengzhang1 commented May 27, 2024

Now, I write a python code which can handle this.

import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.backends.backend_pdf import PdfPages
import math

def read_graph6_file(filepath):
    with open(filepath, 'r') as file:
        graph_data = file.read().splitlines()
    graphs = [nx.from_graph6_bytes(line.encode('ascii')) for line in graph_data]
    return graphs

def draw_graphs_to_pdf(graphs, columns, pdf_path):
    rows_per_page = 6  # Number of rows per page
    graphs_per_page = rows_per_page * columns
    num_pages = math.ceil(len(graphs) / graphs_per_page)

    plt.rcParams['text.usetex'] = True  # Enable LaTeX rendering

    with PdfPages(pdf_path) as pdf:
        for page in range(num_pages):
            start = page * graphs_per_page
            end = start + graphs_per_page
            current_graphs = graphs[start:end]

            num_rows = math.ceil(len(current_graphs) / columns)
            fig, axes = plt.subplots(nrows=num_rows, ncols=columns, figsize=(15, num_rows * 3))

            # Ensure axes is always a 2D array
            if num_rows == 1:
                axes = [axes]
            if columns == 1:
                axes = [[ax] for ax in axes]

            for ax, graph, i in zip([ax for row in axes for ax in row], current_graphs, range(start + 1, end + 1)):
                #pos = nx.spring_layout(graph)
                pos = nx.circular_layout(graph)
                nx.draw_networkx(
                    graph,
                    pos,
                    ax=ax,
                    with_labels=True,
                    labels={node: rf'${node}$' for node in graph.nodes()},
                    node_size=250,
                    node_color='#D3D3D3',
                    edgecolors='black',
                    linewidths=2,
                    font_size=10,
                    font_color='black',
                    font_weight='bold',
                    edge_color='black',
                    width=1.5
                )
                ax.set_title(f'Graph {i}', fontsize=10)
                ax.axis('on')  # Display frame
                ax.set_xticks([])
                ax.set_yticks([])

            # Remove empty axes
            for ax in [ax for row in axes for ax in row][len(current_graphs):]:
                fig.delaxes(ax)

            plt.tight_layout(pad=3.0)
            if len(current_graphs) > 0:  # Avoid saving empty pages
                pdf.savefig(fig)
            plt.close()

# Example usage:
columns = 5      # Number of columns per page
pdf_path = './graph6c.pdf'  # Save to current working directory

graphs = read_graph6_file("graph6c.g6") # download from https://users.cecs.anu.edu.au/~bdm/data/graphs.html
draw_graphs_to_pdf(graphs, columns, pdf_path)

print(f"PDF saved to: {pdf_path}")

image

@fsoupimenta
Copy link
Member

fsoupimenta commented Jun 3, 2024

Hello @lichengzhang1, hope you're well!

Thanks for your implementation. We ended up not giving this issue that much attention because we are working on a complete refactoring of the software architecture #452, in order to ensure quality and facilitate the implementation of new features.

However, we've recently been working on some requests again, and we're about to update a new release including this new feature export functionality. You can follow its development on #451 , where it can be tested on your branch.

Feel free to request new features or report any problem, and also open pull requests.

Thank you for your support, it's great to have you here 😊!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request external user External user issue
Projects
Status: In progress
Development

Successfully merging a pull request may close this issue.

5 participants