简体   繁体   中英

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: 将 matplotlib 后端设置为内联,然后调用脚本。

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.

Instead of calling

!python plot.py

Use this instead

%run plot.py

It will show the plot normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM