# Using matplotlib in Slicer jupyter notebook

**URL:** <https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310>\
**Category:** Development\
**Tags:** feature\
**Created:** [September 5, 2019, 2:31pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310 "2019-09-05T14:31:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 5, 2019, 2:31pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/1 "2019-09-05T14:31:10Z")

</div>

Hello, I am using Slicer 4.11.0-2019-08-29 with jupyter notebook. I would like to be able to use matplotlib in the notebook to do things like plot histograms. I have successfully pip-installed matplotlib with `slicer.util.pip_install('matplotlib')`, and get no errors with `import matplotlib`. However, if I `import matplotlib.pyplot as plt`, I get the following error message:

```
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\site-packages\matplotlib\pyplot.py", line 2355, in <module>
    switch_backend(rcParams["backend"])
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\site-packages\matplotlib\pyplot.py", line 221, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\importlib\ __init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\site-packages\matplotlib\backends\_backend_tk.py", line 6, in <module>
    import tkinter as tk
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\tkinter\ __init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

```

I am guessing that, as the error message suggests, the Slicer python may not be configured for Tk (since it uses Qt for GUI), but I’m not sure how to fix this. I have not had any luck googling to figure out how to install Tk because everywhere I look says “Tk is already included with standard python, you don’t have to install it”.

Any suggestions? I just want to use matplotlib for basic plotting and data exploration as I am working on algorithm development for a segmentation/processing module. If there is another easy-to-use alternative which will already work with Slicer that would be fine. I have a little experience with matplotlib and know it works well with jupyter notebooks, so that’s why I was trying to use that tool.

Thanks for considering and for any suggestions!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 5, 2019, 3:40pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/2 "2019-09-05T15:40:20Z")

</div>

You need to choose a suitable matplotlib backend. See example here:

> [@Use full power of Python in Slicer](https://discourse.slicer.org/t/use-full-power-of-python-in-slicer/7162/5):
>
> I’ve just tried interactive plotting using matplotlib and it works fine for me on Windows. I just had to switch to WXAgg backend. Setup: pip\_install("matplotlib wxPython") Example interactive plot using matplotlib: # Get a volume from SampleData and compute its histogram import SampleData import numpy as np volumeNode = SampleData.SampleDataLogic().downloadMRHead() histogram = np.histogram(arrayFromVolume(volumeNode), bins=50) # Set matplotlib to use WXAgg backend import matplotlib matplotl…

Note that you can also use Slicer’s built-in interactive plotting features. The advantage of Slicer plots is that they are directly connected to MRML nodes: if you update a value in a table node then the plot is updated immediately (and you can drag-and-drop points in the plot to change values in a table).

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 5, 2019, 4:22pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/3 "2019-09-05T16:22:09Z")

</div>

OK, install works great, I can generate plots and view them with plt.show(). However, the `%matplotlib inline` magic command for jupyter notebook doesn’t seem to work (gives a syntax error). Should this work?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 5, 2019, 6:38pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/4 "2019-09-05T18:38:33Z")

</div>

Magic commands are not supported in SlicerJupyter.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 5, 2019, 6:39pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/5 "2019-09-05T18:39:29Z")

</div>

OK, that’s not too bad. Thanks very much for your help.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 6, 2019, 4:03pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/6 "2019-09-06T16:03:51Z")

</div>

I see that SlicerJupyter’s display() command can put images into the output field. Is there a way to create a function to do that with a png image I generate?  
My ability to read C++ is very limited, but it looks to me like the display function ends up running `execute_display_command`, which writes the image in some format to standard out.

> <https://github.com/Slicer/SlicerJupyter/blob/672f13cd141955466ea82033bdfa66a63c021449/JupyterKernel/xSlicerInterpreter.cxx#L166-L185>

  
Could I possibly do something similar from python? If so, how?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 6, 2019, 6:37pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/7 "2019-09-06T18:37:56Z")

</div>

I’ll have a look and get back to you soon.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 6, 2019, 7:07pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/8 "2019-09-06T19:07:52Z")

</div>

Thanks for taking a look!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 7, 2019, 12:08pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/9 "2019-09-07T12:08:57Z")

</div>

I’ve updated `display()` function to accept variables and files. You can now display a png file using:

```
display(filename='MatplotlibExample.png', type="image/png", binary=True)

```

[![](https://www.slicer.org/w/images/a/a2/JupyterNotebookMatplotlibExample.png) ](https://www.slicer.org/w/images/a/a2/JupyterNotebookMatplotlibExample.png)

Complete example: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Plot\_in\_Slicer\_Jupyter\_notebook](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Plot_in_Slicer_Jupyter_notebook)

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 9, 2019, 4:31pm UTC](https://discourse.slicer.org/t/using-matplotlib-in-slicer-jupyter-notebook/8310/10 "2019-09-09T16:31:41Z")

</div>

Thanks, this is outstanding.
