Groups of nodes with the same attributes in GraphViz file
Asked Answered
A

2

15

In the dot language of GraphViz I want to describe a 2-mode network. So I have nodes of two different types. One group for example could contains people how read and the other group could contain the books being read by the people.

I want to give the nodes in these two groups different looks (shape, color, etc). How can I specify the attributes for a group of nodes in one statement. The aim is to be able to change the look for each group of nodes in one place, not in all individual node descriptions.

This could be done with something like attribute inheritance, but I don't know whether the dot language has this concept.

Aeschylus answered 4/3, 2015 at 11:58 Comment(0)
R
24

In principal there are three possibilities

  1. set default attributes before creating a node
    • globally - valid for all following node creations
    • locally in a subgraph - valid for node creations within subgraph only
  2. create nodes with explicit attributes
  3. assign attributes to a group of nodes after creation.

Options 1 and 2 allow only one group per node as creation is a single event. Option 3 allows different grouping for each assignment.


set default attributes globally before creating a node

digraph {
  x // node with current defaults

  // set default
  node [shape=box color=red]
  // create with default values
  a1, a2

  // set default
  node [shape=circle color=blue]
  // create with default values
  b1, b2

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here


set default attributes locally before creating a node

digraph {
  x // node with current defaults

  {
      // set default
      node [shape=box color=red]
      // create with default values
      a1, a2
  }

  {
      // set default
      node [shape=circle color=blue]
      // create with default values
      b1, b2
  }

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here


create nodes with explicit attributes

digraph {
  x // node with current defaults

  // create with explicit attributes
  a1, a2 [shape=box color=red]

  // create with explicit attributes
  b1, b2 [shape=circle color=blue]

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here


assign attributes to a group of nodes after creation

digraph {
  x // node with current defaults

  // create with default values
  a1, a2, b1, b2

  // assign shape
  a1, a2 [shape=box]
  b1, b2 [shape=circle]

  // assign color
  a1, b2 [color=red]
  b1, a2 [color=blue]

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here

Rivkarivkah answered 12/3, 2015 at 11:28 Comment(2)
Wow, this is very comprehensive. Thanks!Aeschylus
@Rivkarivkah Why can't I find the corresponding dot language grammar for a1, a2 [atrrbuites...] in graphviz.org/doc/info/lang.html ?Distillation
B
6

This can be done for all nodes in a graph with the node keyword, or for all edges in graph with the edge keyword. This can also be done on a node-by-node or edge-by-edge basis.

Example for a whole graph or subgraph:

digraph
{
  subgraph readers
  {
      node[shape=box; color=red;]
      r1; r2; r3;
  }

  subgraph books
  {
      node[shape=circle; color=blue;]
      b1; b2; b3;
  }
  r1->{b1 b2}
  r2->{b2 b3}
  r3->{b1 b2 b3}
}

This would give you the graph:

enter image description here

Example of per node attributes:

digraph
{
    n1[shape=triangle];
    n2[shape=star];
    n3[shape=square];

    n1->n2->n3
}

Would give the graph:

enter image description here

Backdate answered 4/3, 2015 at 20:1 Comment(2)
Thanks a lot. The subgraph concept was the one I was after.Aeschylus
Thanks, this helped me too. What caught me out initially was that I added the subgraph specifying the properties after the nodes were created. The nodes need to be created in the subgraph to receive those properties.Templas

© 2022 - 2024 — McMap. All rights reserved.