Message Boards Message Boards

1
|
3495 Views
|
2 Replies
|
5 Total Likes
View groups...
Share
Share this post:

VertexShapeFunction pattern matching

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"] &]
POSTED BY: Wenzhen Zhu
2 Replies

Have you tried with parentheses:

Cases[{"u1", "p1"}, _?(StringStartsQ[#, "u"] &)]
Cases[{"u1", "p1"}, _?(StringStartsQ["u"])]
POSTED BY: Gianluca Gorni

Yup, exact way to do it!

enter image description here

POSTED BY: Wenzhen Zhu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract