'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.



Solution 1:[1]

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.

Solution 2:[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

Solution 3:[3]

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

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

Solution 4:[4]

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.

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anup Tiwari
Solution 2 Sunil Vora
Solution 3 Dharman
Solution 4 Tim Upthegrove
Solution 5 LEONARDO PEREIRA RODRIGUES