【问题标题】:Plot the directed graph in Julia with vertices in fixed positions by x y coordinates在 Julia 中绘制有向图,其中顶点在 x y 坐标的固定位置
【发布时间】:2021-10-08 01:16:44
【问题描述】:

我想在 Julia 中绘制一个基本结构如下图的图表,忽略其他不相关的元素。

A directed graph with vertices in fixed positions

表示有向边的矩阵如下:

mat_arcTwoNodes = [0    1    0    0    0    0    0    0    0    0;
                   0    0    0    0    0    1    0    0    0    0;
                   0    0    0    0    0    0    0    0    0    1;
                   1    0    0    0    1    0    0    0    0    0;
                   0    0    0    0    0    1    0    0    0    0;
                   1    0    0    1    0    0    0    1    0    0;
                   0    1    1    0    0    0    0    0    0    0;
                   0    0    0    0    0    0    0    0    1    0;
                   0    0    0    0    1    0    0    0    0    0;
                   0    0    0    0    0    1    1    0    1    0]

此外,我还有十个顶点的 x y 坐标:

vec_xNode = [1    3    8    2    4    5    6    7    9    9]
vec_yNode = [1    2    1    7    9    5    3    7    9    4]

我知道如何从矩阵中绘制有向图。

julia> using LightGraphs    
julia> using GraphPlot    
julia> G = DiGraph(mat_arcTwoNodes)
{10, 16} directed simple Int64 graph
julia> gplot(G)

但是如何使用 x y 坐标将顶点绘制在图片中的固定位置?

这种可视化在网络设计中很重要。虽然我们可以在没有特定可视化的情况下分析图表,但我不想用另一种方式来绘制图表来可视化最终结果。更全面的固定位置显示它们的相对距离。

【问题讨论】:

  • 目前在 GraphPlot.jl 中无法实现这一点,但请参阅 github.com/JuliaGraphs/GraphPlot.jl/issues/83 获取功能增强请求。
  • 谢谢。现在,我将绘制一个普通图形,其中点是顶点,线代表边。希望以后可以更优雅地做到这一点。
  • 我收回我之前的评论。显然有一种方法可以将locs_x, locs_y 传递给gplot。这目前没有记录,但很快就会修复。试一试(查看gplot 文档字符串)。

标签: graph julia coordinates directed-graph


【解决方案1】:

给你:

# Load the adjacency matrix and graph attributes
mat_arcTwoNodes = [0    1    0    0    0    0    0    0    0    0;
0    0    0    0    0    1    0    0    0    0;
0    0    0    0    0    0    0    0    0    1;
1    0    0    0    1    0    0    0    0    0;
0    0    0    0    0    1    0    0    0    0;
1    0    0    1    0    0    0    1    0    0;
0    1    1    0    0    0    0    0    0    0;
0    0    0    0    0    0    0    0    1    0;
0    0    0    0    1    0    0    0    0    0;
0    0    0    0    0    1    1    0    1    0]
vec_xNode = [1    3    8    2    4    5    6    7    9    9]
vec_yNode = [1    2    1    7    9    5    3    7    9    4]

# Load the packages
using LightGraphs, GraphPlot, MetaGraphs, GraphRecipes, Plots
# Create a generic graph
gr = SimpleDiGraph()
add_vertices!(gr, size(mat_arcTwoNodes, 1))
# Add edges based on the adjacency matrix
for i in 1:size(mat_arcTwoNodes, 1), j in 1:size(mat_arcTwoNodes, 2)
    if mat_arcTwoNodes[i,j] != 0
        add_edge!(gr, i, j)
    else
        nothing
    end
end
# Attach the metagraph to the original graph
mgr = MetaDiGraph(gr)
# Add the attribute of nodes
for i in 1:size(mat_arcTwoNodes,1)
    set_props!(mgr, i, Dict(
        Symbol("vec_xNode") => vec_xNode[i],
        Symbol("vec_yNode") => vec_yNode[i]
        ))
end
# Plot the graph
graphplot(gr, x=vec_xNode, y=vec_yNode)

结果如下:

您可以在绘图中添加其他属性(询问 graphplot)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多