jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.4k stars 4.76k forks source link

`plt.show()` won't render when first run a cell #3670

Open DIYer22 opened 6 years ago

DIYer22 commented 6 years ago

If plot something in first cell after import matplotlib.pyplot as plt
notebook won't render the figure.

But run second cell is rendered.

plt

adharsh commented 6 years ago

Since you're using the Jupyter notebook to render graphs, you would need to explicitly make IPython work with matplotlib to render graphs within your cells.

You can do so by using this command before you plot: %matplotlib inline And you only need to do this once.

Try running the following code for your first cell:

%matplotlib inline

import matplotlib.pyplot as plt
plt.plot([0, 1, 2, 5])
plt.show()
DIYer22 commented 6 years ago

@adharsh Thanks, but I still think that if first cell can't render, the second cell shouldn't render too.

mpacer commented 6 years ago

Does the first render work in an older version of the notebook?

Also, could you try an older version of matplotlib to see whether the problem arises there?

DIYer22 commented 6 years ago

@mpacer I try it on Python 2.7 and notebook server is 5.0.0, first render work well. screenshot from 2018-06-13 01 35 40

adamjstewart commented 5 years ago

I'm seeing the exact same behavior, whenever I launch a notebook, the very first plot I try to render is not displayed. All subsequent plots are displayed. I have to rerun the first cell for the plot to be displayed.

screen shot 2018-10-21 at 7 34 14 pm
samuliasmala commented 5 years ago

I'm experiencing the same issue. The first plot in new notebook (or after kernel restart) is not shown: screenshot from 2018-10-28 01-07-43

However I found two work-arounds:

  1. Use %matplotlib inline in the cell where you first import the matplotlib screenshot from 2018-10-28 01-07-24
  2. Import matplotlib in previous cell: screenshot from 2018-10-28 01-06-44

Version information: screenshot from 2018-10-28 01-07-53

benjimin commented 5 years ago

Same issue.

Notebook server 5.7.0, Python 3.6.6, IPython 7.0.1, matplotlib 3.0.2.

custa commented 5 years ago

Same issue python 3.7.1 notebook 5.7.4 ipython 7.2.0

jjstarn-usgs commented 5 years ago

Same issue python 3.6.8 notebook 5.7.4 ipython 7.2.0

MichaelClerx commented 4 years ago

Same issue Python 3.7.6 notebook 5.7.8 ipython 7.2.0

atomicwelding commented 4 years ago

Same issue Python 3.8.2 jupyter-notebook : 6.0.3 ipython : 7.13.0

kevin-bates commented 4 years ago

@atomicwelding - could you please reconfirm with IPython 7.13.0? The IPython version matters for the kernel's runtime environment, not the notebook (in case you're spanning envs).

I have confirmed this works on both 7.10 and 7.13 and confirmed it didn't work on 7.8.0. I just found that @benjimin fixed this via https://github.com/ipython/ipython/pull/11916 - which first appears in 7.10.

EDIT: Since there may be a relation with ipykernel, my ipykernel version is 5.2.1 (but that remained constant over both success and failure trials).

image

ahlraf commented 3 years ago

Still doesn't work for me:
I've tried %matplotlib inline, %matplotlib notebook, and changed backend to TkAgg ( ref )

My code looks something like this: <%matplotlib inline> <import matplotlib> <matplotlib.use('TkAgg')> <import matplotlib.pyplot as plt>

and finally <plt.show()>

No result on running the cell - not even an address. Code works fine, an associated file is being updated and returned successfully.

Python 3.8.5 Jupyter 6.1.4 IPython 7.19.0