Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.

like image 487
PeterHeuz Avatar asked Mar 14 '18 18:03

PeterHeuz


People also ask

How to use % magic in matplotlib and IPython?

Using the % magic is guaranteed to work in all versions of Matplotlib and IPython. Enable interactive mode. Disable interactive mode. Return whether plots are updated after every plotting command. Display all open figures. Run the GUI event loop for interval seconds. whether changes to artists automatically trigger re-drawing existing figures

What are the functions of the Pyplot module?

The pyplot module provides functions for explicitly creating figures that include interactive tools, a toolbar, a tool-tip, and key bindings: Creates a new empty figure.Figure or selects an existing figure Creates a new figure.Figure and fills it with a grid of axes.Axes

What is the use of Matplotlib in Pyplot?

Matplotlib keeps a reference to all of the open figures created via pyplot.figure or pyplot.subplots so that the figures will not be garbage collected. Figure s can be closed and deregistered from pyplot individually via pyplot.close; all open Figure s can be closed via plt.close ('all').

What is debugging in PyCharm?

This facilitates the process of detecting and fixing bugs in your program. There is a variety of ways how you can run a debugging session, however, for simplicity this documentation assumes that you are building and running your project from PyCharm. This is the most common case, and it has fewer limitations as compared to more advanced techniques.


1 Answers

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()
like image 82
FlyingTeller Avatar answered Oct 15 '22 04:10

FlyingTeller