from PIL import Image import io import matplotlib.pyplot as plt import networkx as nx def plot_graph(self, G): plt.figure(figsize=(3, 2)) nx.draw(G, with_labels=True, node_color="lightblue", font_weight="bold") plt.axis("off") plt.tight_layout() # Save the figure to a BytesIO buffer buf = io.BytesIO() plt.savefig(buf, format="png") plt.close() # Move to the beginning of the buffer before reading buf.seek(0) # Load the image from the buffer into a PIL Image image = Image.open(buf) return image