Cannot change to a different GUI toolkit: notebook - Warning in Jupyter - matplotlib

I want to use the interactive plots in jupyter, but when switching the backend to notebook, I get the warning:
Warning: Cannot change to a different GUI toolkit: notebook. Using qt5 instead.
This happens on Windows 10, Anaconda 1.8.7, jupyter 5.5.0, python 3.6.5 and matplotlib 2.2.2.
Minimal working example:
import matplotlib.pyplot as plt
%matplotlib notebook
Output:
Warning: Cannot change to a different GUI toolkit: notebook. Using qt5 instead.
I found some question here where the problem is the other way around, but could not find anyone having this problem yet.
In some cases, importing ipympl is mentioned as a solution, but this does not change anything in my case.
Any hints?

As ImportanceOfBeingErnest pointed out in the comments, my problem was related to the backend already being set when jupyter starts. However, as mentioned in this post, the backend needs to be set in the ipython_config.py, not the jupyter_notebook_config.py.
After restarting the notebook server, I can switch between backends as described here.

You must put %matplotlib widget in the very beginning of the Jupyterlab

Related

Mplot3D plot freezes when plotting in PyCharm Professional

Recently when I am doing MatPlotLib 3D plots in PyCharm, the debugging and the plot window are freezing when excuting the plt.show() command. In the top bar of the plot is mentioned (Not Responding). This happens both in PyCharm Community edition 2019 and PyCharm Professional 2021. I am running v3.8 of Python. If I execute plt.show(Block=True), the interactive 3D plot will come up, but I will need to close the figure to continue the debugging.
Does anyone have an idea how I can plot the figure with plt.show(), get up the interactive window, and continue debugging while still having the interactive window open? This worked in my previous version of Python v3.7, but the problem happened recently when upgrading to v3.8.
I recently faced the same issue when using matplotlib to plot. This seems to be related to a change in the order of precedence that matplotlib uses to pick its default backend. According to the matplotlibrc file that is bundled with the Python package, the order used to be
MacOSX Qt5Agg Gtk3Agg TkAgg WxAgg Agg
in previous versions (e.g., v3.4.2), while in more recent versions (e.g., v3.5.0) it has been be changed to
MacOSX QtAgg Gtk4Agg Gtk3Agg TkAgg WxAgg Agg
For me, setting the backend to Qt5Agg right after importing matplotlib in Python via
import matplotlib
matplotlib.use("Qt5Agg")
indeed solved the "Not responding" issue. Alternatively, we can specify it through the environment variable MPLBACKEND=Qt5Agg.
Likely, we could also set Qt5Agg as default backend in matplotlibrc either by modifying the bundled matplotlibrc file or by providing our own.
PS: Not sure whether the core issue that prevents the QtAgg backend from working correctly is in PyCharm, matplotlib, or PyQt...

matplotlib plots fail silently in vscode but work from the python interpreter in the same environment

Until today, matplotlib was working with vscode. TOday I updated vscode to the latest version and found that plots don't work. They work from the same virtual environment using the python interpreter but not from vscode. I rolled back to earlier versions of both matplotlib and vscode, but it still doesn't work. Here is my code to plot:
import matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2],[3,4])
plt.show()
The version of vscode is 1.53.2 and the version of matplotlib is 3.3.4
If I call plt.savefig, it saves the figure. It just doesn't display interactively.
I have tried this with multiple virtual envioronments and multiple vscode workspaces, all of which worked before today.
VSCode doesn't see my DISPLAY environment variable on my Linux system, so it is running headless. I can make plots work by giving the command "export DISPLAY=:0" in the vscode terminal window. I would like to have this happen automatically but I don't know how to make that happen.
The way I discovered this was by inserting "matplotlib.use("TKAgg")" in my python script. This elicited a message saying that I was running headless.
The way I found the correct value for DISPLAY was to examine that variable in a terminal window outside of vscode.
When I went to report this on the vscode Github page, I found the suggestion to disable all extensions. When I do that, I don't have to set DISPLAY. I then went and reenabled all extensions, and plotting still works, so it seems the fix was to disable and reenable all extensions.

Conda and Jupyter Notebook Environment Confusion

I am using Jupyter Notebook to help debug some issues I'm having moving between JSON and pandas. The specific application isn't important.
The important part is that I needed to use pandas.json_normalize() which apparently first showed up in pandas version 1.0.3. I was confused when Jupyter said it doesn't exist. I did a version check and got:
In[]: pd.__version
Out[]: 0.25.2
This is not the version of python installed in either my base environment or the conda environment that Jupyter Notebook is running in or that the app is running in. Version checks in both environments in Anaconda Prompt (outside of Jupyter Notebook) confirm this.
What is going on here? Looking around I haven't seen a good answer, but it does appear that other people have had the same issue --- Jupyter defaulting to pandas 0.25.2 for some reason.
It seems that your Notebook is using a different kernel/environment than what you want.
run this in the notebook to see which environment you are using
! which python
or try
import sys
print(sys.executable)
which would show you which environment it's using, if you have env named venv then you will get something like.
/home/your_home_directory/anaconda3/envs/venv/bin/python
If you don't care about all of that and you just want to update the pandas that it's using then copy that path and do this.
! pip install --upgrade pandas
Note that this will also depend on which version of python you are using

VSCode 'default dark' theme doesn't automatically defaults the matplotlib style to 'dark_background'

Summary
When figure is drawn with matplotlib using VSCode Jupyter Notebook, the figure is rendered in light theme even when the VSCode theme is dark. By my understanding, this issue was resolved and the VSCode is supposed to default the matplotlib figures to dark_background when the VSCode theme is default dark.
What am I doing wrong?
I am fairly new to this whole data science and machine learning scene, and up till now I have been using Jupyter Notebook (and sometimes Jupyter Lab) for all my coding needs. However, the code completion in Jupyter Notebook leaves something to be desired, and so began my search for alternatives to Jupyter Notebook.
I recently discovered that VSCode supports the use of Jupyter Notebook natively and after installing python extension, I was able to create new and open previously saved .ipynb files and every thing works great (especially the IntelliSense).
There is, however, one issue which is somewhat a nuisance. My theme for VSCode is default dark, and the figures produced by matplotlib are in light theme.
I know I can use following code to render the figures in dark mode:
from matplotlib import style
style.use('dark_background')
But on GitHub, this issue has already been addressed so that when the VSCode is in dark mode, then the VSCode will automatically default the matplotlib style to dark_background. However I am not experiencing this automation.
Is there some setting that I have failed to configure?
On macOS with VS version 1.63.2 I found that enabling the following setting did the trick:
After some searching I found this solution:
In settings (via ctrl+,) search for
jupyter run startup commands
edit the json to:
"jupyter.runStartupCommands": [
"import matplotlib.pyplot as plt",
"plt.style.use('dark_background')"
]
Then, restart your kernel.
Note, that somehow only using matplotlib.style.use('dark_background') does not yet fully work out, since some part gets overwritten on loading pyplot in the notebook it seems.

kernel busy when pylab is used in ipython notebook

I'm using ipython notebook 1.1.0 (and python 2.7.3) on chromium.
When I use pylab and apply
pylab.show()
the ipython notebook freezes with "kernel busy". I've seen that kernel busy is a common bug in ipynb.
After a while on terminal I got:
ATTENTION: default value of option force_s3tc_enable overridden by environment.
Any ideas on what to look for?
are you including "%matplotlib inline" in your code? I had this error, and adding it to my code solved the issue of a hanging kernel.
Check out the official documentation for a more thorough explanation .