# Software for drawing bayesian networks (graphical models)

I am searching for [free] software that can produce nice looking graphical models, e.g.

Any suggestions would be appreciated.

• You mean drawing pictures like this you linked or drawing ready models from some other software? If the latter, from which?
– user88
Oct 9, 2011 at 17:57
• Drawing pictures liked the one I linked Oct 9, 2011 at 18:30
• It was created manually in Inkscape, so you can try doing the same.
– user88
Oct 9, 2011 at 20:06
• That's awesome - the picture you provide is one I drew for Wikipedia in 2008, and I found this question when I was figuring out how to redraw it in 2012. You provide a PNG, but it was originally an SVG. Back then, I used Inkscape and it was terrible - I had to hand edit the SVG to add in the greek letters, because it couldn't handle it back then (maybe it still can't). I don't recommend it for anyone. I'm going to give the tikz approach a shot. Jul 13, 2012 at 1:05
• When I said I don't recommend it for anyone, I meant I don't recommend Inkscape for anyone trying to draw multiple consistent plate diagrams. Jul 13, 2012 at 1:11

I currently have a similar problem (drawing multiple path diagrams for my dissertation), and so I was examining many of the options listed here already to draw similar diagrams. Many of the listed resources for drawing such vector graphics (such as in microsoft office or google drawings) can produce really nice path diagrams, with fairly minimal effort. But, part of the reason I was unsatisfied with such programs is that I needed to produce many diagrams, with only fairly minor changes between each diagram (e.g. add another node, change a label). The point and click vector graphics tools aren't well suited for this, and take more effort than need be to make such minor changes. Also it becomes difficult to maintain a template between many drawings. So, I decided to examine options to produce such graphics programattically.

Graphviz, as was already mentioned by thias, came really close to having all the bells and whistles I wanted for my graphics (as well as quite simple code to produce them), but it fell short for my needs in two ways; 1) mathematical fonts are lacking (e.g. I'm not sure if you can label a node with the $\beta$ symbol in Graphviz, 2) curved lines are hard to draw (see this post on drawing path diagrams using Graphviz on @Stask's website). Because of these limitations I have currently settled (very happily) on using the Tikz/pgf drawing library in Latex. An example is below of my attempt at reproducing your graphic (the biggest pain was the labels in the lower right corners of the boxes!);

\documentclass[11pt]{report}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\tikzstyle{main}=[circle, minimum size = 10mm, thick, draw =black!80, node distance = 16mm]
\tikzstyle{connect}=[-latex, thick]
\tikzstyle{box}=[rectangle, draw=black!100]
\node[main, fill = white!100] (alpha) [label=below:$\alpha$] { };
\node[main] (theta) [right=of alpha,label=below:$\theta$] { };
\node[main] (z) [right=of theta,label=below:z] {};
\node[main] (beta) [above=of z,label=below:$\beta$] { };
\node[main, fill = black!10] (w) [right=of z,label=below:w] { };
\path (alpha) edge [connect] (theta)
(theta) edge [connect] (z)
(z) edge [connect] (w)
(beta) edge [connect] (w);
\node[rectangle, inner sep=0mm, fit= (z) (w),label=below right:N, xshift=13mm] {};
\node[rectangle, inner sep=4.4mm,draw=black!100, fit= (z) (w)] {};
\node[rectangle, inner sep=4.6mm, fit= (z) (w),label=below right:M, xshift=12.5mm] {};
\node[rectangle, inner sep=9mm, draw=black!100, fit = (theta) (z) (w)] {};
\end{tikzpicture}
\end{figure}
\end{document}
%note - compiled with pdflatex


Now, I am already writing up my dissertation in Latex, so if you just want the image without having to compile a whole Latex document it is slightly inconveniant, but there are some fairly minor workarounds to produce an image more directly (see this question over on stackoverflow). There are a host of other benifits to using Tikz for such a project though

• Extensive documentation. The pgf manual holds your hand through making some similar diagrams. And once you get your feet wet...
• A huge library of examples is there to demonstrate how to produce a huge variety of graphics.
• And finally, the Tex stack exchange site is a good place to ask any questions about Tikz. They have some wizzes over there making some pretty fancy graphics (check out their blog for some examples).

At this time I have not considered some of the libraries for drawing the diagrams in the statistical package R directly from the specified models, but in the future I may consider them to a greater extent. There are some nice examples from the qgraph library for a proof of concept of what can be accomplished in R.

