How to save plotly offline by running my script

你离开我真会死。 提交于 2019-12-25 00:57:47

问题


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')

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.

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.


回答1:


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.

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.

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.

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.




回答2:


tried this https://plot.ly/python/static-image-export/ and volla!

though I had to struggle with orca.

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
import plotly.graph_objs as go
import plotly.io as pio


print(__version__)

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


fig = go.Figure()
fig.add_bar(x=df2['Category'],y=df2['Values'])

iplot(fig)
pio.write_image(fig, 'fig1.png')


来源:https://stackoverflow.com/questions/52549904/how-to-save-plotly-offline-by-running-my-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!