[英]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:我试图从 python 脚本向笔记本显示一个绘图,但我得到的只是一个文本输出,显示了图形的 type() 输出。我有这样的东西:
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).如果我从控制台绘制一些数据(将 python 直接写入代码文本框,绘图工作正常)。 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.我已经在很多论坛中搜索了答案,甚至在 Colab 中绘图的官方 Google 笔记本都说绘图应该可以在脚本中正常工作。
Instead of calling而不是打电话
!python plot.py
Use this instead改用这个
%run plot.py
It will show the plot normally.它将正常显示情节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.