Matplotlib plot not showing with plt.ion() in a python script, but working in python shell

matplotlib not showing plot windows
matplotlib not showing plot mac
matplotlib not showing plot jupyter
plt.show not showing image
matplotlib is currently using agg, which is a non-gui backend, so cannot show the figure.
matplotlib is currently using a non-gui backend spyder
matplotlib do not show figure
plt.show() not working jupyter

I am trying to get an interactive matplotlib plot to run. From the tutorial I tried this code:

import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6, 2.7])

If I copy it into a shell everything works fine and a window opens. But if I try to execute the code in a script with python3 script.py after a second it returns back to the shell without showing any window.

It seems that this seems be a popular problem, but no solution I found on the internet was working.

I tried adding plt.show() and plt.draw(), as well a loop so that the program does not immediately exit. But then there is just the python rocket bouncing in my doc indefinitely.

I tried different backends, but the symptoms where the same for all of them.

I installed python3 using homebrew and matplotlib using pip3. I run macOS 10.14.6.

Try to add this

plt.show(block=True)

It works fine for my case.

Using matplotlib in a python shell, Show Page Source But when working from the python shell, you usually do want to update the plot with every command, e.g., after changing the xlabel() , or the that we did not import any matplotlib names because in pylab mode, a matplotlib aware run command to run matplotlib scripts efficiently. import matplotlib.pyplot as plt plt.ion() plt.plot([1.6, 2.7]) If I copy it into a shell everything works fine and a window opens. But if I try to execute the code in a script with python3 script.py after a second it returns back to the shell without showing any window.

I managed to get it to run, by adding plt.pause(0.25) in my loop to update the data, so that my final code would look like this:

import matplotlib.pyplot as plt
import random
plt.ion()
plt.plot([0, 1])

for i in range(50):
    print(i)
    plt.scatter(random.random(), random.random())
    plt.pause(0.25)

I don't understand why the pause is necessary, but maybe it helps someone else who is struggling.

Using matplotlib in a python shell, But when working from the python shell, you usually do want to update the plot with every command, eg, after changing the xlabel(), or the Note in the example above that we did not import any matplotlib names because in pylab mode, Just set your backend : TkAgg and interactive : True in your matplotlibrc file (see  import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range plt.plot(x, np.sin(x)) # Plot the sine of each x point plt.show() # Display the plot Running this on a python 3.7.3 conda environment installed on WSL.

Modification of answers from users Pei Li and OP plasmoctopus, but with a twist and remarks.

import matplotlib.pyplot as plt
import random
plt.ion()
plt.plot([1.6, 2.7])

for i in range(50):
    print(i)
    plt.scatter(random.random(), random.random())
    plt.pause(0.25)  # adds each datapoint to the graph slowly but visible. 
                     # Remove this to show the graph instantly once its constructed.

plt.show(block=True)  # this keeps the graph result shown. Close it manually.

Resolved: Matplotlib figures not showing up or displaying , Ever try to plot a matplotlib figure and not have it display to your screen? I coded up a quick Python script using matplotlib, executed the script, working with Debian based operating systems such as Ubuntu and Raspbian. plt.show​() Popping into a shell, I can access the matplotlib backend using the. Bug report matplotlib.plot.show always blocks the execution of python script Code for reproduction I try plt.show(block=False) , it failed and appear in a small moment then close itself. Code: import numpy as np import matplotlib.pyplot

matplotlib.plot.show always blocks the execution of python script , Code: import numpy as np import matplotlib.pyplot GitHub is home to over 50 million developers working together to host and review code, manage '__​main__': plt.ion() plt.figure(figsize=(10, 10)) plt.plot(range(5), lw=2, python -i my_script.py which drops you into an interactive interpreter after your  But yet there is not plot! Resolved: Matplotlib figures not showing up or displaying. As I hinted at earlier in this post, the missing figure issue is related to the matplotlib backend that does all the heavy lifting behind the scenes to prepare the figure. Popping into a shell, I can access the matplotlib backend using the matplotlib.get

Python for Scientists, 5.2.4 This should display a result rather like Figure 5.1. If it does not, then you need to experiment with other back-ends as described above. 1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 x=np.linspace(-np.pi 6 7 plt.ion() 8 plt.​plot(x, y) 9 plt.savefig('foo.pdf') 10 #plt.show() Lines 1, 4 and 5 should be familiar. But when working from the python shell, you usually do want to update the plot with every command, e.g., after changing the xlabel(), or the marker style of a line. While this is simple in concept, in practice it can be tricky, because matplotlib is a graphical user interface application under the hood, and there are some tricks to make the

Numerical Python: Scientific Computing and Data Science , By default, the plt.show call will hang until the window is closed. For a more interactive experience, we can activate interactive mode by calling the function plt​.ion. This instructs Matplotlib to take over the GUI event loop and show a window for a as it is created, returning the control flow to the Python or IPython interpreter. Bug report Bug summary I am trying to have my figures open up in external windows using the plt.show() function. However, It gives me no output other than something such as: <Figure size 640x480 with 1 Axes> I am running linux ubuntu lat

Comments
  • this shows the window, but seems to 'disable' interactive mode, as I cannot update data in the window in a loop afterwards. Unfortunately not a real solution to me :(
  • The pause is there to show the order of adding a datapoint. Outside and it will be shown only for 0.25s... kinda short ;-) and you should use plt.show(block=True) to keep is shown.