Problem with 3D charts
I’ve been using Jupyter notebooks inside Visual Studio Code as my Python coding environment, and really enjoy its capability to mix markdown, code, text output and charting in the same place. Recently, I want to visualize the distribution pattern of my dataset for trimming. As I plot a 3D graph using the Matplotlib library, a static inline graph works fine in my notebook, but it provides not much value as it flattens one axis where my data are concentrated. So I try to produce an interactive 3D graph to rotate along the axes or zoom in to a particular region for further examination. However, when doing so, I get the error UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
This happens when running either a Jupyter notebook (.ipynb) or directly from a Python (.py) file in VS Code. Or if I’m running a Jupyter notebook in Azure Data Studio), I’ll get another error JavaScript output is disabled in Notebooks. I have Python ver. 3.10 and Matplotlib ver. 3.5.2 in my machine.
I did some research and learnt that Matplotlib can be set to use different backends to support various types of outputs (e.g., save to PDF, inline - as in a shell or inside Jupyter, interactive, etc.). My current setup seems to be missing the necessary backend, which should be a GUI toolkit for Matplotlib to render graphs outside of Jupyter. Okay, so how can we set it to use a GUI backend?
Tried but failed solutions
I’ve searched high and low for solutions as this seems to be a very common problem among Matplotlib users. Here are some I’ve leveraged from StackOverflow and personal blogs.
do a
pip install tk
for the tkinter interface. This completed successfully, and I followed the instruction to import the tkinter module in code, but it claims the module is not found.import the TKinter module and specify it to be used Matplotlib’s backend as follows, still doesn’t work.
import tkinter import matplotlib matplotlib.use('TkAgg')
install ipympl and followed its documented usage.
use the
%matplotlib
magic line command to setup a backend for plotting. I used%matplotlib widget
or%matplotlib notebook
at the beginning of my code block. These two were very popular answers in SO but they don’t work.do a
pip install tkinter
(couldn’t find this module)per multiple SO answers, tkinter is python version-specific, so I tried to install tkinter for my version of python by doing
pip install python-3.10-tk
(couldn’t find this module).Use Jupyter Lab to run the sample code below, together with doing #2 above. Got a ModuleNotFound error for ipympl.
Setting environmental variables.
What works finally
Turns out this is a fundamental issue in which the TCL/TK GUI toolkit is missing from my Python installation. Installing the TKinter wrapper and all other stuff just won’t fix this gap! What better than to restart the Python installation to modify my existing bits? This time I chose Modify and made sure I checked tcl/tk and IDLE. After that, I can import and use the tkinter
module, and set matplotlib
to use TKAgg
as the GUI backend. The interactive plot finally shows up successfully!
Here’s a very basic sample that I used to test whether my 3D scatter plot could be displayed interactively. And of course after properly installing the toolkit, my original datset was also rendered correctly in interactive mode.
import tkinter
import matplotlib
matplotlib.use('TkAgg') #specify a GUI backend for display
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# create a sample dataset
x =[1,2,3,4,5,6,7,8,9,10]
y =[2,3,4,6,8,2,9,1,4,5]
z =[1,5,7,5,7,9,3,4,2,6]
# create the plot
# plt.rcParams["figure.figsize"] = (16,14)
ax.scatter(x, y, z, c='g', marker='o')
# set title and axis labels
ax.set_title("Sample 3D plot")
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# display the plot
plt.show()
Here’s the interactive plot. Notice that you can change the parameters, zoom, pan and save the plot! What a nice reminder of my college days using Mathematica and MathCAD in the lab :-)
Alternatives that should also work
If you’re running on Mac or
Linux distros,
here are setup instructions for making sure TK is installed properly in your machine. And since this is basically a missing GUI backend issue, you can install any interactive
backends that Matplotlib supports, e.g., installing Qt5Agg, importing it and switching to it by invoking matplotlib.use()
in code.