Network Graphs in Julia

How to make Network Graphs in Julia with Plotly. One examples of a network graph with LightGraphs.jl


In this example we show how to visualize a network graph created using LightGraphs.jl.

Install the Julia library LightGraphs with import Pkg; Pkg.add("LightGraphs").

Create random graph

using LightGraphs

G = euclidean_graph(200, 2, cutoff=0.125)[1]
LightGraphs.SimpleGraphs.SimpleGraph{Int64}(860, [[25, 28, 39, 43, 47, 59, 89, 112, 113, 121, 125, 182, 198], [90, 111, 134, 166], [29, 82, 88, 96, 163, 194], [63, 79, 84, 135, 145, 157, 193, 199], [58, 79, 81, 102, 109, 136, 145, 149, 175], [10, 110, 180], [17, 18, 31, 75, 98, 130, 195], [34, 57, 83, 91, 115, 133, 181, 185, 190], [50, 55, 67, 86, 132, 137, 139, 142, 153, 155, 191], [6, 23, 52, 72, 110, 158, 170, 180], [151, 174, 176], [21, 36, 37, 45, 53, 101, 120, 138, 159, 165, 169, 173], [95, 97, 151, 160, 178, 186], [46, 49, 73, 76, 119, 167, 196], [36, 40, 52, 54, 128, 138, 158, 168], [19, 94, 97, 100, 107, 117, 118, 131, 143, 146, 147, 161, 179], [7, 18, 26, 31, 63, 75], [7, 17, 31, 75, 98, 130, 195], [16, 94, 100, 107, 117, 118, 131, 143, 146, 161, 179], [41, 65, 96, 124, 139, 140, 155, 191], [12, 45, 92, 101, 159, 163, 165, 173], [51, 60, 85, 106, 129], [10, 52, 72, 110, 158, 170, 180, 197], [33, 35, 69, 167, 171], [1, 28, 43, 78, 112, 125, 182, 184, 188], [17, 31, 61, 63, 75, 104, 114, 156, 183], [28, 32, 39, 42, 43, 59, 112, 121, 152, 182], [1, 25, 27, 32, 39, 43, 47, 59, 89, 112, 113, 121, 125, 182, 198], [3, 82, 88, 96, 107, 194], [97, 99, 144, 147], [7, 17, 18, 26, 63, 75, 175], [27, 28, 39, 42, 43, 44, 59, 89, 103, 112, 121, 141, 152, 198], [24, 35, 69], [8, 83, 91, 133, 140, 155, 185, 191], [24, 33, 69, 171], [12, 15, 37, 45, 53, 66, 120, 138, 165, 169], [12, 36, 45, 53, 66, 101, 120, 138, 148, 159, 162, 165, 169, 173], [70, 71, 80, 104, 114, 123, 127, 154, 156, 177], [1, 27, 28, 32, 43, 44, 47, 59, 89, 103, 112, 113, 121, 125, 182, 198], [15, 54, 128, 168], [20, 55, 82, 86, 96, 139, 163, 191], [27, 32, 43, 71, 80, 152, 190], [1, 25, 27, 28, 32, 39, 42, 112, 121, 125, 152, 182, 188], [32, 39, 59, 89, 103, 112, 113, 121, 141, 198], [12, 21, 36, 37, 53, 66, 101, 120, 138, 159, 165, 169, 173], [14, 49, 73, 119, 150, 196], [1, 28, 39, 59, 89, 112, 113, 121, 125, 182, 198], [56, 64, 68, 77, 81, 102, 116, 126, 172], [14, 46, 73, 76, 119, 150, 196], [9, 55, 61, 67, 115, 132, 137, 142, 153, 155, 156, 183, 185], [22, 57, 60, 106, 129, 181], [10, 15, 23, 54, 72, 110, 128, 158, 170, 180], [12, 36, 37, 45, 66, 101, 120, 138, 165, 169], [15, 40, 52, 128, 168], [9, 41, 50, 67, 86, 132, 137, 139, 142, 153, 155], [48, 64, 68, 77, 116, 126, 172, 197, 200], [8, 51, 91, 181, 185, 190], [5, 81, 102, 109, 116, 126, 136, 149, 172, 175], [1, 27, 28, 32, 39, 44, 47, 89, 103, 112, 113, 121, 125, 141, 198], [22, 51, 106, 129, 141], [26, 50, 67, 115, 127, 132, 137, 153, 156, 183], [73, 76, 195, 196], [4, 17, 26, 31, 79, 145, 157, 175, 183, 199], [48, 56, 68, 72, 77, 197], [20, 82, 94, 96, 100, 124, 131], [36, 37, 45, 53, 101, 120, 138, 158, 165, 169], [9, 50, 55, 61, 86, 132, 137, 139, 142, 153, 155, 157], [48, 56, 64, 77, 81, 102, 116, 126, 162, 172], [24, 33, 35, 167, 171], [38, 80, 104, 105, 114, 122, 123, 154, 164, 177], [38, 42, 80, 127, 152, 154, 188], [10, 23, 52, 64, 158, 170, 197], [14, 46, 49, 62, 76, 167, 196], [90, 134, 164, 166, 184, 188], [7, 17, 18, 26, 31, 98], [14, 49, 62, 73, 119, 136, 149, 196], [48, 56, 64, 68, 197], [25, 182], [4, 5, 63, 84, 109, 135, 145, 148, 157, 175, 193, 199], [38, 42, 70, 71, 154, 164, 177, 184, 188], [5, 48, 58, 68, 84, 102, 109, 126, 135, 148, 162, 172, 193], [3, 29, 41, 65, 88, 96, 107, 194], [8, 34, 91, 133, 140, 155, 181, 185], [4, 79, 81, 92, 109, 135, 145, 148, 157, 173, 193, 199], [22, 106, 129], [9, 41, 55, 67, 139, 142, 155], [98, 122, 130, 187, 189], [3, 29, 82, 96, 163, 194], [1, 28, 32, 39, 44, 47, 59, 103, 112, 113, 121, 125, 141, 198], [2, 74, 111, 134, 166], [8, 34, 57, 83, 133, 181, 185], [21, 84, 135, 159, 163, 173, 199], Int64[], [16, 19, 65, 100, 107, 117, 118, 131, 143, 146, 161, 179], [13, 97, 99, 151, 186], [3, 20, 29, 41, 65, 82, 88, 107, 194], [13, 16, 30, 95, 99, 118, 131, 143, 144, 147, 179, 186], [7, 18, 75, 87, 130, 189, 195], [30, 95, 97, 118, 131, 143, 144, 147, 179, 186], [16, 19, 65, 94, 107, 117, 118, 131, 146, 161, 179], [12, 21, 37, 45, 53, 66, 120, 138, 148, 159, 165, 169, 173], [5, 48, 58, 68, 81, 109, 116, 126, 136, 149, 172], [32, 39, 44, 59, 89, 113, 121, 141, 198], [26, 38, 70, 105, 114, 123, 127, 154, 156, 177], [70, 104, 114, 122, 123, 154, 177], [22, 51, 60, 85, 129, 141], [16, 19, 29, 82, 94, 96, 100, 117, 131, 146, 161, 194], [140, 151, 160, 176, 178, 192], [5, 58, 79, 81, 84, 102, 126, 135, 145, 148, 162, 172, 193], [6, 10, 23, 52, 158, 170, 180], [2, 90, 166], [1, 25, 27, 28, 32, 39, 43, 44, 47, 59, 89, 113, 121, 125, 182, 198], [1, 28, 39, 44, 47, 59, 89, 103, 112, 121, 125, 198], [26, 38, 70, 104, 105, 123, 127, 154, 156, 177, 183], [8, 50, 61, 127, 132, 137, 153, 156, 183, 185, 190], [48, 56, 58, 68, 102, 126, 149, 150, 172, 200], [16, 19, 94, 100, 107, 146, 161], [16, 19, 94, 97, 99, 100, 131, 143, 144, 147, 161, 179, 186], [14, 46, 49, 76, 150, 196, 200], [12, 36, 37, 45, 53, 66, 101, 138, 159, 165, 169, 173], [1, 27, 28, 32, 39, 43, 44, 47, 59, 89, 103, 112, 113, 125, 141, 182, 198], [70, 87, 105, 123, 177], [38, 70, 104, 105, 114, 122, 154, 177], [20, 65, 140, 155, 191, 192], [1, 25, 28, 39, 43, 47, 59, 89, 112, 113, 121, 182], [48, 56, 58, 68, 81, 102, 109, 116, 136, 149, 150, 172], [38, 61, 71, 104, 114, 115, 156], [15, 40, 52, 54, 168], [22, 51, 60, 85, 106], [7, 18, 87, 98, 189, 195], [16, 19, 65, 94, 97, 99, 100, 107, 118, 143, 147, 161, 179, 186], [9, 50, 55, 61, 67, 115, 137, 142, 153, 156, 157, 183], [8, 34, 83, 91, 181, 185], [2, 74, 90, 164, 166, 184], [4, 79, 81, 84, 92, 109, 145, 148, 157, 173, 193, 199], [5, 58, 76, 102, 126, 149, 175, 196], [9, 50, 55, 61, 67, 115, 132, 142, 153, 155, 156, 183, 185], [12, 15, 36, 37, 45, 53, 66, 101, 120, 165, 169], [9, 20, 41, 55, 67, 86, 142, 155, 191], [20, 34, 83, 108, 124, 155, 178, 191, 192], [32, 44, 59, 60, 89, 103, 106, 121, 198], [9, 50, 55, 67, 86, 132, 137, 139, 153, 155, 157], [16, 19, 94, 97, 99, 118, 131, 144, 147, 161, 179, 186], [30, 97, 99, 118, 143, 147, 179, 186], [4, 5, 63, 79, 84, 109, 135, 148, 157, 175, 193, 199], [16, 19, 94, 100, 107, 117, 161], [16, 30, 97, 99, 118, 131, 143, 144, 179, 186], [37, 79, 81, 84, 101, 109, 135, 145, 162, 173, 193], [5, 58, 76, 102, 116, 126, 136, 150, 172, 175, 196], [46, 49, 116, 119, 126, 149, 196, 200], [11, 13, 95, 108, 160, 174, 176, 178], [27, 32, 42, 43, 71, 190], [9, 50, 55, 61, 67, 115, 132, 137, 142, 155, 156, 183, 185], [38, 70, 71, 80, 104, 105, 114, 123, 164, 177, 188], [9, 20, 34, 50, 55, 67, 83, 86, 124, 137, 139, 140, 142, 153, 191], [26, 38, 50, 61, 104, 114, 115, 127, 132, 137, 153, 183], [4, 63, 67, 79, 84, 132, 135, 142, 145, 193, 199], [10, 15, 23, 52, 66, 72, 110, 170], [12, 21, 37, 45, 92, 101, 120, 163, 165, 169, 173], [13, 108, 151, 176, 178, 192], [16, 19, 94, 100, 107, 117, 118, 131, 143, 146, 179], [37, 68, 81, 109, 148, 172], [3, 21, 41, 88, 92, 159, 194], [70, 74, 80, 134, 154, 166, 177, 184, 188], [12, 21, 36, 37, 45, 53, 66, 101, 120, 138, 159, 169, 173], [2, 74, 90, 111, 134, 164, 184], [14, 24, 69, 73, 171], [15, 40, 54, 128], [12, 36, 37, 45, 53, 66, 101, 120, 138, 159, 165], [10, 23, 52, 72, 110, 158, 180], [24, 35, 69, 167], [48, 56, 58, 68, 81, 102, 109, 116, 126, 149, 162], [12, 21, 37, 45, 84, 92, 101, 120, 135, 148, 159, 165], [11, 151, 176], [5, 31, 58, 63, 79, 136, 145, 149], [11, 108, 151, 160, 174, 178], [38, 70, 80, 104, 105, 114, 122, 123, 154, 164], [13, 108, 140, 151, 160, 176, 192], [16, 19, 94, 97, 99, 100, 118, 131, 143, 144, 147, 161, 186], [6, 10, 23, 52, 110, 170], [8, 51, 57, 83, 91, 133, 185, 190], [1, 25, 27, 28, 39, 43, 47, 78, 112, 121, 125], [26, 50, 61, 63, 114, 115, 132, 137, 153, 156], [25, 74, 80, 134, 164, 166, 188], [8, 34, 50, 57, 83, 91, 115, 133, 137, 153, 181, 190], [13, 95, 97, 99, 118, 131, 143, 144, 147, 179], [87, 189], [25, 43, 71, 74, 80, 154, 164, 184], [87, 98, 130, 187], [8, 42, 57, 115, 152, 181, 185], [9, 20, 34, 41, 124, 139, 140, 155], [108, 124, 140, 160, 178], [4, 79, 81, 84, 109, 135, 145, 148, 157, 199], [3, 29, 82, 88, 96, 107, 163], [7, 18, 62, 98, 130], [14, 46, 49, 62, 73, 76, 119, 136, 149, 150], [23, 56, 64, 72, 77], [1, 28, 32, 39, 44, 47, 59, 89, 103, 112, 113, 121, 141], [4, 63, 79, 84, 92, 135, 145, 157, 193], [56, 116, 119, 150]])

