Pycharm plot windows doesn't open with 'Agg' backend - python

I am pretty new to Pycharm (community edition), which I am using via Thinlinc client on a remote desktop server. The issue I am having is that I am not able to create interactive plots. On my local computer with Canopy, after importing matplotlib and calling the plot( ... ) command on the console with some variable X inside, the plot window opens.
However, in my remote machine on the Pycharm console, calling plot() does not give errors, but does not open a window (not even when calling .show()). By contrast, plt.savefig() works perfectly. I tried to get the following info:
matplotlib.is_interactive()
Out[8]: True
matplotlib.get_backend()
Out[10]: 'agg'

Related

Forcing `Interactive mode` in the VSCode Python debugger

If I run a Python program under the debugger in VSCode, and the program
has the line import matplotlib.pyplot as plt, I see the following
message in the Python Debug Console (under the TERMINAL tab):
Backend TkAgg is interactive backend. Turning interactive mode on.
This is desired, as now I don't have to type plt.show() after every
plt.plot() when I'm exploring data in the REPL under the
DEBUG CONSOLE tab.
The problem is, not every program has that import line, and so I'm
forced to manually do so in the debug REPL, and follow each plt.plot()
with a plt.show().
My question: Is there a way to force this 'interactive mode',
perhaps through the launch.json file? I've looked at the docs for launch.json
but don't see anything applicable.
I'm using VSCode 1.63.2 on Windows 10 Version 20H2.

Attach external system terminal into Spyder

I have been running into a trouble whereby Spyder IPython console is not producing Matplotlib figures as desired. I thought initially that there is something wrong in my code since jupyter notebook gives me the same wrong figures. However, when running the script in Spyder using external terminal the figures are produced as desired. Also, when I run the code in VSC the correct figures are displayed.
So the only option I am left with in Spyder is to use the external terminal to execute the code. However, it is quite a pain every time to run some codes and then manually close the terminal.
I would like to know if there is a way to permanently attach the external terminal inside Spyder? I hate the IPython console when it comes to plotting matplotlib figures!!
(Spyder maintainer here) Sorry but there's no way to dock an external Python terminal inside Spyder.

Manually closing matplotlib plots doesn't release the program

When I call pyplot.show(block=True) a window with the correct plot appears. When trying to manually close it, the window closes but the program won't continue to run (like it's still blocked).
For instance, the following program will display the plot window and print Before, but after manually closing the window, the program still seems blocked, and After is not printed.:
plt.plot(x,y)
print("Before")
plt.show(block=True)
print("After")
I work with the latest matplotlib version (2.1.2) using the interactive TkAgg backend on Python 3.5.2.
Update - Additional info: I work with Pycharm 2017.2 on Ubuntu (VM on Windows host)
Solved by unselecting the "Use IPython if available" checkbox in PyCharm's settings.

Configuring macOS PyCharm for X11 Forwarding

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.linspace(1,1000)
plt.plot(np.linspace(1, 1000))
print("Works")
plt.show()
I am trying to run the simple code above within PyCharm on a remote machine, but showing the plots on my local machine (mac). The plot does not appear. I do have xQuartz X11 Server running.
Pycharm runs the remote interpreter fine.
If I run it from macOS terminal, using
ssh -X pier#129.168.0.181
python test.py
plt.show() works.
I reckon that the missing piece is the -X which enables the X11 to be forwarded to my local machine.
Where do I include this with PyCharm's command to ssh? I'm spending too much time trying to figure this out...
Note: I'm also not able to use PyCharm's Python Console to do plotting. No errors are shown but the plot is not forwarded to my local machine.
Ok, I found I needed to do two things to get it working well enough for me :
(1) Set DISPLAY = localhost:10.0 in the Environment Variables under Build, Execution, Deployment -> Python Console
(2) Right after
import matplotlib
matplotlib.use('Qt5Agg')
With this, I can use the remote interpreter as if it were local.
Building of #Ippiers answer, on windows this works via:
install xming (and have it running)
run a putty session with X11 forwarding enabled
env on the putty session and check the DISPLAY variable, it will probably be localhost:10.0
Set this display variable in pycharms run configuration DISPLAY=localhost:10.0
matplotlib.use('TkAgg') as Qt5 gave me errors

Cannot manually close matplotlib plot window

I'm running Python v2.7 and matplotlib v1.5.0 on Mac OS X Yosemite. Up to recently, I was able to run my script in the interactive interpreter, show a plot, and then manually kill the window. For example
import numpy as np
x = np.arange(1,10)
y = np.arange(1,10)
plt.plot(x,y)
plt.show()
This used to work fine. The window would close and I'd return to the prompt. However, now when I hit the red X to close the window, the window remains open. My command prompt returns and works fine, but the window is stuck and I see the spinning beach ball over it.
(Note that the cursor doesn't appear in the image, but you can see the red X is greyed out because I've hit close but it is stuck open).
It's not until I run the script a second time that the first window closes, but then I'm stuck with a second open plot with the same problem. I'm a bit confused because this only happened recently. Up till now, the window would close fine.
It's not a huge issue, because Python still runs and I can still make new plots, but I'm curious as to why the window would all of a sudden stick open. Any advice?
UPDATE
I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue. But the question remains why the macosx and CocoaAgg backends show this behavior.
For a permanent solution (I'd rather not have to switch backends every time I open ipython) you can modify matplotlibrc.
There are various matplotlibrc files that can be changed (i.e. for just a local directory, or globally, etc.). To find the configuration file that's been loaded for your ipython session, use matplotlib.matplotlib_fname(). Here's what I got:
In [1]: import matplotlib
In [2]: matplotlib.matplotlib_fname()
Out[2]: u'/usr/local/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
Opening the rc file and changing the line:
backend : macosx
to read:
backend : Qt4Agg
did the trick for me.
I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue.

Categories

Resources