20

Mathematica 是否可以画出这样的东西(由 Graphviz 创建):

在此处输入图像描述

这是我能得到的最好的(但形状和风格并不令人满意):

在此处输入图像描述

代码:

GraphPlot[{{A -> C, "go"}, {C -> B, "gone"}, {C -> D, 
   "went"}, {C -> C, "loop"}}, VertexLabeling -> True, 
 DirectedEdges -> True]
4

2 回答 2

21

您可以使用VertexRenderingFunction.

GraphPlot[{{A -> C, "go"}, {C -> B, "gone"}, {C -> D, "went"}, {C -> C, "loop"}}, 
 DirectedEdges -> True, 
 VertexRenderingFunction -> ({{White, Disk[#, 0.15]}, 
     AbsoluteThickness[2], Circle[#, 0.15], 
     If[MatchQ[#2, A | B], Circle[#, 0.12], {}], Text[#2, #]} &)]

在此处输入图像描述


方法 2015 年 2 月更新

为了保留使用绘图工具(双击)交互式重新排列图形的能力,必须将顶点图形保留在 内GraphicsComplex,使用索引而不是坐标。我相信可以通过VertexRenderingFunction使用递增变量来做到这一点,但使用后处理似乎更容易,也可能更健壮。这适用于Mathematica的 7 和 10 版本,大概也适用于 8 和 9:

GraphPlot[
  {{A -> C, "go"}, {C -> B, "gone"}, {C -> D, "went"}, {C -> C, "loop"}},
  DirectedEdges -> True
] /.
 Tooltip[Point[n_Integer], label_] :>
   {{White, Disk[n, 0.15]},
    Black, AbsoluteThickness[2], Circle[n, 0.15], 
    If[MatchQ[label, A | B], Circle[n, 0.12], {}], Text[label, n]}

在此处输入图像描述

于 2011-11-13T03:46:02.210 回答
5

正如 mr.Wizard 在他的回答中所建议的那样,无需交互式放置即可将您的顶点放在所需的位置。您可以VertexCoordinateRules为此使用:

GraphPlot[{{A -> C, "go"}, {C -> B, "gone"}, {C -> D, "went"}, {C -> C, "loop"}}, 
    DirectedEdges -> True, 
    VertexRenderingFunction -> 
          ({{White, Disk[#, 0.15]}, AbsoluteThickness[2], Circle[#, 0.15], 
           If[MatchQ[#2, A | B], Circle[#, 0.12], {}], Text[#2, #]} &),
    VertexCoordinateRules -> 
          {A -> {0, 0}, C -> {0.75, 0},B -> {1.5, 0.25}, D -> {1.5, -0.25}}
]

在此处输入图像描述

于 2011-11-13T19:48:02.807 回答