PeterHeuz
PeterHeuz

Reputation: 430

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

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:

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.

Upvotes: 8

Views: 15123

Answers (3)

Mario
Mario

Reputation: 274

Just another option if someone is facing the same issues:

Check, if interactive plots are enabled in the PyCharm settings.

Search for: Use "mpld3" interactive plots for Matplotlib

and disable it.

Upvotes: 0

anispepper
anispepper

Reputation: 1

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

Upvotes: -1

FlyingTeller
FlyingTeller

Reputation: 20526

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()

Upvotes: 7

Related Questions