29

The plots are not displayed inline in notebooks when using matplotlib

The plots appear completely blank. Any ideas?

seen here

DotPi
  • 3,977
  • 6
  • 33
  • 53
WilliamGates
  • 291
  • 1
  • 3
  • 4
  • This happened me once. Are you importing matplotlib and pyplot? Or just using `%matplotlib notebook` . In my case I had to import them too. – Ivan Nov 27 '17 at 09:50
  • Could it be that you try to switch the backend in the middle of the notebook? (See [this question](https://stackoverflow.com/questions/41125690/matplotlib-notebook-showing-a-blank-histogram)) – ImportanceOfBeingErnest Nov 27 '17 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. – travc Mar 23 '18 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). – travc Mar 23 '18 at 21:29
  • For me `matplotlib.pyplot.show()` works. – zyy Dec 05 '20 at 04:40

5 Answers5

42

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.

dgruending
  • 973
  • 9
  • 15
5

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.

Akshar Patel
  • 61
  • 1
  • 1
  • Your pointer towards trying a different browser saved my sanity, thanks... Still don't know why this happens though. – Anakhand Nov 04 '20 at 22:13
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

1

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

fig, ax = plt.subplots() 

above your ax.plot

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
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!