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

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!