简体   繁体   English

如何从 Google Colab 中的脚本文件使用 pyplot 进行绘图?

[英]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() 输出。我有这样的东西: 将 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).如果我从控制台绘制一些数据(将 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.

相关问题 如何在 google colab 中创建实时 matplotlib.pyplot plot? - How to create a live matplotlib.pyplot plot in google colab? 如何在 Google Colab 笔记本的“.py”文件中运行 Python 脚本? - How to run a Python script in a '.py' file from a Google Colab notebook? 如何在 google colab 中显示 catboost 图? - How to show catboost plot in google colab? 如何将 Python 脚本从 Google Drive 下载到 Google Colab,并避免将脚本导入 Colab 后出现错误“” - How to download Python script from Google Drive to Google Colab, and avoid error "<!DOCTYPE html>" after importing script into Colab 如何在 PyPlot 中的分类箱线图上绘制拟合曲线? 为什么结果与 Google Sheets 中的同一个图不同? - How to plot a fitted curve over a categorical boxplot in PyPlot? Why does the result differ from the same plot in Google Sheets? 如何从Colab / Jupyter中的共享Google云端硬盘链接获取文件? - How to get file from a shared Google Drive link in Colab/Jupyter? 如何从 Google Colab 中的 HTML 文件中提取文本 - How to extract text from an HTML file in Google Colab 如何在 google Colab 中运行脚本 shell? - How to run a script shell in google Colab? 从本地驱动器读取文件到google colab - read in a file from a local drive to google colab 从 Google Colab 获取图像文件名 - Get Image File Name from Google Colab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM