Guides

Diagrams as code: Mermaid vs Graphviz

Diagrams written as text live next to the code they describe: they diff in a pull request, nobody needs a drawing tool, and they never drift into a stale screenshot. Mermaid and Graphviz are the two most common languages for them. They overlap less than it seems: Mermaid is built for diagrams people write by hand in documentation, Graphviz for graphs of any size that are laid out automatically, often by a program.

By Radhika Singh · Last updated

Side by side

The short version, before the detail:

MermaidGraphviz (DOT)
First released20141991, at AT&T Labs
SyntaxMarkdown-like, one line per connection: A --> BGraph language: a -> b; with attributes in [brackets]
Diagram typesFlowcharts, sequence, class, state, entity-relationship, Gantt, pie and moreGraphs: nodes and edges, any structure
LayoutAutomatic, few controlsSeveral layout engines and fine control over ranks, spacing and shapes
Renders inGitHub, GitLab, Notion and Obsidian Markdown, many docs sitesAnything that runs the Graphviz program or its WebAssembly build
Best forHand-written diagrams in docs and READMEsGenerated or large graphs: dependencies, call graphs, state machines

Mermaid: diagrams that render where you write

Mermaid's main advantage is distribution. A fenced code block marked mermaid renders as a diagram in GitHub (since February 2022), GitLab, Notion and Obsidian, so a flowchart in a README stays correct as long as someone updates two lines of text. The syntax reads almost like prose: A[Visitor] --> B{Has an account?} draws a box, a diamond and an arrow.

It also covers diagram types Graphviz does not try to: sequence diagrams for API calls, Gantt charts, entity-relationship diagrams for a database schema, class and state diagrams. The trade-off is layout control: Mermaid arranges nodes for you, and big diagrams can tangle. Draft and preview one in the Mermaid Editor, which starts from a template for each of these types, exports SVG or PNG and copies the diagram as a Markdown code block.

Graphviz: layout engines for real graphs

Graphviz describes a graph and lets a layout engine place it. dot draws hierarchies with edges flowing in one direction, which suits dependency trees, pipelines and org charts; neato and fdp use spring models for networks without a natural direction; circo draws circular layouts and twopi radial ones. Attributes on nodes, edges and the whole graph control shapes, colors, ranks and spacing in detail.

Because DOT is simple to generate, many tools emit it: terraform graph writes infrastructure dependencies as DOT, and build systems and static analyzers produce call graphs and module graphs the same way. Paste that output into Graphviz Online to see it laid out, switch engines to find the clearest view, and export SVG or PNG.

Which one to pick

A few questions settle it:

  • Will it live in a README, wiki or Notion page? Mermaid, because it renders there with no build step.
  • Is it a sequence of calls, a Gantt chart or a database schema? Mermaid has dedicated syntax for each.
  • Is the graph generated by a program, or larger than a few dozen nodes? Graphviz, which lays out big graphs far better.
  • Do you need precise control over where things go? Graphviz.

Many teams use both: Mermaid for the diagrams people draw, Graphviz for the ones their tools produce.

Rendering diagrams safely

Both tools output SVG, and SVG can carry scripts. When a page renders diagrams from text that other people wrote, render it in a sandbox or display the SVG as an image, not inline markup. This site does both: Mermaid runs in its strict security mode, and every diagram is shown through an image element, where scripts inside the SVG cannot run.

The tools in this guide

Found a mistake? Write in from the About page and it gets fixed.

Pick a tool.
Get it done.

125 utilities today, more every month. Bookmark the one you reach for.