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

北城余情 提交于 2020-08-19 09:06:20

问题


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.

I have tried adding code like plt.show() to the test script, but it doesn't show the graph in the Jupyter output. I have also tried adding %matplotlib inline to the Jupyter cell, but that doesn't help either.

In Jupyter Notebook cell:

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

In the test_plot.py Python script:

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)


回答1:


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



来源:https://stackoverflow.com/questions/57135570/matplotlib-plot-from-python-script-not-showing-up-in-output-when-run-in-jupyter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!