在Google Colaboratory中保存或下载可绘制的iplot图像 [英] Saving or downloading plotly iplot images on Google Colaboratory
问题描述
我一直在尝试下载在Google colaboratory上使用plotly创建的图.到目前为止,这是我尝试过的:
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),您需要安装Orca,这实际上可以在Colab中使用以下命令来实现:
To save vector or raster images (e.g. SVGs or PNGs) from Plotly figures you need to have Orca installed, which is actually possible using the following commands in Colab:
!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 this is done you can use this 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中保存或下载可绘制的iplot图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!