Render a document as a graph (PNG, SVG, PDF)

prov.dot turns a document into a pydot graph, which Graphviz can write in any format it supports. It needs the dot extra and a local Graphviz install.

python -m pip install "prov[dot]"

Important

The dot extra installs the pydot and networkx packages. Rendering also needs the Graphviz dot executable, which you install separately:

Without it every write_*() call below fails, usually with an error about the missing dot executable. Check with dot -V that Graphviz is on your PATH before debugging your own code.

Convert a document to a pydot.Dot

import prov.model as pm
from prov.dot import prov_to_dot

document = pm.ProvDocument()
document.set_default_namespace("http://example.org/")
e = document.entity("e1")
a = document.activity("a1")
document.wasGeneratedBy(e, a)

dot = prov_to_dot(document)

prov_to_dot() unifies the document before it draws, so records that share an identifier become one node.

Write PNG, SVG or PDF

pydot.Dot has a write_<format> method for each Graphviz output format:

dot.write_png("document.png")
dot.write_svg("document.svg")
dot.write_pdf("document.pdf")

Layout direction

direction sets the rank direction. The default is "BT", bottom to top. The other values are "TB", "LR" and "RL":

dot_lr = prov_to_dot(document, direction="LR")
dot_lr.write_svg("document-lr.svg")

Hide attribute annotations

By default every element and relation gets a note node listing its non-formal attributes. Turn either off to declutter a dense graph:

dot_plain = prov_to_dot(
    document,
    show_element_attributes=False,
    show_relation_attributes=False,
)

Use labels instead of identifiers

Pass use_labels=True to show each element’s prov:label as the node text. An element without a label falls back to its identifier:

document.entity("e1", {pm.PROV_LABEL: "Crime report"})
dot_labelled = prov_to_dot(document, use_labels=True)

Hide n-ary relation elements

A relation with more than two formal attributes, such as a wasDerivedFrom that also records an activity, usage and generation, draws every element by default. Set show_nary=False to draw only the first two:

dot_binary = prov_to_dot(document, show_nary=False)

Plot in a notebook or window

plot() draws the same graph to the file its extension names, or, with no filename, shows it through matplotlib, which needs the plot extra.

document.plot("document.png", use_labels=True)

prov.dot.prov_to_dot() documents every parameter.

Edge cases

Draw one bundle

prov_to_dot() and plot() take a document. A bundle that is not a document draws with a DeprecationWarning, and 4.0 draws documents only. To draw one bundle, copy it into a document of its own. The new document declares the source’s namespaces and default, and the bundle copy declares the bundle’s own default, so every name resolves as it does in the source. The new document draws the bundle as a cluster, as its source document does, where a bundle passed alone draws its records at the top level:

bundle = document.bundle("b")
bundle.entity("e2")

alone = pm.ProvDocument(namespaces=document.get_registered_namespaces())
default = document.get_default_namespace()
if default is not None:
    alone.set_default_namespace(default.uri)
copy = alone.bundle(bundle.identifier)
own = bundle.get_default_namespace()
if own is not None:
    copy.set_default_namespace(own.uri)
copy.update(bundle)

dot_bundle = prov_to_dot(alone)