简体   繁体   English

在Pycharm中调试Matplotlib:如何关闭交互模式?

[英]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. 首先,我正在研究Pycharm调试控制台,并希望在我的图表下添加一个标题。 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 . 我发现这必须是因为matplotlib的交互模式所以我在调试会话中使用plt.ioff()将其关闭,之后plt.isinteractive()返回False Still this does not change its behaviour and shows the plot right after the plt.plot(...) command. 这仍然不会改变它的行为,并在plt.plot(...)命令之后plt.plot(...)显示情节。

Weirdly enough when I put plt.ioff() in my script, it is ignored and plt.isinteractive() returns True . 奇怪的是,当我把plt.ioff()放在我的脚本中时,它被忽略了, plt.isinteractive()返回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 PyCharm CE 2017.3.2
  • macOS Sierra 10.12.6 macOS Sierra 10.12.6
  • Python 3.6.3 in an Anaconda Environment Anaconda环境中的Python 3.6.3

Can anyone reproduce this? 任何人都可以重现这个吗? Is there another way to create more complicated diagrams from the Pycharm debug console? 还有另一种方法可以从Pycharm调试控制台创建更复杂的图表吗? I would prefer to not change my development environment everytime I want to plot something more complicated. 每次我想绘制更复杂的东西时,我宁愿不改变我的开发环境。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM