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

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!