# Graph / subgraph attributes

**URL:** https://forum.graphviz.org/t/graph-subgraph-attributes/670
**Category:** Help
**Created:** [May 22, 2021, 4:46pm UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670 "2021-05-22T16:46:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nicolas](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/nicolas/32/381_2.png) [@Nicolas](https://forum.graphviz.org/u/Nicolas)
#### Post date: [May 22, 2021, 4:46pm UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670/1 "2021-05-22T16:46:37Z")

</div>

I’m playing a bit with the Graphviz grammar, and there are a few things that puzzle me.

First: subgraph attributes. The following is valid:

```auto
graph {
  subgraph {}[color=red]
}

```

But evaluating it (or variations with nested nodes and edges) through the `canon` output format will simply remove `[color=red]`. My assumption is that the syntax is legal (because it makes the parser easier) but this kind of attributes on a subgraph are ignored. Is that correct?

My second question is about graph attributes.  
The following graph:

```auto
graph {
  node[color=red]
  a
}

```

Will have, more or less, a canonical output of:

```auto
graph {
  a[color=red]
}

```

But the following graph:

```auto
graph {
  graph[bgcolor=red]
}

```

Does not change when “canonized”.

Is there a particular reason for this apparently inconsistent behaviour? I think there might be something subtle I’m missing, the distinction between graphs and subgraphs, and the way they are handled, always seems to elude me.

---

<div class="post-metadata">

### Author: ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)
#### Post date: [May 22, 2021, 5:32pm UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670/2 "2021-05-22T17:32:40Z")

</div>

[I am not a Graphviz developer, just a groupie]

- Graphviz allows one to write many legal-but-head-scratching statements
- Note that **color=red** only applies to nodes, clusters, and edges.
- Graphviz is more _casual_ about punctuation than most languages. Nothing too magic about newlines
- I think what you effectively wrote was:

```auto
graph {
  subgraph {} // subgraph commands are terminated by }
  [color=red] // does not apply to graphs, so applies to nothing at all, a legal noop
}

```

As to your _graphs-vs-nodes_ question, the two results are different but equivalent.  
Finally, graphs, subgraphs, and clusters are (sometimes confusing) variations on a theme. Until someone produces a giant table describing all the similarities and differences, I suggest attacking the question piecemeal. Pose questions about specific features/problems.

---

<div class="post-metadata">

### Author: ![Nicolas](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/nicolas/32/381_2.png) [@Nicolas](https://forum.graphviz.org/u/Nicolas)
#### Post date: [May 22, 2021, 8:05pm UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670/3 "2021-05-22T20:05:10Z")

</div>

Thanks for taking the time to answer!

I have concrete reasons to believe that my `[color=red]` is considered to apply to the subgraph statement.

First, the following fails to be processed:

```auto
graph {
  [color=red]
}

```

Which seems to imply that you can’t really have a list of attributes that apply to nothing.

Second, if I understand the [parser’s code](https://gitlab.com/graphviz/graphviz/-/blob/main/lib/cgraph/grammar.y#L154) properly, it explicitly supports `graph[attributes]`. This is born out by the [language specifications](https://graphviz.org/doc/info/lang.html), although these do not appear to be up to date.

This second reason is the one that bothers me: this doesn’t appear to be an accident but something that’s explicitly supported by the language, and I don’t understand what it does!

Oh, and it seems I was completely mistaken about the second part of my question - I don’t know why I thought the `node[color=red]` statements were merged into every node in the canonical form, but I just checked again and they do not. So, no inconsistency there, sorry for the misreport!

---

<div class="post-metadata">

### Author: ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)
#### Post date: [May 23, 2021, 12:14am UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670/4 "2021-05-23T00:14:36Z")

</div>

Speaking from the perspective of the person that wrote it, I’d say the best thing is to read cgraph/grammar.y and possibly suggest improvements (that don’t break anything that already works). No one poked around in these corners very much.

---

<div class="post-metadata">

### Author: ![Nicolas](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/nicolas/32/381_2.png) [@Nicolas](https://forum.graphviz.org/u/Nicolas)
#### Post date: [May 23, 2021, 11:56am UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670/5 "2021-05-23T11:56:16Z")

</div>

I’d be happy to, if this was in a language that I could program in. Unfortunately…

As the author of this code, could you explain how graphviz uses attributes set on a `graph` element? I specifically mean in that part of the grammar:

```auto
attr_stmt : ( `graph` | `node` | `edge` ) attr_list

```

I understand `node` and `edge`, but I’m not sure what `graph` does.

I suppose I should have phrased my question that way from the beginning…

---

<div class="post-metadata">

### Author: ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)
#### Post date: [May 23, 2021, 3:39pm UTC](https://forum.graphviz.org/t/graph-subgraph-attributes/670/6 "2021-05-23T15:39:18Z")

</div>

> [@Nicolas](#):
>
> could you explain how graphviz uses attributes set on a `graph` element?

It sets graph specific attributes on a graph, e.g. `bgcolor` as in [this example](http://magjac.com/graphviz-visual-editor/?dot=graph%20%7B%0A%20%20%20%20graph%20%5Bbgcolor%3Dgreen%5D%0A%20%20%20%20a%20--b%0A%7D%0A).

You can also omit the `graph` keyword and the brackets and specify the same attribute directly as in [this example](http://magjac.com/graphviz-visual-editor/?dot=graph%20%7B%0A%20%20%20%20bgcolor%3Dred%0A%20%20%20%20a%20--b%0A%7D%0A).

Graph specific attributes are indicated with a `G` in the `Used By` column in the table in the [Node, Edge and Graph Attributes](https://graphviz.org/doc/info/attrs.html) documentation.
