# Graphical visualization for pymc model

**URL:** <https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994>\
**Category:** v5\
**Tags:** modeling\
**Created:** [September 26, 2023, 3:33pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994 "2023-09-26T15:33:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 26, 2023, 3:33pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/1 "2023-09-26T15:33:55Z")

</div>

Hi, I am trying to visualize my pymc model using graph and I am not able to execute pm.model\_to\_graphviz()

I have tried the following -

```auto
from pymc import model_to_graphviz
import graphviz
os.environ["PATH"] += os.pathsep + r'/opt/conda/lib/python3.9/site-packages/graphviz'

with model_factory(df):
    graphviz = pm.model_to_graphviz()
graphviz

```

I could not find any .exe file in the graphviz package folder though.  
I am getting the following error -

```auto
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
/opt/conda/lib/python3.9/site-packages/graphviz/backend/execute.py in run_check(cmd, input_lines, encoding, quiet, **kwargs)
     78 kwargs['stdout'] = kwargs['stderr'] = subprocess.PIPE
---> 79 proc = _run_input_lines(cmd, input_lines, kwargs=kwargs)
     80 else:

/opt/conda/lib/python3.9/site-packages/graphviz/backend/execute.py in _run_input_lines(cmd, input_lines, kwargs)
     98 def _run_input_lines(cmd, input_lines, *, kwargs):
---> 99 popen = subprocess.Popen(cmd, stdin=subprocess.PIPE, **kwargs)
    100 

/opt/conda/lib/python3.9/subprocess.py in __init__ (self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)
    946 
--> 947 self._execute_child(args, executable, preexec_fn, close_fds,
    948 pass_fds, cwd, env,

/opt/conda/lib/python3.9/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, gid, gids, uid, umask, start_new_session)
   1818 err_msg = os.strerror(errno_num)
-> 1819 raise child_exception_type(errno_num, err_msg, err_filename)
   1820 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: PosixPath('dot')

The above exception was the direct cause of the following exception:

ExecutableNotFound Traceback (most recent call last)
/opt/conda/lib/python3.9/site-packages/IPython/core/formatters.py in __call__ (self, obj, include, exclude)
    968 
    969 if method is not None:
--> 970 return method(include=include, exclude=exclude)
    971 return None
    972 else:

/opt/conda/lib/python3.9/site-packages/graphviz/jupyter_integration.py in _repr_mimebundle_(self, include, exclude, **_)
     96 include = set(include) if include is not None else {self._jupyter_mimetype}
     97 include -= set(exclude or [])
---> 98 return {mimetype: getattr(self, method_name)()
     99 for mimetype, method_name in MIME_TYPES.items()
    100 if mimetype in include}

/opt/conda/lib/python3.9/site-packages/graphviz/jupyter_integration.py in <dictcomp>(.0)
     96 include = set(include) if include is not None else {self._jupyter_mimetype}
     97 include -= set(exclude or [])
---> 98 return {mimetype: getattr(self, method_name)()
     99 for mimetype, method_name in MIME_TYPES.items()
    100 if mimetype in include}

/opt/conda/lib/python3.9/site-packages/graphviz/jupyter_integration.py in _repr_image_svg_xml(self)
    110 def _repr_image_svg_xml(self) -> str:
    111 """Return the rendered graph as SVG string."""
--> 112 return self.pipe(format='svg', encoding=SVG_ENCODING)

/opt/conda/lib/python3.9/site-packages/graphviz/piping.py in pipe(self, format, renderer, formatter, neato_no_op, quiet, engine, encoding)
    102 '<?xml version='
    103 """
--> 104 return self._pipe_legacy(format,
    105 renderer=renderer,
    106 formatter=formatter,

/opt/conda/lib/python3.9/site-packages/graphviz/_tools.py in wrapper(*args, **kwargs)
    169 category=category)
    170 
--> 171 return func(*args, **kwargs)
    172 
    173 return wrapper

/opt/conda/lib/python3.9/site-packages/graphviz/piping.py in _pipe_legacy(self, format, renderer, formatter, neato_no_op, quiet, engine, encoding)
    119 engine: typing.Optional[str] = None,
    120 encoding: typing.Optional[str] = None) -> typing.Union[bytes, str]:
--> 121 return self._pipe_future(format,
    122 renderer=renderer,
    123 formatter=formatter,

/opt/conda/lib/python3.9/site-packages/graphviz/piping.py in _pipe_future(self, format, renderer, formatter, neato_no_op, quiet, engine, encoding)
    147 if codecs.lookup(encoding) is codecs.lookup(self.encoding):
    148 # common case: both stdin and stdout need the same encoding
--> 149 return self._pipe_lines_string(*args, encoding=encoding, **kwargs)
    150 try:
    151 raw = self._pipe_lines(*args, input_encoding=self.encoding, **kwargs)

/opt/conda/lib/python3.9/site-packages/graphviz/backend/piping.py in pipe_lines_string(engine, format, input_lines, encoding, renderer, formatter, neato_no_op, quiet)
    210 kwargs = {'input_lines': input_lines, 'encoding': encoding}
    211 
--> 212 proc = execute.run_check(cmd, capture_output=True, quiet=quiet, **kwargs)
    213 return proc.stdout

/opt/conda/lib/python3.9/site-packages/graphviz/backend/execute.py in run_check(cmd, input_lines, encoding, quiet, **kwargs)
     82 except OSError as e:
     83 if e.errno == errno.ENOENT:
---> 84 raise ExecutableNotFound(cmd) from e
     85 raise
     86 

ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [September 26, 2023, 3:45pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/2 "2023-09-26T15:45:29Z")

</div>

See [here](https://discourse.pymc.io/t/saving-graphviz-and-arviz-outputs/4317) for more info on saving graphviz plots.

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 26, 2023, 3:56pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/3 "2023-09-26T15:56:43Z")

</div>

Thanks for the reply. I guess the issue is with the system’s PATH variable settings. If I run the following code -

```auto
with model_factory(df):
    g = pm.model_to_graphviz()
    g.render("graphname", format="png")

```

It throws the same error as above and creates a “graphname” file like this. How to visualize this?

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/d/d0f2130b0858a8e3969f55e93148ad20966aa34d.png)

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 28, 2023, 9:59am UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/4 "2023-09-28T09:59:09Z")

</div>

Hi - I have been trying to follow the example in this notebook -

[https://www.pymc.io/projects/docs/en/v5.6.1/api/generated/pymc.model\_to\_graphviz.html](https://www.pymc.io/projects/docs/en/v5.6.1/api/generated/pymc.model_to_graphviz.html)

It says - _Alternatively, you may install the graphviz binaries yourself, and then pip install graphviz to get the python bindings_

Can anyone help me here in installing graphviz binaries? I am using a jupyter notebook from terminal for this. Thanks

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [September 28, 2023, 12:30pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/5 "2023-09-28T12:30:25Z")

</div>

A couple of questions. How did you install graphviz? What platform are you on? And can you share the output of `conda list graphviz`?

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 28, 2023, 1:13pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/6 "2023-09-28T13:13:34Z")

</div>

I am using ubuntu and launching a jupyter notebook from there. I installed graphviz like this -

```auto
pip install graphviz
from pymc import model_to_graphviz

```

And here is the output of conda list graphviz -

![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/0/0e24d36cc831f6e7faa3bcb1ee254baed12bf55d.png)

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [September 28, 2023, 2:35pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/7 "2023-09-28T14:35:18Z")

</div>

Looks like you don’t have graphviz itself installed. Check out [here](https://www.graphviz.org/download/) and [here](https://forum.graphviz.org/t/new-simplified-installation-procedure-on-windows/224) if you’re on windows.

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 28, 2023, 2:45pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/8 "2023-09-28T14:45:20Z")

</div>

I did install graphviz using pip. See below. Sorry, but is this not the right way?

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/b/b797d7d3ae8ac3c72c1c71a834c815f809e45073.png)

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [September 28, 2023, 2:53pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/9 "2023-09-28T14:53:27Z")

</div>

You installed python-graphviz. But if you read the installation instructions found [here](https://pypi.org/project/graphviz/) or [here](https://graphviz.readthedocs.io/en/stable/), you will see that one of the dependencies of python-graphviz is graphviz. python-graphviz is just connects python with graphviz.

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 29, 2023, 5:34am UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/10 "2023-09-29T05:34:45Z")

</div>

The [link](https://pypi.org/project/graphviz/) that you shared has this command -

```auto
pip install graphviz

```

I did the same but it is still downloading the python interface package -

```auto
ubuntu@run-65165e55753e5058124cd5e8-pvd6d:/mnt/code$ pip install --user graphviz
Collecting graphviz
  Downloading 
     |████████████████████████████████| 47 kB 1.5 MB/s             
Installing collected packages: graphviz
Successfully installed graphviz-0.20.1
ubuntu@run-65165e55753e5058124cd5e8-pvd6d:/mnt/code$ pip show graphviz
Name: graphviz
Version: 0.20.1
Summary: Simple Python interface for Graphviz
Home-page: https://github.com/xflr6/graphviz
Author: Sebastian Bank
Author-email: sebastian.bank@uni-leipzig.de
License: MIT
Location: /home/ubuntu/.local/lib/python3.9/site-packages
Requires: 
Required-by: 
ubuntu@run-65165e55753e5058124cd5e8-pvd6d:/mnt/code$ 

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [September 29, 2023, 10:22am UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/11 "2023-09-29T10:22:50Z")

</div>

Yes. And the python interface is required. But that link also states:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/6/68ebe7f9a110979286b5db4b7dd79a8f68b1634a.png)

So you need to install graphviz itself (in addition to the python interface) using the instructions linked to in that screenshot (which I also linked to above).

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [September 29, 2023, 12:24pm UTC](https://discourse.pymc.io/t/graphical-visualization-for-pymc-model/12994/12 "2023-09-29T12:24:33Z")

</div>

Thanks @cluhmann for the help. It works now!
