The immediate problem is that within the loop the object is modified but never returned. That can be fixed.
using Plots
# Sample data based on your example
vertices = [[2, 3]]
adj = Dict(
[2, 3] => [[3, 2], [1, 0], [2, 1]]
)
# 1. Initialize an empty plot (or a scatter plot of vertices)
# 'legend = false' keeps it clean since loops create many series
plot(legend = false, aspect_ratio = :equal)
# 2. Loop and add the lines
for v in vertices
for a in adj[v]
# X-coordinates: [v[1], a[1]]
# Y-coordinates: [v[2], a[2]]
plot!([v[1], a[1]], [v[2], a[2]], color = :blue, linewidth = 2)
end
end
# 3. Scatter the vertices on top so they stand out
# (Using unzip-style syntax to get all X and Y coordinates)
xs = [v[1] for v in vertices]
ys = [v[2] for v in vertices]
scatter!(xs, ys, color = :red, markersize = 6)
# 4. CRITICAL: Force the plot to display
current()
But that’s not what you should be doing to plot graph objects. It’s fine for toy examples but for real work there’s another toolkit.
using Graphs
using GraphRecipes
using Plots
# Your original data
adj = Dict(
[2, 3] => [[3, 2], [1, 0], [2, 1]]
)
# 1. Properly collect unique coordinate vectors without flattening them
all_vertices = unique(vcat(collect(keys(adj)), collect(values(adj))...))
# 2. Create the mapping dictionaries
vertex_to_id = Dict(v => i for (i, v) in enumerate(all_vertices))
id_to_vertex = Dict(i => v for (i, v) in enumerate(all_vertices))
# 3. Initialize the graph
g = SimpleGraph(length(all_vertices))
# 4. Add the edges
for (v, neighbors) in adj
for n in neighbors
add_edge!(g, vertex_to_id[v], vertex_to_id[n])
end
end
# 5. Extract coordinates (This will work perfectly now!)
locs_x = [id_to_vertex[i][1] for i in 1:nv(g)]
locs_y = [id_to_vertex[i][2] for i in 1:nv(g)]
# 6. Plot
graphplot(g,
x = locs_x,
y = locs_y,
curves = false,
nodeshape = :circle,
names = [string(v) for v in all_vertices],
fontsize = 8,
nodesize = 0.15)