简体   繁体   English

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

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

Trying to run a Python script from the directory that my Jupyter notebook is in. The script executes as expected, but the plot does not show up in the output in my Jupyter Notebook. 尝试从Jupyter笔记本所在的目录中运行Python脚本。脚本按预期执行,但该图未显示在Jupyter笔记本的输出中。

I have tried adding code like plt.show() to the test script, but it doesn't show the graph in the Jupyter output. 我尝试将像plt.show()这样的代码添加到测试脚本中,但是它没有在Jupyter输出中显示图形。 I have also tried adding %matplotlib inline to the Jupyter cell, but that doesn't help either. 我还尝试了将%matplotlib内联添加到Jupyter单元中,但这也无济于事。

In Jupyter Notebook cell: 在Jupyter Notebook单元中:

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

In the test_plot.py Python script: 在test_plot.py Python脚本中:

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

Expected Results: When I run the code straight in the notebook, 预期结果:当我直接在笔记本中运行代码时,

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

it produces a piecewise function. 它产生一个分段函数。 (Can't post image due to low reputation). (由于声誉低下,无法发布图片)。 However, this is not the actual result. 但是,这不是实际结果。

Actual results printed in Jupyter Cell: Figure(640x480) 实际结果打印在Jupyter Cell中:图(640x480)

Try using: 尝试使用:

%run 'test_plot.py'

as opposed to 相对于

!python test_plot.py

You should be able to run the cell correctly with the desired chart being plotted 您应该能够正确运行单元格并绘制所需的图表

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

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