spyder won't show the matplotlib window - python

i am using spyder to run the following script
import matplotlib.pyplot as plt
fig = plt.figure()
ax=fig.add_subplot(111)
ax.plot([2,1,3])
plt.ion()
plt.show()
input('something')
the problem is, that the matplotlib-window appears at the end of the script (after i entered 'something')
when i'm executing this script in a terminal the window appears and i can enter 'something' simultaneously - this is what i need.
does someone has an idea what causes this problem?

Related

Matplotlib not showing plot when program is executed in the VS Code terminal

I have this simple program here
import numpy as np
import matplotlib.pyplot as plt
num_of_intervals = 2000
x = np.linspace(-10,10,num=num_of_intervals)
y_inputs = 1/(1+np.exp(-x)) # SIGMOID FUNCTION
plt.figure(figsize = (15,9))
plt.plot(x,y_inputs,label = 'Sigmoid Function')
plt.vlines(x=0, ymin = min(y_inputs), ymax=max(y_inputs), linestyles='dashed')
plt.title('Sigmoid Function')
plt.show()
When the above program is ran in the vscode terminal. The plot cannot be seen (usually a pop-up window appears showing the plot).
But when the program is ran in the Ubuntu terminal, the plot can be seen as a pop-up window.
Any idea how I can solve this issue with vscode.
OS : Ubuntu 20.04
Visual Studio Code 1.54.3
Python : 3.8.5
Double check that this option is turned on in Settings: terminal.integrated.inheritEnv

render matplotlib figure from debugger

Cannot get the following code to do all three things:
stop at the debug breakpoint
render the figure
return control to the console with the figure rendered (in debugger mode)
import matplotlib.pyplot as plt
from ipdb import set_trace
fig, ax = plt.subplots()
ax.plot(range(10))
plt.show()
set_trace()
The use case to do all three things simultaneously is debugging inside of a module that requires information in the matplotlib visualization.
Running IPython from the console as ipython --pylab accomplishes (1) and (3) above only, as shown below. Using plt.ion() in the code does the same. The debugger is available but the visualization will not render.
Running IPython from the console as just ipython, or running python <script.py>, accomplishes (1) and (2) above only, as shown below. The visualization has rendered but the debugger is not available.
Right now I am using python 3.7.7, matplotlib 3.1.3 with Qt5Agg backend, ipython 7.13.0, and ipdb 0.12.3.
If you enable the interactive mode using ion(), you can achieve it while running python <script.py>. It will show the plots (by calling draw) immediately after plot and leave the control back to the console at set_trace.
import matplotlib.pyplot as plt
from ipdb import set_trace
# Enable interactive mode
plt.ion()
fig, ax = plt.subplots()
# Shown immediately
ax.plot(range(10))
set_trace()
Scenario 1
import matplotlib.pyplot as plt
from ipdb import set_trace
fig, ax = plt.subplots()
ax.plot(range(10))
plt.show()
set_trace()
In your example, you are
in ipython,
not in interactive mode.
Hence, plt.show() blocks execution of the rest of the script until the figure is closed.
Scenario 2
import matplotlib.pyplot as plt
from ipdb import set_trace
# Enable interactive mode
plt.ion()
fig, ax = plt.subplots()
ax.plot(range(10))
# Shown immediately
set_trace()
With #ilke444 code you are in interactive mode. However, interactive mode works a little bit differently then #ilke444 expects, given the code comment. It does not force a draw immediately but when control is returned to the REPL, in your case ipython. However, we never get there as we enter the debugger before that happens.
Scenario 3
import matplotlib.pyplot as plt
from ipdb import set_trace
# Enable interactive mode
plt.ion()
fig, ax = plt.subplots()
ax.plot(range(10))
plt.show() # or: fig.canvas.draw() or plt.pause()
set_trace()
#ilke444 suggestion in the comment works because we actually force the figure draw before entering the debugger.
I encountered a similar problem in the following situation:
I am running on debug mode on Pycharm and stopped in a certain place
Then I tried to run a function in the debug console that's supposed to plot figures using matplotlib.pyplot and it didn't
The solution was to actually delete the plt.show() at the end of the function
now it works

Jupyter (IPython) notebook: plot in the qt mode + prompt raw_input()

