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