如何通过运行脚本来离线保存Plot [英] How to save plotly offline by running my script

查看:144
本文介绍了如何通过运行脚本来离线保存Plot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jupyter笔记本中使用以下代码.

I am using below code in my jupyter notebook.

import pandas as pd
import numpy as np
%matplotlib inline

from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

print(__version__)

import cufflinks as cf
init_notebook_mode(connected=True)
cf.go_offline()

df2 = pd.DataFrame({'Category':['A','B','C'],'Values':[22,33,45]})
df2.iplot(kind='bar',x='Category',y='Values', asImage=True, filename='bar')

它提供了要在notebook中运行时要保存的图像,我将此代码保存在bar.py中,然后运行了python bar.py,这给了我错误You must be authenticated to generate an image via json.

Its giving an image to save while running in notebook, and i saved this code in bar.py and I ran python bar.py it's giving me the error You must be authenticated to generate an image via json.

我想运行脚本,将条形图保存为同一位置的图像,我不能使用notebook,因为该脚本将自动运行.

I want to run my script which will save my bar plot as image in the same location, I can't use notebook because this scripts is going to be in my automation.

推荐答案

Plotly图是使用HTML + Javascript生成的.在Jupyter Notebook中运行时,您已经在已在浏览器中运行的Web应用程序中,因此它可以直接呈现它们.

The Plotly graphs are generated in HTML+Javascript. When you run in Jupyter Notebook, you're in a web application that runs in the browser already, so it can render them directly.

在命令行上运行时,它可以为您生成带有图形的HTML文件,但是您需要在浏览器中将其打开以进行渲染.

When running on the command line, it can generate an HTML file with the graph for you, but you'll need to open that in the browser to have it rendered.

离线" 文档页面对此进行了说明.那里的文字说,只有在Notebook中运行时才能保存图像.似乎有一种使用在线模式生成图像的方法-为此,您将需要一个Plotly帐户和网络访问权限.

The Plotly Offline documentation page explains this. The text there says that you can save images only when running in Notebook. There seems to be a way to generate an image using online mode - you will need a Plotly account and network access for that.

您可能需要考虑使用其他绘图库来进行自动脱机工作,这不需要网络访问或运行HTML用户代理.

You might want to consider a different plot library for automated offline work, which does not require network access or running an HTML user agent.

这篇关于如何通过运行脚本来离线保存Plot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