June Yoon
June Yoon

Reputation: 512

matplotlib plotting is not working in google colab

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.

Upvotes: 22

Views: 42914

Answers (8)

Oluwatomiwa Amosu
Oluwatomiwa Amosu

Reputation: 11

try this

if 'google.colab' in str(get_ipython()):
        get_ipython().run_line_magic('matplotlib', 'inline')

it worked for me, I guess it should for you.

Upvotes: 1

Pinaki
Pinaki

Reputation: 1030

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

enter image description here

Upvotes: 1

Roya Parsaee
Roya Parsaee

Reputation: 89

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)

Upvotes: 7

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

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

Upvotes: 4

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

Upvotes: 2

Tim Upthegrove
Tim Upthegrove

Reputation: 1

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.

Upvotes: 0

Sunil Vora
Sunil Vora

Reputation: 63

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

Upvotes: 2

Anup Tiwari
Anup Tiwari

Reputation: 494

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.

Upvotes: 8

Related Questions