6

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.

PeterHeuz
  • 390
  • 1
  • 4
  • 17

1 Answers1

4

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()
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • 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. – PeterHeuz Mar 15 '18 at 07:37
  • 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? – FlyingTeller Mar 15 '18 at 07:45
  • 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. – PeterHeuz Mar 15 '18 at 08:13
  • It still displays empty figures? – FlyingTeller Mar 15 '18 at 08:19
  • 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. – PeterHeuz Mar 15 '18 at 12:55
  • 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 pycharm – FlyingTeller Mar 15 '18 at 13:35
  • They both return `MacOSX` – PeterHeuz Mar 16 '18 at 07:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166941/discussion-between-peterheuz-and-flyingteller). – PeterHeuz Mar 16 '18 at 07:36
  • What is 'Agg' please? – slashdottir Nov 03 '19 at 23:53
  • 1
    @slashdottir It"s the backend that uses the `Anti-Grain Geometry engine`. For more info on backends, [read the docs](https://matplotlib.org/3.1.1/tutorials/introductory/usage.html#what-is-a-backend) – FlyingTeller Nov 04 '19 at 12:36