Matplotlib while debugging in Pycharm: How to turn off interactive mode?
Asked Answered
P

2

7

First of all, I am working on the Pycharm debug console and want to put a caption under my diagram. According to this answer this can be achieved by:

plt.plot([2,5,1,2]
fig = plt.figure()
fig.text(.5, .05, "text", ha="center")
plt.show()

However, this shows me the plot at first, then an empty window (after typing the second line) and nothing later.

I figured out this must be because of interactive mode of matplotlib so I turned it off using plt.ioff() in the debug session after which plt.isinteractive() returns False. Still this does not change its behaviour and shows the plot right after the plt.plot(...) command.

Weirdly enough when I put plt.ioff() in my script, it is ignored and plt.isinteractive() returns True.

import matplotlib.pyplot as plt

plt.ioff()
plt.plot([1,2,3,4,5])
print(plt.isinteractive())

My system information:

  • PyCharm CE 2017.3.2
  • macOS Sierra 10.12.6
  • Python 3.6.3 in an Anaconda Environment

Can anyone reproduce this? Is there another way to create more complicated diagrams from the Pycharm debug console? I would prefer to not change my development environment everytime I want to plot something more complicated.

Personality answered 14/3, 2018 at 18:7 Comment(0)
A
6

To answer your question: use a different (non interactive) backend:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

Your code is probably not working because you created the figure instance after your plot. Try:

fig = plt.figure()
plt.plot([2,5,1,2]
fig.text(.5, .05, "text", ha="center")
plt.show()
Abele answered 14/3, 2018 at 21:40 Comment(10)
Using your suggested different backend does not show me any plot now. Nether with plt.plot(...) nor with plt.plot(...) and plt.show(). Executing my test file (without matplotlib.use('Agg')) from the bash works as expected: it displays the plots after plt.show() and I can even do interactive plotting with pdb.Personality
That is what a non interactive backend does, it is not supposed to open a window with your plot in it. Where you able to solve your original problem with the text not showing by switching the figure and plot call?Abele
The text works now, thanks! But my main problem that I want to plot in debug mode of pycharm just like it works from the bash with pdb is not solved.Personality
It still displays empty figures?Abele
It still either displays the plot directly when executing plt.plot(...) or nothing when I switch to a non-interactive backend like you described. I would like to have the plot displayed when I run plt.show() like it works on the bash with pdf.Personality
Could you add import matplotlib followed by print(matplotlib.rcParams['backend']) at the top of your script (before other imports) and report what it does when a) executed from bash b) executed using pycharmAbele
They both return MacOSXPersonality
Let us continue this discussion in chat.Personality
What is 'Agg' please?Propagate
@Propagate It"s the backend that uses the Anti-Grain Geometry engine. For more info on backends, read the docsAbele
C
-1

I solved this by adding this line matplotlib.use('module://backend_interagg'). None of the other backend options worked for me.

Certify answered 10/7 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.