Why Matplotlib GUI changes in IDLE and Spyder? - matplotlib

I am using exactly the same code and environment but Matplotlib graphs in IDLE and Spyder have different GUI (graph options). Why is this happening?
The version of matplotlib is 2.2.2, and Python is 3.6.8, Spyder 3.3.1.
Pictures are attached, so please refer to them. The area I marked have different options.
Matplotlib graph in IDLE
Matplotlib graph in Spyder
I actually would like to use graphs on ArcGIS but it appears with the IDLE one. I want to use the one from Spyder because it has few more options.

You use different Backends. In your case the GUI in IDLE is using "TkAgg", while the GUI in Spyder uses "Qt5Agg". The Backends user guide names 3 options to set the backend. Relevant for you are probably
Set the backend via the backend parameter in your matplotlibrc file (see Customizing Matplotlib with style sheets and rcParams):
backend : Qt5Agg # use Qt5Agg with antigrain (agg) rendering
Set the backend in the code
import matplotlib
matplotlib.use('Qt5Agg')
Those need to be at the start of your script before importing pyplot.
In Spyder you can select the backend via the
Tools/Preferences/IPython Console/Graphics/Graphics Backend option.

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.

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.

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

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

Matplotlib interactive windows hang when pyqt is imported

I'm using matplotlib 1.4 with the OSX backend:
>>> matplotlib.get_backend()
u'MacOSX'
If I plot anything, the window is active and healthy: I can use the zoom buttons, save, etc. The moment I import Qt:
from PyQt4 import QtGui, QtCore
the plot windows freeze, resulting in a spinning beach ball if I hover over them. I can get the plot windows to update with pl.draw(), but I cannot interact with them. In particular, Mac OS will no longer bring the windows to the front when activated via the Dock or apple-tab.
This is especially a problem because I am using another module that imports PyQt4, but I need to use that module with interactive plotting available. Is there a workaround for this problem?
Starting IPython with the --matplotlib argument loads and initialises matplotlib with the default backend that is set in your .matplotlibrc. There is a matplotlib limitation that you cannot switch backends once one has been loaded. This is described in the documentation for use():
This function must be called before importing pyplot for the first time; or, if you are not using pyplot, it must be called before importing matplotlib.backends.
There is an experimental switch_backend() but this is for image-only backends, and does not work with the Qt or MacOSX backends.
This feature is experimental, and is only expected to work switching to an image backend [...] If you try to interactively switch from one GUI backend to another, you will explode.
The upshot of all this is that it is not possible to change the backend once you have started IPython with --matplotlib (whether or not you plot, as you confirmed). The solution is to either start with ipython --matplotlib=qt or change the default backend in your .matplotlibrc file.
On MacOSX the matplotlib config file is found in ~/.matplotlibrc/matplotlibrc. Open that file and change the following line:
backend : MacOSX
to...
backend : Qt4Agg
Save the file, and IPython (and matplotlib) will now start up with the Qt4 backend by default when you use ipython --matplotlib.
More information in .matplotlibrc configuration is available in the documentation.