32

The plots are not displayed inline in notebooks when using matplotlib

The plots appear completely blank. Any ideas?

seen here

5
  • This happened me once. Are you importing matplotlib and pyplot? Or just using %matplotlib notebook . In my case I had to import them too. Commented Nov 27, 2017 at 9:50
  • Could it be that you try to switch the backend in the middle of the notebook? (See this question) Commented Nov 27, 2017 at 11:20
  • 1
    I'm getting this fairly often too and the cause isn't anything obvious to me. No multiple switching backends or simple failure to import. The problem is intermittent. Often just running the cell again will cause the figure to show up. Also, sometimes saving the figure (via the button) returns a blank figure too. Really odd and annoying. Commented Mar 23, 2018 at 21:17
  • Also... Happens under Firefox and Chrome. Restart and Run All seems to be screw up more than running individual cells (no, it isn't because Run All is hitting an error before rendering). Commented Mar 23, 2018 at 21:29
  • For me matplotlib.pyplot.show() works. Commented Dec 5, 2020 at 4:40

8 Answers 8

52

Came across the same issue on calling "Restart & Run all". Following this

%matplotlib notebook showing a blank histogram

I could resolve the issue adding

%matplotlib inline 

at the beginning of the cell. Also note that you can prevent string output by adding ; to the end of a line.

Sign up to request clarification or add additional context in comments.

2 Comments

this should be accepted answer
adding the %matplotlib inline didn't solve my problem with Sagemaker notebooks
6

I faced a similar issue with my Chrome Browser, whereas it works fine with Mozilla.

%matplotlib inline will lead to static images of your plot embedded in the notebook, so that cannot be the solution for this prevailing issue.

1 Comment

Your pointer towards trying a different browser saved my sanity, thanks... Still don't know why this happens though.
1

Had a similar issue - %matplotlib notebook command would not display the plot but %matplotlib inline would. Had to downgrade matplotlib version from 3.1.3 to 3.1.2.

Python 3.7.9, conda 4.5.11

Comments

1

If you're trying to create a graph using the Object-Oriented Interface try adding

fig, ax = plt.subplots() 

above your ax.plot

Comments

0

Having the whole snippet in the same cell solves this issue. This means that first you add %matplotlib inline in one cell. Run it. Then you add all the elements of the figure, axes and all in the same cell, then run it. It shows the chart perfectly!

Comments

0

I find inconsistent behaviour, but any of the following 2 methods should work:

Method 1 Start a cell like so:

import matplotlib matplotlib.use('TkAgg') from matplotlib import pyplot as plt plt.ion() 

Then in the next cell type:

plt.hist([3,8,10]) 

In most cases the plot will appear in its own window.

Method 2 However, sometimes (not sure why), this does not work even after a kernel restart.

You may see a little window appear that does not show the plot but remains blank and if you hover over it with the mouse, you'll get the "thinking" icon.

In that case end your plotting cell like so:

plt.pause(1) 

This should show the plots in the figures and they should be responsive too.

Good luck!

Tested on:

Windows 10 jupyter running inside Visual Studio Code 1.83.1 jupyter_client==8.6.0 jupyter_core==5.7.1 jupyterlab_widgets==3.0.10 python 3.10.13 matplotlib 3.8.2 

Comments

0

I got solution on this. Object creation, add_axes and plot do in one cell you will get result.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

In case you do not have any output after running the cell, make sure that the cell is a code cell - In my case the cell was mistakenly turned into a 'raw' cell.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.