如何使用pyplot从Google Colab中的脚本文件进行绘图? [英] How to plot with pyplot from a script file in Google Colab?
问题描述
我正在尝试从python脚本显示绘图到笔记本上,但是我得到的只是一个文本输出,显示了该图的type()输出.我有这样的东西:
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:
这是我的脚本(我的实际脚本的非常简化的版本,但概念相同).
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()
我也尝试过将后端设置为笔记本,但是得到的结果相同.如果我从控制台绘制一些数据(将python直接写入代码文本框,则绘制工作正常).我已经在很多论坛上搜索了答案,甚至在Colab中进行绘图的官方Google笔记本都说该绘图应该可以在脚本中正常工作.
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.
推荐答案
代替调用
!python plot.py
改为使用此
%run plot.py
它将正常显示情节.
这篇关于如何使用pyplot从Google Colab中的脚本文件进行绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!