在 Mathematica 中创建具有不同颜色边的图形

发布于 2024-09-26 22:51:24 字数 1054 浏览 0 评论 0原文

我想创建一个图(图论),其中某些边具有与其他边不同的颜色,这将用于突出显示图中从一个顶点到另一个顶点的路径。

以下是一些具有不同颜色边缘的示例 http://demonstrations.wolfram.com/AGraphTheoryInterpretationOfTheSumOfTheFirstNIntegers/http://demonstrations.wolfram.com/Ramsey336/。我查看了这些的源代码,但这些解决方案似乎很复杂。我需要一个简单的例子来工作。我认为我需要使用 EdgeRenderingFunction 作为 GraphPlot 的选项之一。

另外,在 EdgeRenderingFunction 文档中的“更多 信息”部分它说:

Mathematicagraphics

这看起来很有用,但不幸的是没有可以尝试的编码示例。

从字面上看,我尝试过类似的事情

GraphPlot[{1 ->; 2, 2-> 3, 3-> 4, 4 -> 1, 2-> 4, 4-> 5、4-> 6}、顶点标注->确实如此,
EdgeRenderingFunction -> 边缘渲染函数g[{1, 2}, {1, 2},红色]]

但这是行不通的。这需要比这更聪明的东西。

I want to create a graph (Graph Theory) where certain edges have a different colour to other edges, which would be used to highlight a path in the graph from one vertex to another.

Here are some examples which have different coloured edges http://demonstrations.wolfram.com/AGraphTheoryInterpretationOfTheSumOfTheFirstNIntegers/ and http://demonstrations.wolfram.com/Ramsey336/. I looked at source code for these but those solutions seem complicated. I need a simple example to work from. I reckon I need to use the EdgeRenderingFunction as one of the options for GraphPlot.

Additionally under EdgeRenderingFunction documentation in "More
Information" part it says:

Mathematica graphics

This looks useful but unfortunately there is no coded examples to try.

Taking that very literally I tried things like

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 4
-> 1, 2 -> 4, 4 -> 5, 4 -> 6}, VertexLabeling -> True,
EdgeRenderingFunction -> g[{1, 2}, {1,
2}, Red]]

But that wouldn't work. It will take something more clever than that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-10-03 22:51:24

下面的示例说明了如何自动突出显示图形中的特定路径。

这是一个愚蠢的图表,由边缘规则列表指定:

edges = Table[i -> Mod[1 + i^2, 10], {i, 0, 9}];
GraphPlot[edges, VertexLabeling -> True]

Mathematicagraphics

这是我们想要的穿过图表的路径强调。

path = {0, 1, 2, 5, 6, 7, 0};

让我们将路径划分为边缘,考虑到我们想要突出显示边缘而与其方向无关。

edgesToHighlight = Partition[path, 2, 1];
edgesToHighlight = Join[edgesToHighlight,
    Reverse /@ edgesToHighlight];

我们编写了一个 EdgeRenderingFunction,它以两种样式之一渲染边缘,无论它是否在我们的列表中。

erf[pts_, edge_, ___] := If[MemberQ[edgesToHighlight, edge],
    {Thick, Black, Arrow[pts, 0.1]}, {Darker[Red], Line[pts]}];

最后,我们显示结果。

GraphPlot[edges, EdgeRenderingFunction -> erf,
    VertexLabeling -> True]

Mathematica 图形

Here's an example that illustrates how to automate the highlighting of a particular path through a graph.

Here's a silly graph, specified by a list of edge rules:

edges = Table[i -> Mod[1 + i^2, 10], {i, 0, 9}];
GraphPlot[edges, VertexLabeling -> True]

Mathematica graphics

Here's a path through the graph we'd like to highlight.

path = {0, 1, 2, 5, 6, 7, 0};

Let's partition the path into edges, accounting for the fact that we want to highlight the edge independent of its orientation.

edgesToHighlight = Partition[path, 2, 1];
edgesToHighlight = Join[edgesToHighlight,
    Reverse /@ edgesToHighlight];

We write an EdgeRenderingFunction that renders an edge in one of two styles, depending no whether it's in our list or not.

erf[pts_, edge_, ___] := If[MemberQ[edgesToHighlight, edge],
    {Thick, Black, Arrow[pts, 0.1]}, {Darker[Red], Line[pts]}];

Finally, we display the result.

GraphPlot[edges, EdgeRenderingFunction -> erf,
    VertexLabeling -> True]

Mathematica graphics

千纸鹤 2024-10-03 22:51:24
GraphPlot[
 {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 2 -> 4, 4 -> 5, 4 -> 6}, 
 VertexLabeling -> True, 
 EdgeRenderingFunction -> (
    {If[#2 == {1, 2}, Red, Black], 
     Line[#1]}
  &)
]

Mathematicagraphics

渲染函数是一个回调函数,它有 3 个参数。第一个是线的坐标列表,第二个是边的顶点,第三个是边的标签。

在 Mathematica 中,您可以使用 (f[#1,#2,#3,...] &) 创建匿名函数。

GraphPlot[
 {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 2 -> 4, 4 -> 5, 4 -> 6}, 
 VertexLabeling -> True, 
 EdgeRenderingFunction -> (
    {If[#2 == {1, 2}, Red, Black], 
     Line[#1]}
  &)
]

Mathematica graphics

The rendering function is a callback function, which takes 3 arguments. The 1st is the list of coordinates of the line, the 2nd is the vertices of the edge, and the 3rd is the edge's label.

In Mathematica you could create an anonymous function with (f[#1,#2,#3,...] &).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文