plt.figure.Figure.show() does nothing when not executing interactively
plt.show
starts an event loop, creates interactive windows and shows all current figures in them. If you have more figures than you actually want to show in your current pyplot state, you may close all unneeded figures prior to calling plt.show()
.
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
ax1.plot([1,3,4])
fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
ax2.plot([1,2,5])
# close first figure
plt.close(fig1)
# show all active figures (which is now only fig2)
plt.show()
In contrast fig.show()
will not start an event loop. It will hence only make sense in case an event loop already has been started, e.g. after plt.show()
has been called. In non-interactive mode that may happen upon events in the event loop. To give an example, the following would show fig2
once a key on the keyboard is pressed when fig1 is active.
import matplotlib.pyplot as plt
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
ax1.plot([1,3,4])
def show_new_figure(evt=None):
fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
ax2.plot([1,2,5])
fig2.show()
# Upon pressing any key in fig1, show fig2.
fig1.canvas.mpl_connect("key_press_event", show_new_figure)
plt.show()
Matplotlib `figure.show()` does not show anything on Mac OSX
figure.show()
makes sense in interactive mode, not for permanently showing a figure. So you need to use plt.show()
. In order not to show the second figure, you may close it beforehands,
plt.close(figure2)
plt.show()
Matplotlib plots aren't shown when running file from bash terminal
You need to add matplotlib.pyplot.show()
in your code to show plots in non-interactive mode. See docs at http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.show
After further info from OP, blocking had to be enabled explicitly using plt.show(block=True)
.
How can I show figures separately in matplotlib?
Sure. Add an Axes
using add_subplot
. (Edited import
.) (Edited show
.)
import matplotlib.pyplot as plt
f1 = plt.figure()
f2 = plt.figure()
ax1 = f1.add_subplot(111)
ax1.plot(range(0,10))
ax2 = f2.add_subplot(111)
ax2.plot(range(10,20))
plt.show()
Alternatively, use add_axes
.
ax1 = f1.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(range(0,10))
ax2 = f2.add_axes([0.1,0.1,0.8,0.8])
ax2.plot(range(10,20))
Plots not showing in Jupyter notebook
If you are working with a Jupyter Notebook then you can add the following line to the top cell where you call all your imports. The following command will render your graph
%matplotlib inline
Is there a way to detach matplotlib plots so that the computation can continue?
Use matplotlib
's calls that won't block:
Using draw()
:
from matplotlib.pyplot import plot, draw, show
plot([1,2,3])
draw()
print('continue computation')
# at the end call show to ensure window won't close.
show()
Using interactive mode:
from matplotlib.pyplot import plot, ion, show
ion() # enables interactive mode
plot([1,2,3]) # result shows immediatelly (implicit draw())
print('continue computation')
# at the end call show to ensure window won't close.
show()
Pandas plot doesn't show
Once you have made your plot, you need to tell matplotlib to show
it. The usual way to do things is to import matplotlib.pyplot
and call show
from there:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
plt.show()
In older versions of pandas
, you were able to find a backdoor to matplotlib, as in the example below. NOTE: This no longer works in modern versions of pandas
, and I still recommend importing matplotlib separately, as in the example above.
import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
pd.tseries.plotting.pylab.show()
But all you are doing there is finding somewhere that matplotlib
has been imported in pandas
, and calling the same show
function from there.
Are you trying to avoid calling matplotlib
in an effort to speed things up? If so then you are really not speeding anything up, since pandas
already imports pyplot
:
python -mtimeit -s 'import pandas as pd'
100000000 loops, best of 3: 0.0122 usec per loop
python -mtimeit -s 'import pandas as pd; import matplotlib.pyplot as plt'
100000000 loops, best of 3: 0.0125 usec per loop
Finally, the reason the example you linked in comments doesn't need the call to matplotlib
is because it is being run interactively in an iPython notebook
, not in a script.
Related Topics
How to Check Which Version of Nltk, Scikit Learn Installed
How to See Pip Package Sizes Installed
How to Make Python3 Command Run Python 3.6 Instead of 3.5
Check If a Process Is Running Using Python on Linux
Runtimeerror: Invalid Display Variable
Linux Bash Script Running Multiple Python
Error While Installing Matplotlib
Typeerror: Argument 1 Must Be Pygame.Surface, Not Str How to Fix
Can't Install Gcloud on Amazon Linux:Invalid Syntax
How to Disable Stdout Buffer When Running Shell
Error: Could Not Build Wheels for Glpk Which Use Pep 517 and Cannot Be Installed Directly
Python Not Displaying Executable Output Properly
Cat, Grep and Cut - Translated to Python
Pipe Raw Opencv Images to Ffmpeg
Convert a Gtk Python Script to C
How to Get the Day of Week Given a Date