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

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.

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

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.

Load methods/properties/module from Scikit-Learn in PyCharm

I am using PyCharm 3.4.1 (professional version). And I am exploring Scikit-Learn, NumPy etc. But unfortunately I can't get methods/properties/module in PyCharm when I write code with any installed package instead of Python core. Though when I import any module at the beginning of any .py file, it shows the corresponding methods/modules after pressing dot(.), but in code, PyCharm does not show the list after pressing dot(.).
How I can get all things after pressing dot(.)?
Thanks!

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.

Spyder together with Mayavi

Is it possible to use a different IDE as a front end while using the Canopy EPD engine in the back end? In particular, I'd like to use mayavi.mlab scripting from Spyder's ipython interpreter. I'm working on a macbook pro running OSX 10.8. Is there a way to set this up?
I have tried to set up mayavi and spyder using macports, but that does not seem to work. The only working version of mayavi that I have managed to set up runs from the commercial Canopy platform. So if I can't get the macports mayavi to work with the macports spyder, I am hoping to get the Canopy mayavi working with spyder.
Thanks.
Adrian
(Spyder dev here): This is what you need to do:
Run this command in a console
enpkg ipython update
Go to
Spyder > Preferences > Console > Advanced Settings > Python Executable
Select the option
Use the following interpreter
Note: This option is not available right now but it will be in 2.2.3. If it's not, then just the text field present there.
Put there the full path to Canopy's Python version, which you can get with these commands inside Canopy:
In [10]: import sys
In [11]: sys.executable
Out[11]: '/home/carlos/Enthought/Canopy_64bit/User/bin/python'
Maybe I'm missing what you want to do (I don't even know what mayavi is), but here's something you can try.
I'm using Spyder (version 2.2.1 ) with the Canopy EPD distribution (version 1.0.3)
Try doing this:
1) At the command line, update ipython with the enpkg command ( "enpkg ipython update" should work)
In my case, this reverted the ipython to ipython-0.13.1-2
2) Then in Spyder, go to preferences -> Console -> Advanced Settings and in the python executable put something like /Users//Library/Enthought/Canopy_64bit/User/bin/python
Restart Spyder and now you should be able to use the EPD distribution with all the modules you have installed.