Message Boards Message Boards


VertexShapeFunction pattern matching

Posted 2 months ago
2 Replies
5 Total Likes

I am trying to visualize a heterogeneous graph which nodes are categorical. Here is a dummy graph for implementation:

Input is

graph = UndirectedEdge @@@ {{"u0", "c1"}, {"u1", "c3"}, {"u2", "c0"}, {"u3", "c2"}, {"u4", "c1"}, {"u0", "i0"}, {"u1", "i0"}, {"u2", "i0"}, {"u3", "i1"}, {"u4", "i2"}}

And it looks like this by specifying vertex coordinates. enter image description here

I would like to customize vertex shape by using VertexShapeFunction, but I cannot make the pattern matching work, for instance, I would like to make the nodes starts with u have shape square:

Graph[graph, VertexLabels -> "Name", VertexShapeFunction -> {_?StringStartsQ["u"] -> "Square" }]

I am aware the it should be a pattern, as

CompleteGraph[5, VertexShapeFunction -> {_?EvenQ -> "Star"}, VertexSize -> 0.2]

but I don't know how to make pattern work, I tried:

Cases[{"u1", "p1"}, _?StringStartsQ[#, "u"] &]

I know StringCases and Select can easily returns the strings I want, but unfortunately, seems only Cases with pattern matching would work for VertexShapeFunction

StringCases[{"u1", "p1"}, "u" ~~ _]
Select[{"u1", "p1"}, StringStartsQ[#, "u"] &]
2 Replies

Have you tried with parentheses:

Cases[{"u1", "p1"}, _?(StringStartsQ[#, "u"] &)]
Cases[{"u1", "p1"}, _?(StringStartsQ["u"])]

Yup, exact way to do it!

enter image description here

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract