繁体   English   中英

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

[英]Matplotlib plot from Python script not showing up in output when run in Jupyter Notebook

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

我尝试将像plt.show()这样的代码添加到测试脚本中,但是它没有在Jupyter输出中显示图形。 我还尝试了将%matplotlib内联添加到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

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

暂无
暂无

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

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