11

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.

1 Answer 1

20

Instead of calling

!python plot.py

Use this instead

%run plot.py

It will show the plot normally.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm! Could you explain the difference?
%run will load the code line-by-line, as if you type it all in Colab. !python will run it separately, so it cannot access the output cell, and cannot display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.