matplotlib draw showing nothing - python

I'm using python's matplotlib to do some contours using contour and contourf functions. They all work fine when using show, but when I try to use draw() inside a method, I get the matplotlib window but not graph. The show() call will be done much later on the code and in a different method, and I would like to show one graph at the moment when it's done with draw(), not having to wait until the much later show(). What I'm doing wrong?
Thanks.

Have you turned interactive mode on using ion()? The following works for me on OSX, using the Tk backend and running from the shell's command line:
import matplotlib.pyplot as plt
plt.ion()
plt.figure()
for i in range(10):
plt.plot([i], [i], 'o')
plt.draw()
raw_input("done >>")
That is, as it does each loop, you see the plot change (i.e., it gets redrawn) as each point is added. Here, btw, if I instead call plt.ioff(), I don't see the figure or any updates.

IIRC ,You should be able call fig.show() multiple times. Also, check out using ipython (ipython -pylab) and http://matplotlib.sourceforge.net/users/shell.html

Related

Seaborn pairplot is closing immediately [duplicate]

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.

how to plot the image predicted by keras [duplicate]

The behavior of matplotlib's plot and imshow is confusing to me.
import matplotlib as mpl
import matplotlib.pyplot as plt
If I call plt.show() prior to calling plt.imshow(i), then an error results. If I call plt.imshow(i) prior to calling plt.show(), then everything works perfectly. However, if I close the first figure that gets opened, and then call plt.imshow(i), a new figure is displayed without ever calling plt.show().
Can someone explain this?
If I call plt.show() prior to calling
plt.imshow(i), then an error results.
If I call plt.imshow(i) prior to
calling plt.show(), then everything
works perfectly.
plt.show() displays the figure (and enters the main loop of whatever gui backend you're using). You shouldn't call it until you've plotted things and want to see them displayed.
plt.imshow() draws an image on the current figure (creating a figure if there isn't a current figure). Calling plt.show() before you've drawn anything doesn't make any sense. If you want to explictly create a new figure, use plt.figure().
... a new figure is displayed without ever
calling plt.show().
That wouldn't happen unless you're running the code in something similar to ipython's pylab mode, where the gui backend's main loop will be run in a separate thread...
Generally speaking, plt.show() will be the last line of your script. (Or will be called whenever you want to stop and visualize the plot you've made, at any rate.)
Hopefully that makes some more sense.

How to make Python wait for a function to end? [duplicate]

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.

Difference between plt.draw() and plt.show() in matplotlib

I was wondering why some people put a plt.draw() into their code before the plt.show(). For my code, the behavior of the plt.draw() didn't seem to change anything about the output. I did a search on the internet but couldn't find anything useful.
(assuming we imported pyplot as from matplotlib import pyplot as plt)
plt.show() will display the current figure that you are working on.
plt.draw() will re-draw the figure. This allows you to work in interactive mode and, should you have changed your data or formatting, allow the graph itself to change.
The plt.draw docs state:
This is used in interactive mode to update a figure that has been altered using one or more plot object method calls; it is not needed if figure modification is done entirely with pyplot functions, if a sequence of modifications ends with a pyplot function, or if matplotlib is in non-interactive mode and the sequence of modifications ends with show() or savefig().
This seems to suggest that using plt.draw() before plt.show() when not in interactive mode will be redundant the vast majority of the time. The only time you may need it is if you are doing some very strange modifications that don't involve using pyplot functions.
Refer to the Matplotlib doc, "Interactive figures" for more information.

Matplotlib python show() returns immediately

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.

Categories