Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot with pyplot from a script file in Google Colab?

I am trying to show a plot to the notebook from a python script, but all I get is a text output showing me the type() output of the figure.I have something like this: Setting matplotlib backend to inline, then calling the script.

This is my script (a very simplified version of my actual script, but same concept).

import matplotlib.pyplot as plt
x=[1,2,3,4,5,6,5,3,2,4,2,3,4,2]

plt.plot(x)
plt.show()

I have also tried setting the backend to notebook, but I get the same result. If I plot some data from the console (writing python directly into the code textbox, the plot works fine). I have searched for the answer in a lot of forums, and even the official Google notebook to plotting in Colab says that the plot should work fine from within a script.

like image 413
Juan David Avatar asked Jan 30 '20 01:01

Juan David


1 Answers

Instead of calling

!python plot.py

Use this instead

%run plot.py

It will show the plot normally.

like image 197
korakot Avatar answered Oct 01 '22 05:10

korakot