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.
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
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
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').
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With