简体   繁体   English

通过 matplotlib 在 Pycharm 调试控制台中进行交互式绘图

[英]Interactive plotting in Pycharm debug console through matplotlib

I have a python script I'm trying to debug and I'm using Pycharm Community Edition version 2016.3.2.我有一个正在尝试调试的 python 脚本,我正在使用 Pycharm 社区版 2016.3.2 版。

What I'd like to do is make some plots in the debug console (which I activate by setting a breakpoint and starting the debugger), but the problem is that the plot simply doesn't show up.我想做的是在调试控制台中绘制一些图(我通过设置断点并启动调试器来激活它),但问题是该图根本不显示。

Some code to get a reproducible example of my problem is provided on the official matplotlib documentation here , in particular this bit of code:这里的官方matplotlib文档中提供了一些代码来获得我的问题的可重现示例,特别是这段代码:

import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6, 2.7])

What I find strange is that if I open a new python console from inside pycharm, when executing this code pycharm pops up a new window showing the plot, but this doesn't happen if I paste the same code in the "debug" console.我觉得奇怪的是,如果我从 pycharm 内部打开一个新的 python 控制台,当执行此代码时,pycharm 会弹出一个显示绘图的新窗口,但如果我将相同的代码粘贴到“调试”控制台中,则不会发生这种情况。

In both cases, I get the following output in the console在这两种情况下,我都在控制台中得到以下输出

在此处输入图片说明

I found a potentially related post here , but frankly I can't tell if the two problems reduce to the same issue.我在这里找到了一个可能相关的帖子,但坦率地说,我不知道这两个问题是否归结为同一个问题。

  • Tested in python 3.8.12 , matplotlib 3.4.3 , PyCharm 2021.2.3 (Professional Edition)python 3.8.12matplotlib 3.4.3PyCharm 2021.2.3 (Professional Edition)
  • In PyCharm Create a Pure Python Project在 PyCharm 中创建一个纯 Python 项目
    • File New Project... Pure Python文件新建项目...纯 Python
    • 在此处输入图片说明
    • Finish the project setup完成项目设置
  • Create a new python file:创建一个新的python文件:
    • 在此处输入图片说明
  • After the package imports, specify an interactive backend包导入后,指定一个交互式后端
    • mpl.use('Qt5Agg')
    • mpl.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib as mpl
# mpl.use('Qt5Agg')  # interactive mode works with this, pick one
mpl.use('TkAgg')  # interactive mode works with this, pick one

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)

fig1, ax1 = plt.subplots()

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
ax1.axis('equal')

plt.show()

在此处输入图片说明

Debug Console调试控制台

  • The script also works with debug, but the plot shows and then goes away as the debugger closes, so an extra line is included to prevent the debugger from closing the plot window.该脚本也适用于调试,但绘图显示,然后在调试器关闭时消失,因此包含额外的行以防止调试器关闭绘图窗口。

在此处输入图片说明

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

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