• For those interested in drawing path diagrams for SEM (or for some more ideas about what can be accomplished in Microsoft Office products), I would suggest you check out Jeremy Mile's ppt file of path diagrams (at the bottom of the referenced webpage). Oct 12, 2011 at 12:31
• Great response, it's a bit of a learning curve, but like anything TeX, I think it'll pay off in the long run. Thanks! Oct 16, 2011 at 18:49

Laura Dietz has written a very nice library for tikz that enables drawing of Bayesian Networks in latex without needing to actually use tikz directly.

To demonstrate this package, see the following example for this question:

\documentclass[11pt]{report}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{figure}
\centering
\tikz{ %
\node[latent] (alpha) {$\alpha$} ; %
\node[latent, right=of alpha] (theta) {$\theta$} ; %
\node[latent, right=of theta] (z) {z} ; %
\node[latent, above=of z] (beta) {$\beta$} ; %
\node[obs, right=of z] (w) {w} ; %
\plate[inner sep=0.25cm, xshift=-0.12cm, yshift=0.12cm] {plate1} {(z) (w)} {N}; %
\plate[inner sep=0.25cm, xshift=-0.12cm, yshift=0.12cm] {plate2} {(theta) (plate1)} {M}; %
\edge {alpha} {theta} ; %
\edge {theta} {z} ; %
\edge {z,beta} {w} ; %
}
\end{figure}
\end{document}
%note - compiled with pdflatex


While not exactly the same, it certainly conveys the same information and could be tweaked to better fit specific requirements. This package generates very acceptable figures without needing to learn the full tikz package.

• This library is awesome! Greatly simplifies the creation of bayesian network diagrams! Thanks for sharing. Nov 20, 2016 at 12:27
• I tried this software and aft-pgm, this one is much more easier to use than the other one. Thanks the author for the excellient software. Sep 19, 2019 at 7:50

You can't beat http://daft-pgm.org/

Daft is a Python package that uses matplotlib to render pixel-perfect probabilistic graphical models for publication in a journal or on the internet. With a short Python script and an intuitive model-building syntax you can design directed (Bayesian Networks, directed acyclic graphs) and undirected (Markov random fields) models and save them in any formats that matplotlib supports (including PDF, PNG, EPS and SVG).

• daft is awesome! this should be the accepted answer imho Jun 12, 2017 at 17:33
• Daft has a few drawbacks: It doesn't autoscale the nodes to fit the text, doesn't calculate the layout of the network, etc. That was enought to turn me off. Jul 10, 2017 at 18:23

You could try GraphViz.

This allows you to specify the graph in a text-file and it will be drawn automatically (avoiding overlapping arrows and so on). Go here (pdf) for a minimal example and a manual.

Inkscape is essentially a free version of Adobe Illustrator, and is a very strong program for doing vector graphics, like the picture you posted. It also plays rather nicely with most statistical packages for doing final edits/annotations/etc. to graphs - R, SAS, etc. can output a graph as a PDF or other vector format (like .eps), and then you can bring it in to Inkscape to mess about with colors, symbols, axis labels etc.

• I prefer the tools that treat arrows as special objects: when I move the variables around, I hope the arrows will follow. It seems Inkscape cannot do this. Jun 18, 2014 at 12:19

If you have a particular interest in using LaTeX, the LaTeXDraw program has some nice functionality for creating flow charts with embedded latex code.

It imports / exports PSTricks code and SVG, and can also export svg, pdf, eps, jpg, png, etc. It runs in Linux, Mac OS X, and Windows.

Dia is a free open source program for drawing diagrams. I find it useful and it's not too difficult to get started.

I have found Diagrammix to be a very flexible package, available for Mac OS X. It is a well rounded vector graphics package and does a good job at graphical models. It is fairly inexpensive and has some good add-ons that have helped improve the shapes and directions of edges.

• A suggestion: you could show how to reproduce the graphical model in the question with Diagrammix like some answers did. Nov 2, 2016 at 16:32

You could try Google Docs Draw. It looks like it will do what you want for free, right in your browser.

• I have tried it, but (IMHO) it lacks Inkscape's flexibility. Oct 10, 2011 at 0:22

You can go for PlantUML. It is open source and quite flexible.

You can also use the Lucidchart webapp.

I've used it in the past for drawing graphs and it's free.

SCAVIS has a Bayesian network. Try to google "scavis baysian network". The same program can draw different diagrams using Python (or Java) syntax.

you can use draw.io and use one or their many templates to create these icons. It helps you create SVGs or any other format. and does not require you to install anything on your system.