Skip to content

Lack of Support for Directed Graph Visualization with Arrowheads #114

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

Open
MohammadRaziei opened this issue Mar 23, 2025 · 0 comments
Open

Comments

@MohammadRaziei
Copy link

The pygsp library currently does not support the visualization of directed graphs with arrowheads on edges. When using the plot_graph function to visualize a graph created from a directed adjacency matrix, the edges are displayed as undirected lines without any indication of directionality (e.g., arrowheads).

For example, consider the following directed graph defined by the adjacency matrix:

import numpy as np
import pygsp as gsp

# Define a directed graph using an adjacency matrix
W = np.array([
    [0, 3, 7, 0, 0],  # Node 1 → Node 2 (weight=3), Node 1 → Node 3 (weight=7)
    [0, 0, 0, 5, 0],  # Node 2 → Node 4 (weight=5)
    [0, 0, 0, 2, 0],  # Node 3 → Node 4 (weight=2)
    [0, 0, 0, 0, 4],  # Node 4 → Node 5 (weight=4)
    [0, 0, 0, 0, 0]   # Node 5 has no outgoing edges
])

G = gsp.graphs.Graph(W)  # Create a graph from the adjacency matrix
G.set_coordinates(seed=42)  # Set coordinates for visualization

When visualizing this graph using gsp.plotting.plot_graph(G), the edges are displayed as undirected lines, even though the adjacency matrix explicitly defines directed edges.

Expected Behavior:

  • The edges should be displayed with arrowheads to indicate the direction of each edge.
  • The visualization should clearly distinguish between directed and undirected graphs.

Current Behavior:

  • The edges are displayed as undirected lines, regardless of the adjacency matrix being directed or undirected.

Proposed Solution:

  • Add an optional parameter (e.g., directed=True) to the plot_graph function to enable visualization of directed graphs with arrowheads.
  • Use Matplotlib's arrow functionality to draw directed edges with customizable arrow styles (e.g., arrowheads, colors, widths).

Workaround:
Currently, users can manually draw directed edges using Matplotlib's arrow function, but this requires additional code and is not integrated into the plot_graph functionality.

Example Workaround Code:

import matplotlib.pyplot as plt

for i, (u, v) in enumerate(G.get_edge_list()):
    x_start, y_start = G.coords[u]
    x_end, y_end = G.coords[v]
    plt.arrow(x_start, y_start, x_end - x_start, y_end - y_start,
              head_width=0.05, head_length=0.1, fc='gray', ec='gray',
              length_includes_head=True)
plt.show()

This workaround demonstrates how directed edges can be visualized, but it would be ideal to have this functionality built into pygsp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant