17

I wanted to use matplotlib in colab but it didn't work. As this link says I tried both %matplotlib inline and %matplotlib notebook but they both didn't work. But can I ask why this link plots are working well? It seems it's just using normal matplotlib with %matplotlib inline. I want to know the difference.

June Yoon
  • 433
  • 1
  • 3
  • 11

7 Answers7

6

According to colab docs:

In the IPython notebook, you also have the option of embedding graphics directly in the notebook, with two possible options:

%matplotlib notebook will lead to interactive plots embedded within the notebook.

%matplotlib inline will lead to static images of your plot embedded in the notebook.

Anup Tiwari
  • 474
  • 2
  • 5
  • I wrote some more detail to the question. Please read again. – June Yoon Sep 14 '20 at 22:12
  • Scroll to LEONARDO PEREIRA RODRIGUES's answer. I had the exact same issue and it fixed it for me! Other sources had been telling me to "Start by launching the IPython interpreter by typing 'ipython' on the command-line". You can't access the command line in colab without a paid subscription. – Allison B Mar 24 '23 at 13:28
4

I came up with the same problem with Google colab today, the solution is quite simple: Using %matplotlib inline

Here is my code:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

X = np.linspace(-0.1, 10, 20)
y = 2 * X - 1
plt.scatter(X, y)
2

Plotting from an IPython notebook

The IPython notebook is a browser-based interactive data analysis tool that can combine narrative, code, graphics, HTML elements, and much more into a single executable document.

Plotting interactively within an IPython notebook can be done with the %matplotlib command, and works in a similar way to the IPython shell. In the IPython notebook, you also have the option of embedding graphics directly in the notebook, with two possible options:

  • %matplotlib notebook will lead to interactive plots embedded within the notebook

  • %matplotlib inline will lead to static images of your plot embedded in the notebook

Sunil Vora
  • 63
  • 3
  • 6
    I tried both but they didn't work. My question is why, nonetheless, that [this link](https://colab.research.google.com/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/04.00-Introduction-To-Matplotlib.ipynb#scrollTo=eCo1lGY4UdF2) plots are working correctly. – June Yoon Sep 14 '20 at 22:08
2

Selecting a GPU from the hardware accelaration tab solved the issue for me.

Go into Edit > Notebook Settings > Select GPU in the drop-down.

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

I saw in some issue in github this soluction, and this solved my problem:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

https://github.com/jupyter/notebook/issues/3523

0

I was having a similar problem to the original author -- no matter what I tried, I could not get plots to display in a notebook, even though plots were displayed just fine in other notebooks. I found this very surprising.

I added a dead-simple test plot to the original notebook and to a brand new notebook, and this simple plot did not show up in a factory reset environment of the original notebook, while it was rendered without issue in a new notebook:

import matplotlib.pyplot as plt

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

Factory resetting the runtime, restarting the browser, and restarting the computer had no impact on this issue over the course of multiple days.

In the end, I found that closing the browser tab with the notebook, opening a new tab and browsing to colab, and selecting my notebook from the list of files resolved the issue. This was in Firefox running on Ubuntu 20.04.

0

%matplotlib notebook not working in google colab, Use instead %matplotlib inline

enter image description here

Pinaki
  • 792
  • 8
  • 17