3

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.

1
  • The trick is to understand what each of the available tools do, then use the most suitable one. The answers to your current question appear to be guess-and-check while the ones in the linked question have more substantive explanations based on an actual understanding of the system. Commented Feb 9, 2024 at 13:42

5 Answers 5

2

Try to add this

plt.show(block=True)

It works fine for my case.

Sign up to request clarification or add additional context in comments.

2 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 :(
That's because you need to show the window after you've finished the updates.
1

You could use the python debugging module to do this.

Use plt.ion() before you start plotting.

Then add import pdb; pdb.set_trace() at the point you want to start working interactively.

I use this method a lot for playing around with a complex plot until I'm happy with it.

Comments

0

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.

1 Comment

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.
0

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.

Comments

0

Alternatively, add user input to keep the plot window until you enter something in the terminal

import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6,2.7])
input('> hit return to dismiss plot')

Or use plt.waitforbuttonpress to display plot until the plot window is clicked

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.