Drawing (Plotting) Bayesian Networks in pyBN

The importance of drawing a Bayesian network can not be understated, especially in a research setting. Being able to visualize a network's random variables and the dependencies between them is critical to determining whether the network is valid and useful.

With that, the pyBN package provides seamless integration of graphviz -- both standalone and with matplotlib by way of networkx -- for concise visualizations of BN's which have great layouts, and overall great asthetics.

In this tutorial, we will demonstrate the plotting functionality in pyBN.


In [12]:
from pyBN import *

bn = read_bn('data/cancer.bif')

With the above code, we loaded the pyBN package into the namespace, and read a BayesNet object from one of the files included with the package. Let's explore its attributes:


In [13]:
print bn.V
print bn.E


['Pollution', 'Smoker', 'Cancer', 'Xray', 'Dyspnoea']
[['Pollution', 'Cancer'], ['Smoker', 'Cancer'], ['Cancer', 'Xray'], ['Cancer', 'Dyspnoea']]

As we can see, this network has 5 vertices and 4 edges. Its visualization should not be much issue. Let's introduce the "draw_bn" function. Calling this function on a BayesNet object will give you a not-so-bad standard visualization of the network in matplotlib:


In [14]:
%matplotlib inline
draw_bn(bn)


Really, that doesn't look so nice. The nodes are too small and aggressively red. Luckily, we can pass in any attributes we like as allowed in the networkx function "draw_networkx". Let's start by changing the node size and the node color:


In [19]:
draw_bn(bn, node_size=2000, node_color='white')


As we can see, it's easy to make a small network look good. The real test of a BN visualization engine is how it performs with large networks. Let's load one and see:


In [22]:
bn2 = read_bn('data/win95pts.bif')
draw_bn(bn2)


Yikes, that will take some work. Or will it? Let's apply a few of the same arguments as before:


In [29]:
import pylab
pylab.figure(3,figsize=(12,12)) 
draw_bn(bn2, node_size=200, with_labels=False, node_color='white')


That's not bad, but clearly a lot of work still remains to be done! The visualization functionality is currently only limited by the user's imagination and ability to work with the networkx drawing capabilities. However, look for pyBN's drawing functionality to become much better in the future!