Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 760
plasmoctopus Avatar asked Sep 22 '26 07:09

plasmoctopus


2 Answers

Try to add this

plt.show(block=True)

It works fine for my case.

like image 56
Pei Li Avatar answered Sep 24 '26 01:09

Pei Li


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.

like image 22
Not Dr Jeff Avatar answered Sep 24 '26 00:09

Not Dr Jeff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!