8

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.

3 Answers 3

7

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()
Sign up to request clarification or add additional context in comments.

10 Comments

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.
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?
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.
It still displays empty figures?
@slashdottir It"s the backend that uses the Anti-Grain Geometry engine. For more info on backends, read the docs
|
0

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.