Create Edges

Add edges as disconnected lines in a single trace and nodes as a scatter trace

using PlotlyJS, LightGraphs
import GraphPlot  # for spring_layout

# Generate a random layout
G =  LightGraphs.euclidean_graph(200, 2, cutoff=0.125)[1]
# Position nodes
pos_x, pos_y = GraphPlot.spring_layout(G)

# Create plot points
edge_x = []
edge_y = []

for edge in edges(G)
    push!(edge_x, pos_x[src(edge)])
    push!(edge_x, pos_x[dst(edge)])
    push!(edge_y, pos_y[src(edge)])
    push!(edge_y, pos_y[dst(edge)])
end

#  Color node points by the number of connections.
color_map = [size(neighbors(G, node))[1] for node in 1:200]

# Create edges
edges_trace = scatter(
    mode="lines",
    x=edge_x,
    y=edge_y,
    line=attr(
        width=0.5,
        color="#888"
    ),
)

# Create nodes
nodes_trace = scatter(
    x=pos_x,
    y=pos_y,
    mode="markers",
    text = [string("# of connections: ", connection) for connection in color_map],
    marker=attr(
        showscale=true,
        colorscale=colors.imola,
        color=color_map,
        size=10,
        colorbar=attr(
            thickness=15,
            title="Node Connections",
            xanchor="left",
            titleside="right"
      )
    )
)

# Create Plot
plot(
    [edges_trace, nodes_trace],
    Layout(
        hovermode="closest",
        title="Network Graph made with Julia",
        titlefont_size=16,
        showlegend=false,
        showarrow=false,
        xaxis=attr(showgrid=false, zeroline=false, showticklabels=false),
        yaxis=attr(showgrid=false, zeroline=false, showticklabels=false)
    )
)

Reference

See https://plotly.com/julia/reference/scatter/ for more information and chart attribute options!