在 Mathematica 中创建具有不同颜色边的图形
我想创建一个图(图论),其中某些边具有与其他边不同的颜色,这将用于突出显示图中从一个顶点到另一个顶点的路径。
以下是一些具有不同颜色边缘的示例 http://demonstrations.wolfram.com/AGraphTheoryInterpretationOfTheSumOfTheFirstNIntegers/和http://demonstrations.wolfram.com/Ramsey336/。我查看了这些的源代码,但这些解决方案似乎很复杂。我需要一个简单的例子来工作。我认为我需要使用 EdgeRenderingFunction 作为 GraphPlot 的选项之一。
另外,在 EdgeRenderingFunction 文档中的“更多 信息”部分它说:
这看起来很有用,但不幸的是没有可以尝试的编码示例。
从字面上看,我尝试过类似的事情
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的示例说明了如何自动突出显示图形中的特定路径。
这是一个愚蠢的图表,由边缘规则列表指定:
这是我们想要的穿过图表的路径强调。
让我们将路径划分为边缘,考虑到我们想要突出显示边缘而与其方向无关。
我们编写了一个 EdgeRenderingFunction,它以两种样式之一渲染边缘,无论它是否在我们的列表中。
最后,我们显示结果。
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:
Here's a path through the graph we'd like to highlight.
Let's partition the path into edges, accounting for the fact that we want to highlight the edge independent of its orientation.
We write an
EdgeRenderingFunction
that renders an edge in one of two styles, depending no whether it's in our list or not.Finally, we display the result.
渲染函数是一个回调函数,它有 3 个参数。第一个是线的坐标列表,第二个是边的顶点,第三个是边的标签。
在 Mathematica 中,您可以使用
(f[#1,#2,#3,...] &)
创建匿名函数。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,...] &)
.