Plotting Models#

pgmpy provides built-in plotting helpers for visualizing graph structures. These work on all DAG-based models (since model classes inherit from DAG) and support multiple backends for different use cases — quick inspection, publication figures, or file output.

At a Glance#

API#

Plotting helpers are called directly on the graph or model object:

from pgmpy.example_models import load_model

model = load_model("bnlearn/sachs")

graphviz_graph = model.to_graphviz()
graphviz_graph.draw("sachs.png", prog="dot")

daft_graph = model.to_daft()
daft_graph.render()

Graphviz#

to_graphviz() converts the model to a PyGraphviz object with support for multiple layout algorithms (dot, neato, circo, etc.) and output formats. Best for generating high-quality file output for documentation and reports. Also works on PDAG objects for visualizing partially directed graphs from discovery workflows.

Publication Figures#

to_daft() produces clean, publication-ready probabilistic graphical model diagrams suitable for academic papers:

from pgmpy.example_models import load_model

model = load_model("bnlearn/sachs")
daft_graph = model.to_daft(node_pos="circular")
daft_graph.render()

NetworkX Drawing#

Since pgmpy models are built on networkx, you can use networkx.draw() and related functions directly for lightweight interactive plotting or integration with existing networkx visualization pipelines:

import matplotlib.pyplot as plt
import networkx as nx
from pgmpy.example_models import load_model

model = load_model("bnlearn/sachs")
pos = nx.spring_layout(model)
nx.draw(model, pos, with_labels=True, node_size=800, font_size=8)
plt.show()

See Also#

See also

API Reference#

For the full list of graph and model classes with plotting support: