在 Google Colaboratory 上保存或下载 plotly iplot 图像 [英] Saving or downloading plotly iplot images on Google Colaboratory
问题描述
我一直在尝试下载使用 plotly 在 google colaboratory 上创建的情节.到目前为止,这是我尝试过的:
I have been attempting to download a plot created using plotly on google colaboratory. So far this is what I have attempted:
我尝试过改变
files.download('foo.svg')
到
files.download('foo')
我仍然没有得到任何结果.我导航到 Google colab 上的文件,但那里没有显示任何内容
and I still get no results. I navigated to the files on Google colab and nothing shows there
import numpy as np
import pandas as pd
from plotly.offline import iplot
import plotly.graph_objs as go
from google.colab import files
def enable_plotly_in_cell():
import IPython
from plotly.offline import init_notebook_mode
display(IPython.core.display.HTML('''<script src="/static/components/requirejs/require.js"></script>'''))
init_notebook_mode(connected=False)
#this actually shows the plot
enable_plotly_in_cell()
N = 500
x = np.linspace(0, 1, N)
y = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
df.head()
data = [
go.Scatter(
x=df['x'], # assign x as the dataframe column 'x'
y=df['y']
)
]
iplot(data,image = 'svg', filename = 'foo')
files.download('foo.svg')
这是我得到的错误:
OSErrorTraceback (most recent call last)
<ipython-input-18-31523eb02a59> in <module>()
29 iplot(data,image = 'svg', filename = 'foo')
30
---> 31 files.download('foo.svg')
32
/usr/local/lib/python2.7/dist-packages/google/colab/files.pyc in download(filename)
140 msg = 'Cannot find file: {}'.format(filename)
141 if _six.PY2:
--> 142 raise OSError(msg)
143 else:
144 raise FileNotFoundError(msg) # pylint: disable=undefined-variable
OSError: Cannot find file: foo.svg
推荐答案
要从 Plotly 图形中保存矢量或光栅图像(例如 SVG 或 PNG),您需要有 Kaleido(首选)或 Orca(旧版)已安装,实际上可以在 Colab 中使用以下命令:
To save vector or raster images (e.g. SVGs or PNGs) from Plotly figures you need to have Kaleido (preferred) or Orca (legacy) installed, which is actually possible using the following commands in Colab:
万花筒:
!pip install kaleido
虎鲸:
!pip install plotly>=4.0.0
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca
!chmod +x /usr/local/bin/orca
!apt-get install xvfb libgtk2.0-0 libgconf-2-4
一旦上述任何一项完成,您就可以使用以下代码制作、显示和导出图形(使用 plotly
版本 4):
Once either of the above is done you can use the following code to make, show and export a figure (using plotly
version 4):
import plotly.graph_objects as go
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
fig.show()
fig.write_image("image.svg")
fig.write_image("image.png")
然后可以通过以下方式下载文件:
The files can then be downloaded with:
from google.colab import files
files.download('image.svg')
files.download('image.png')
这篇关于在 Google Colaboratory 上保存或下载 plotly iplot 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!