Using matplotlib in Slicer jupyter notebook

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\", line 2355, in <module>
  File "C:\Users\mikeb\AppData\Local\NA-MIC\Slicer 4.11.0-2019-08-29\lib\Python\Lib\site-packages\matplotlib\", 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\", 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\", 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\", 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\", 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!

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

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).

1 Like

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

1 Like

Magic commands are not supported in SlicerJupyter.

1 Like

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

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.

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

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

1 Like

Thanks for taking a look!

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)

Complete example:

1 Like

Thanks, this is outstanding.

1 Like