EdgeRenderingFunction and VertexRenderingFunction Help

I am trying to build a LayeredGraphPlot where some nodes connect to other nodes, such as a tree. I would like to have the freedom to choose the thickness, color, presence or absence of marking of each edge.

I would also like to have the freedom to display some vertex names and display some vertices as “dots”. I cannot understand how EdgeRenderingFunction and VertexRenderingFunction allow me to do this.

I am new to math. I clicked on the additional information on Wolfram's help page, but it seems to have a vague description and not exact syntax, followed by some nice but useless (for me) examples (compared to matlab in mathworks, where the help provides the exact syntax .. at least in my opinion).
I was looking for about 10 books on mathematics (Smith and Blachman, mathematica demystified .. etc. Etc. etc.), but they all seem to partially cover the function with one or two examples and do not provide the most general syntax.

Can someone help with this and I would also like some tips on how to study math? I am a smart guy and I should not have so many problems learning how to use teams.

LayeredGraphPlot [{1-> 2.1> 3.2> 4.3> 5.3-> 6}]

So, for example, I would like to:

  • Deny all vertex names except vertices 4,5 and 6.
  • Color as blue and thick edges from 3-> 6, 2-> 4 and 1-> 3
  • All other edges should be red and thin.
+6
source share
3 answers

VertexRenderingFunction and EdgeRenderingFunction allow you to exercise explicit control over how vertices and edges are drawn on the chart. Both functions are expected to return a symbolic graphic directive (or a list of such directives) that is valid for the Graphics command.

Let's start with the VertexRenderingFunction . We will define a new function called drawVertex . A VertexRenderingFunction is called with two arguments: the position of the vertex (as the X / Y coordinate pair) and the name of the vertex.

 drawVertex[position_, vertex:(4 | 5 | 6)] := Inset[Framed[vertex, Background -> LightMagenta], position] drawVertex[position_, _] := {PointSize[Medium], Blue, Point[position]} 

There are two definitions for drawVertex . The first applies only to vertices 4 or 5 or 6 . He draws these peaks as framed marks. The second definition applies to all other vertices and draws simple blue dots.

Now for an EdgeRenderingFunction named drawEdge . The function will be passed three arguments: the end points of the edge, a list of the source and target vertex of the edge, and the label for the edge (if any). In our case, all the edges will be drawn as arrows, but the color and thickness will vary depending on the edge. To introduce these differences, the edgeStyle helper function is edgeStyle :

 drawEdge[ends_, fromTo_, label_] := Join[edgeStyle[fromTo], {Arrowheads[{{Automatic, 0.5}}], Arrow[ends]}] edgeStyle[{3, 6} | {2, 4} | {1, 3}] := {Blue, Thick} edgeStyle[_] := {Red, Thin} 

Using these definitions, we can now use them in combination with LayeredGraphPlot to create a custom chart:

 LayeredGraphPlot[{1 -> 2, 1 -> 3, 2 -> 4, 3 -> 5, 3 -> 6}, VertexRenderingFunction -> drawVertex, EdgeRenderingFunction -> drawEdge ] 

layered graph

+7
source
 LayeredGraphPlot[ {1 -> 2, 1 -> 3, 2 -> 4, 3 -> 5, 3 -> 6}, VertexRenderingFunction -> (If[Intersection[{4, 5, 6}, {#2}] != {}, {White, EdgeForm[Black], Disk[#, .1], Black, Text[#2, #1]}, {PointSize[Large], Point[#1]}] &), EdgeRenderingFunction -> (If[Intersection[{{3, 6}, {2, 4}, {1, 3}}, {#2}] != {}, {Blue, Thick, Line[#1]}, {Red, Line[#1]}] &) ] 

enter image description here

Mathematica syntax is more complicated at first glance. Almost every powerful tool. I think the short snippets presented here in StackOverflow, as answers to new Mma users, are a good starting point for the first steps. Please note that there are also advanced and more complex questions.

Mathematica is not intended for the casual user. If you do not plan to invest some time in training and getting used to it, I suggest trying another tool.

Finally, the LayeredGraphPlot[] function is not considered in depth in the help system, simply because it is the cousin of GraphPlot[] , whose man page is the root of all search queries ...... [] ./ p>

+3
source

I imitated your code and was able to create something very close to what I really wanted. Many thanks for your help!

enter image description here

  {drawVertex[position_, vertex : (1111 | 1112 | 1121 | 1122 | 1211 | 1212 | 1221 | 1222)] := Inset[Framed[ Text[If[vertex == 1111, "80,70", If[vertex == 1112, "50,60", If[vertex == 1121, "105,50", If[vertex == 1122, "70,55", If[vertex == 1211, "70,40", If[vertex == 1212, "90,50", If[vertex == 1221, "85,60", "40,50"]]]]]]]], Background -> White], position] drawVertex[position_, _] := {PointSize[Medium], Blue, Point[position]} drawEdge[ends_, fromTo_, label_] := {Join[ edgeStyle[fromTo], {Arrowheads[{{Automatic, 1}}], Arrow[ends]}], Text[label, Mean[ends]]} edgeStyle[{1, 11} | {11, 111} | {111, 1111}] := {Blue, Thick} edgeStyle[_] := {Red, Thin} LayeredGraphPlot[{{1 -> 11, "\!\(\*SubscriptBox[\"C\", RowBox[{\"1\", \"L\"}]]\)"}, {1 -> 12, "\!\(\*SubscriptBox[\"C\", RowBox[{\"1\", \"R\"}]]\)"}, {11 -> 111, "\!\(\*SubscriptBox[\"H\", \"L\"]\)"}, {11 -> 112, "\!\(\*SubscriptBox[\"H\", \"H\"]\)"}, {12 -> 121, "\!\(\*SubscriptBox[\"H\", \"L\"]\)"}, {12 -> 122, "\!\(\*SubscriptBox[\"H\", \"H\"]\)"}, {111 -> 1111, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"L\"}]]\)"}, {111 -> 1112, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"H\"}]]\)"}, {112 -> 1121, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"L\"}]]\)"}, {112 -> 1122, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"H\"}]]\)"}, {121 -> 1211, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"L\"}]]\)"}, {121 -> 1212, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"H\"}]]\)"}, {122 -> 1221, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"L\"}]]\)"}, {122 -> 1222, "\!\(\*SubscriptBox[\"C\", RowBox[{\"0\", \"H\"}]]\)"}}, EdgeRenderingFunction -> drawEdge, VertexRenderingFunction -> drawVertex]} 
0
source

Source: https://habr.com/ru/post/898228/


All Articles