GraphMakie.jl

repository·master·Indexed 41 days ago

https://github.com/makieorg/graphmakie.jl

A plotting recipe for the Makie.jl ecosystem used to visualize graphs. It bridges graph data structures from Graphs.jl with Makie backends like GLMakie, CairoMakie, or WGLMakie, and utilizes NetworkLayout.jl for node positioning. It provides the `graphplot` function for rendering and supports predefined interactions for highlighting and dragging nodes and edges.

Tokens
1.1K
Snippets
5
Records
7
Agent score
50%

What's inside GraphMakie

  1. Overview of GraphMakie

    master
    GraphMakie.jl is a plotting recipe for visualizing graphs within the Makie ecosystem. It integrates with Graphs.jl for graph data structures and utilizes NetworkLayout.jl for graph layout algorithms. Because it is a recipe, it does not include a rendering engine itself; you must load a backend like GLMakie or CairoMakie to see the plots.
  2. Implement custom interactions via the Interaction Interface

    master

    If predefined interactions do not meet your needs, you can implement custom interactions. The interface follows a pattern: create a handler type, provide an action function, and register it with the axes.

    GraphMakie provides specific handler types for different interaction modes:

    Click Interactions

    • NodeClickHandler
    • EdgeClickHandler

    Hover Interactions

    • NodeHoverHandler
    • EdgeHoverHandler

    Drag Interactions

    • NodeDragHandler
    • EdgeDragHandler
  3. Configure Network Layouts in GraphMakie

    master

    Layouts determine the positions of nodes in a graph. GraphMakie uses algorithms from NetworkLayout.jl.

    A layout must be a function with the signature f(g::AbstractGraph) -> pos::Vector{Point}. You can use predefined layouts or provide your own custom layout function. You can also integrate layouts from other packages like LayeredLayouts.jl by wrapping them to return a vector of Point objects.

    using LayeredLayouts
    using GraphMakie
    using Graphs
    
    function mylayout(g::SimpleGraph)
       xs, ys, _ = solve_positions(Zarate(), g)
       return Point.(zip(xs, ys))
    end
    
    g = wheel_graph(10)
    f, ax, p = graphplot(g, layout=mylayout)
  4. Add predefined interactions to a graph plot

    master

    GraphMakie includes pre-built interaction types for highlighting and dragging nodes and edges. To use them, you must register them to the axis (ax) using register_interaction!. It is often necessary to deregister_interaction!(ax, :rectanglezoom) first if you want to avoid conflicts with default Makie interactions.

    Available predefined interactions:

    • NodeHoverHighlight(p): Highlights nodes on hover.
    • EdgeHoverHighlight(p): Highlights edges on hover.
    • NodeDrag(p): Enables dragging nodes.
    • EdgeDrag(p): Enables dragging edges.
    using GLMakie
    using GraphMakie
    using Graphs
    
    g = wheel_graph(10)
    f, ax, p = graphplot(g, edge_width=[3 for i in 1:ne(g)],
                         node_size=[10 for i in 1:nv(g)])
    
    deregister_interaction!(ax, :rectanglezoom)
    register_interaction!(ax, :nhover, NodeHoverHighlight(p))
    register_interaction!(ax, :ehover, EdgeHoverHighlight(p))
    register_interaction!(ax, :ndrag, NodeDrag(p))
    register_interaction!(ax, :edrag, EdgeDrag(p))
  5. Install GraphMakie.jl

    master

    To install GraphMakie, use the Julia package manager. Note that GraphMakie is a plotting recipe and requires a Makie backend (such as GLMakie, CairoMakie, or WGLMakie) to actually render the plots.

    using Pkg
    Pkg.add("GraphMakie")
  6. Use the `graphplot` recipe for Graphs.jl

    master

    GraphMakie provides a plotting recipe specifically for graph types from Graphs.jl. You can use the graphplot function to visualize a graph object.

    To customize the appearance of individual elements (nodes or edges), you can pass a Vector of values. Alternatively, you can pass a Dict or DefaultDict where the keys are the Int index of the element (for nodes) or the Edge object (for edges) to specify configurations for specific elements while letting others use default values.

    using Graphs
    using GraphMakie
    
    g = wheel_graph(10)
    # Basic usage
    f, ax, p = graphplot(g)
  7. Plot a graph using graphplot

    master

    To plot a graph, you need to use Graphs.jl to define your graph structure, GraphMakie.jl to provide the plotting recipe, and a Makie backend (e.g., GLMakie) to display the result. The primary function for rendering is graphplot(g), where g is a graph object from Graphs.jl.

    using GLMakie
    using GraphMakie
    using Graphs
    
    g = complete_graph(10)
    graphplot(g)