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

标签 python pycharm

我有一个正在尝试调试的 python 脚本,我使用的是 Pycharm Community Edition 版本 2016.3.2。

我想做的是在调试控制台中绘制一些图(我通过设置断点和启动调试器来激活它),但问题是图根本不显示。

在官方 matplotlib 文档中提供了一些代码来获得我的问题的可重现示例 here ,特别是这段代码:

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

我觉得奇怪的是,如果我从 pycharm 内部打开一个新的 python 控制台,当执行这段代码时,pycharm 会弹出一个显示绘图的新窗口,但如果我在“调试”中粘贴相同的代码,则不会发生这种情况"控制台。

在这两种情况下,我都会在控制台中得到以下输出

enter image description here

我找到了一个可能相关的帖子 here ,但坦率地说,我无法判断这两个问题是否归结为同一个问题。

最佳答案

  • 测试于python 3.8.12matplotlib 3.4.3PyCharm 2021.2.3(专业版)
  • 在 PyCharm 中创建一个纯 Python 项目
    • 文件 新项目... 纯Python
    • enter image description here
    • 完成项目设置
  • 创建一个新的 python 文件:
    • enter image description here
  • 包导入后,指定一个交互式后端
    • 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()

enter image description here

调试控制台

  • 脚本也适用于调试,但绘图会显示,然后在调试器关闭时消失,因此包含额外的一行以防止调试器关闭绘图窗口。

enter image description here

关于python - 通过 matplotlib 在 Pycharm 调试控制台中进行交互式绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43189394/

相关文章:

python - 如何在 Python 控制台中使用其选项运行 .py 文件?

python - 为什么它说我的类(class)没有该属性?

Python 3.9 和 Pycharm、HTMLParser AttributeError

opencv - 在 pycharm 中安装 OpenCV 时出错 - python 3.5.3

python - 显示 chrome 历史文件的 sql 数据库

python - Pycharm 在配置为使用 virtualenv 解释器时使用基本解释器

python Bottle网络服务器缓存html文件

Python重新无限执行

python - 在父类中调用 `super()`

unit-testing - 在 PyCharm 中禁用 Pytest