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.
Related
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
I want to display an animation in Jupyter using Matplotlib. Here is some basic example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)
def update(data):
line.set_ydata(data)
return line,
def data_gen():
while True:
yield np.random.rand(10)
ani = animation.FuncAnimation(fig, update, data_gen, interval=100);
from IPython.display import HTML
HTML(ani.to_jshtml())
When I run the code for the first time (or after restarting the kernel) I get what I want:
However, when I run the very same code for the second time I get a leftover in the left bottom:
I noticed that when I add %matplotlib inline at the top, then I got the bad output even after restarting the kernel. Thus my guess is that I have to set the magic command %matplotlib to default at the top each time I create an animation, but I can't even find if %matplotlib have a default value.
I use Anaconda. Here are my versions:
Conda version: 4.4.10
Python version: Python 3.6.4 :: Anaconda, Inc.
IPython version: 6.2.1
Jupyter version: 5.4.0
I used plt.close() to stop the first (unwanted) plot, and have not seen issues running the animation in a separate cell. I believe the issue is similar to those linked in the comments, jupyter is automatically displaying an unwanted plot for the first two lines - fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10)). I tired suggestions such as using semicolons at end of lines and a few different magic attempts, but no joy. A more concrete solution will no doubt appear, but for now....
I have plotted an interactive figure, run the cell, and now all my keyboard presses are being captured by the interactive plot. How do I exist this without the mouse?
Shift-enter sort of works, but it seems to require there be a cell below the plot.
I think matplotlib recommends ctrl-w but as I am in a web browser (Jupyter) that would just close my tab.
The plot is within the cell.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1,1)
x = np.linspace(0, 1, 100)
y = np.random.random(size=(100, 1))
ax.plot(x, y)
If you run this, you can't then use j,k to move up and down cells until you exit the interactive plot.
This is just a code snippet, in the actual code I am updating the plot from within a loop which is why I'm using interactive mode.
You can add another short-key to close the plot. This can be accomplished via the rcParams.
So let's say you want to close the plot by pressing q.
%matplotlib notebook
import matplotlib.pyplot as plt
plt.rcParams["keymap.quit"] = "ctrl+w", "cmd+w", "q"
plt.plot([1,3,2])
Now pressing q will exit the interactive plot, such that you can navigate as usual through the notebook. E.g. to then get to the next cell, which would already be active, just press Enter.
Eventually you would probably want to change your matplotlib rc file with this option not to have to type it in every time you start a notebook.
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?
I am trying to use the ipython in canopy with matplotlib to prepare graphs (backend set to qt). I wrote the following code line by line int the terminal
import matplotlib.pyplot as plt
fig = plt.figure()
s = fig.add_subplot(1,1,1)
after the second line I can see the figure being made. However after the third line I do not see the sub plot being created. However If I print fig, the sub-plot is can be seen both inline and in the figure window created. This sub-plot also magically appears if I try to zoom. Similar thing happens every time i plot something on the figure. The old version is displayed till I either print the figure or if i try to modify the view using the GUI tools. This is really annoying. It would be great if someone could tell me where the problem is.
Edit: Tried using fig.show() which does not work. Also when I use the plt.plot() directly, there seems to be no problem. The problem comes only when i use fig or any of its subplots
type:
fig.show() when you need to update it.
you should try using fig.canvas.draw() instead of using fig.show() when it comes to interactive plots.
import matplotlib.pyplot as plt
fig = plt.figure()
fig.show()
## should show an empty figure ##
s = fig.add_subplot(1,1,1)
fig.show()
## things stay unchanged ##
fig.canvas.draw()
## things should be OK now ##