python - 在 Jupyter Notebook 中运行时,来自 Python 脚本的 Matplotlib 图未显示在输出中

标签 python jupyter-notebook

尝试从我的 Jupyter 笔记本所在的目录运行 Python 脚本。脚本按预期执行,但绘图没有显示在我的 Jupyter 笔记本的输出中。

我尝试将 plt.show() 之类的代码添加到测试脚本中,但它没有在 Jupyter 输出中显示图形。我也尝试过将 %matplotlib inline 添加到 Jupyter 单元格中,但这也无济于事。

在 Jupyter Notebook 单元格中:

import matplotlib as plt
%matplotlib inline
!python test_plot.py

在 test_plot.py Python 脚本中:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()

预期结果: 当我直接在笔记本中运行代码时,

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

它产生一个分段函数。 (由于声誉低,无法发布图片)。然而,这不是实际结果。

在 Jupyter Cell 中打印的实际结果: 图(640x480)

最佳答案

尝试使用:

%run 'test_plot.py'

相对于

!python test_plot.py

您应该能够正确运行单元格并绘制所需的图表

关于python - 在 Jupyter Notebook 中运行时,来自 Python 脚本的 Matplotlib 图未显示在输出中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57135570/

相关文章:

python - 如何在 Python 中解压 RDD 中每个项目的值(列表)?

python - 有没有一种快速的方法来找到两个大字节序列不同的第一个偏移量?

python 3 Tkinter ComboBox 没有获得值(value)

python - App Engine(python)在生产中跳过 webapp 中间件(如 Appstats),但在开发服务器上运行良好

python - 如何使用pytest模拟类属性

python - Linspace 未在 python (jupyter) 上定义

python - 无法在 sqlite3 python 中对关闭的游标进行操作

python-3.x - 为什么网格只在最后一个子图上打开?

python - 使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本

python-3.x - CSV 文件无法上传