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