I am trying to plot in Jupyter notebook (Python 2.7), then prompt user for input, save it and then change the plot (this is a crucial point: I don't want to create a new plot, I need to modify the old one after user input). This completely fails to work. Instead of showing the figure and then prompting for input, it opens the figure window, but freezes (doesn't display anything) until I respond to the raw_input() prompt. Only then it plots.
Simple version of the code to show the error:
import matplotlib.pyplot as plt
%matplotlib qt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3],[1,2,3])
plt.show(block=False)
my_input = raw_input()
This bug only appears when I use %matplotlib qt, but I have to use it, because with %matplotlib inline I am unable to modify the plot after it was displayed (at least as far as I am aware).
In fact, I noticed that it freezes until the end of the cell execution, even if it is just time.sleep().
Am I missing something? Some settings of how matplotlib displays figures?
Since I am using Python3 I had to change raw_input() to input() and removed the block=False because IPython told me that this is an unknown attribute.
This should work nice:
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3], [1,2,3])
plt.show()
my_input = input()
Fur sure, you need to adapt this back to Python2 to fit your needs.

pyplot.show function in spyder with python 3.4

I'm a novice with Python and working through "Machine Learning In Action" by P. Harrington. I'm having issues seeing my plot when using the pyplot.show() function. Below is the code that I'm entering in the IPython console on the bottom right of Spyder.
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
plt.show()
After I enter this last line, nothing happens. Does anyone have an idea why it doesn't appear?

Matplotlib figure window disappears when in interactive mode [duplicate]

This question already has answers here:
Interactive(?) plotting in Spyder with matplotlib
(2 answers)
Closed 3 months ago.
I need to use interactive mode when plotting with matplotlib (it should be a script, not python or ipython console). But setting plt.ion() causes a strange bug (?). When I try to plot my figure (I don't think it really matters, what I do exactly, because in non-interactive mode it works perfectly fine) I don't see it - I get a blank grey window for split-second, which momentarily disappears and the programme exits.
If I explicitly add plt.draw() (and plt.pause(1) to see the result), I see that the figure appears as expected. If I do the same after modifications I want to do to the figure when it is visible, the figure changes. But the window still disappears after the pause is over.
I run it in Spyder with Qt4Agg as a backend under Ubuntu. Tried running the script from terminal as python my_script.py, the result is identical.
What could be the problem? How do I stop the figure from disappearing when in interactive mode?
UPDATE
Working example:
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
x = np.linspace(1, 10)
y = np.sin(x)
plt.plot(x, y)
plt.draw()
plt.pause(1)
If I run this code I see the sine plot for 1 second, then the window disappears.
UPDATE 2
I found a solution here: https://stackoverflow.com/a/10724654/1304161
If I set the run options in Spyder correctly, it works perfectly fine. Although running it in gnome-terminal doesn't work, I don't really need it. Hopefully, there won't be a problem with this when it becomes a part of a GUI app...
I found a solution here: https://stackoverflow.com/a/10724654/1304161
If I set the run options in Spyder correctly, it works perfectly fine. Although running it in gnome-terminal doesn't work, I don't really need it. Hopefully, there won't be a problem with this when it becomes a part of a GUI app. I will be back, if it will be a problem then :)
You can make it work by adding these two lines at the end:
plt.ioff()
plt.show()
So this program works fine:
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
x = np.linspace(1, 10)
y = np.sin(x)
plt.plot(x, y)
plt.draw()
plt.ioff()
plt.show()
To display multiple figures at different times, you can use code.interact(local=locals()), after plt.show() to pause the Python interpreter until a Ctrl-Z is pressed in the Python Shell:
import code
import matplotlib.pyplot as plt
import numpy as np
# Start pyplot's "interactive mode" which lets you run plt.show multiple times
plt.ion()
x = np.linspace(1, 10)
y = np.sin(x)
# PLOT #1 - displayed
plt.plot(x, y)
plt.draw()
plt.show()
# Wait for figure to be closed AND Ctrl-Z pressed in Python Shell
code.interact(local=locals())
print("Some more code can go here...")
# PLOT #2 - displayed
plt.plot(x, y)
plt.show()
# Wait for figure to be closed AND Ctrl-Z pressed in Python Shell
code.interact(local=locals())

Categories